Question

I need to make some unit test for my school project in Qt and although I have read Qt tutorial on that I can't figure out how am I supposed to write such tests. All of tests shown in tutorial I've mentioned refer to built-in methods. How should I write a unit test for a custom class say this is the simplest class I have:

task.h

#ifndef TASK_H
#define TASK_H

#include <QDateTime>
#include <QTime>

class Task
{
private:
    bool ifDone;
    QString name;
    QString description;
    QDateTime *startTime;
    QTime *start;
    QDateTime *endTime;
    QTime *end;
    bool neededReminder;
    QDateTime *reminderTime;

public:
    Task(QString _name, QString _description, QDate *dayClicked, 
         QTime *_startTime, QTime *_endTime, bool reminder);

    QString toString();
};

#endif // TASK_H    `

task.cpp

#include "task.h"

Task::Task(QString _name, QString _description, QDate *dayClicked, 
    QTime *_startTime, QTime *_endTime, bool reminder)
{
    ifDone = 0;
    name = _name;
    description = _description;
    start = _startTime;
    end = _endTime;
    startTime = new QDateTime(*dayClicked, *start);
    endTime = new QDateTime(*dayClicked, *end);
    neededReminder = reminder;
}

QString Task::toString() {
    QString task;
    task.append(this->name);
    task.append(" ");
    task.append(this->start->toString("HH:mm"));
    task.append(" - ");
    task.append(this->end->toString("HH:mm"));
    return task;
}

I was trying to #include this class to testing class as well as adding both .h and .cpp files to the project and I didn't manage to do anything. Can anyone write some sample testing methods (for toString method and constructor) for the above class so I could carry on myself with the rest. Thanks in advance.

Was it helpful?

Solution

To be sincere, I think this question should be closed and that you should start reading some of the books you've been provided. Also, I don't see what there is to test here, maybe the result of the string? However, see if this helps you:

UnitTests.pro

QT       += testlib
QT       -= gui
TARGET = tst_unitteststest
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += tst_unitteststest.cpp task.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
HEADERS += task.h

tst_unitteststest.cpp

#include <QString>
#include <QtTest>
#include "task.h"

class UnitTestsTest : public QObject
{
   Q_OBJECT

public:
   UnitTestsTest();

private Q_SLOTS:
   void initTestCase();
   void cleanupTestCase();
   void testCase1();
};

UnitTestsTest::UnitTestsTest()
{
}

void UnitTestsTest::initTestCase()
{
}

void UnitTestsTest::cleanupTestCase()
{
}

void UnitTestsTest::testCase1()
{
   QVERIFY2(true, "Failure");
   Task t("name", "desc", new QDate(1, 1, 2012), new QTime(0, 0), new QTime(1, 0), false);
   QVERIFY(t.toString() == "name 00:00 - 01:00");
}
QTEST_APPLESS_MAIN(UnitTestsTest)
#include "tst_unitteststest.moc"

Consider that there are mem leaks here... but I don't know how you want your class to become. The test will pass here of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top