1

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.

cgmb
  • 4,284
  • 3
  • 33
  • 60
jahsiotr
  • 141
  • 1
  • 11
  • Everything you need is here: http://qt-project.org/doc/qt-4.8/qtestlib-tutorial.html – Mitch Dec 12 '12 at 17:26
  • As I stated above I have read this tutorial already, but I have no idea how to refer to it when it comes to methods that aren't from standard libraries. Trying to include header files to test file gives me an error, adding class files to the project result in nothing more. – jahsiotr Dec 12 '12 at 17:47
  • The first code snippet shows you how to `#include` the header files, if that's what you mean? There's also an attached `.pro` file that tells you what you need to add with the `CONFIG` variable. – Mitch Dec 12 '12 at 17:51

1 Answers1

1

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.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
  • What's with `QVERIFY2(true, "Failure");`? – cgmb Dec 12 '12 at 21:34
  • That was added by default by the Qt Creator wizard. I thought it could help understand how that works, so I left it there. – Luca Carlon Dec 12 '12 at 21:40
  • Well I am aware that there is nothing to test there, but my teacher doesn't really want to listen to that. He said do the tests so I'm doing them. Thanks for help anyways. – jahsiotr Dec 13 '12 at 11:11
  • @jahsiotr There actually is behaviour to test in toString. Your professor is right. A test like the above is useful. If you had, for example, misunderstood the requirements of the QDate format string, this test would show you that. – cgmb Dec 14 '12 at 08:25