Simple Unit Test Project

Let’s create a new unit test project firstly.



Write our test functions in feild private Q_SLOTS for the class.
tst_string.cpp:

#include <QString>
#include <QtTest>

class String : public QObject
{
Q_OBJECT

public:
String();

private Q_SLOTS:
void toUpper();
};

String::String()
{
}

void String::toUpper()
{
QVERIFY2(true, "Failure");
}

QTEST_APPLESS_MAIN(String)
#include "tst_string.moc"            // compiled from tst_string.cpp by Meta-Object Compiler

The Qt project file:

QT += testlib
TARGET = tst_string
CONFIG += console
CONFIG -= app_bundle

SOURCES += \
tst_string.cpp

DEFINES += SRCDIR=\\\"$$PWD/\\\"

The macro QTEST_APPLESS_MAIN will help us to create main function to run our test cases.
Then we can enter some commands in shell to run our test program.

qmake
make
./tst_string

Here are some output logs.

********* Start testing of String *********
Config: Using QtTest library 5.9.2, Qt 5.9.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by Clang 7.0.2 (clang-700.1.81) (Apple))
PASS : String::initTestCase()
PASS : String::toUpper()
PASS : String::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 1ms
********* Finished testing of String *********

Data Driving Test

We add a new test function void toUpper_data() in private slot field.

void TestQString::toUpper_data()
{
QTest::addColumn<QString>("string");
QTest::addColumn<QString>("result");

QTest::newRow("all lower") << "hello" << "HELLO";
QTest::newRow("mixed") << "Hello" << "HELLO";
QTest::newRow("all upper") << "HELLO" << "HELLO";
}

Change check function TestQString::toUpper to suit the current new data input way.

void TestQString::toUpper()
{
QFETCH(QString, string);    // take data from table
QFETCH(QString, result);

QCOMPARE(string.toUpper(), result);
}



The running result:

********* Start testing of String *********
Config: Using QtTest library 5.11.2, Qt 5.11.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by Clang 8.1.0 (clang-802.0.42) (Apple))
PASS : String::initTestCase()
PASS : String::toUpper(all lower)
PASS : String::toUpper(mixed)
PASS : String::toUpper(all upper)
PASS : String::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted, 2ms
********* Finished testing of String *********

code link:
github StringTest.
We should be careful that the name of data preparation function must have same prefix with test function’s and end with _data, eg: TestCases_data – TestCases

One more example

We can use qt test module to test the basic arithmetic operations: addition.
Here are main parts in test project.

#include <QtTest>
#include <QDebug>

// add necessary includes here

class PlusTest : public QObject
{
    Q_OBJECT

public:
    PlusTest();
    ~PlusTest();

private slots:
    void initTestCase();
    void cleanupTestCase();
    void TestCases_data();
    void TestCases();
};
//...
void PlusTest::TestCases_data()
{
    QTest::addColumn<int>("number1");
    QTest::addColumn<int>("number2");
    QTest::addColumn<int>("result");

    QTest::newRow("test1") << 1 << 1 << 2;
    QTest::newRow("test2") << 2 << 2 << 4;
    QTest::newRow("test3") << 1 << 2 << 3;
}

void PlusTest::TestCases()
{
    QFETCH(int, number1);    // take data from table
    QFETCH(int, number2);
    QFETCH(int, result);

    QCOMPARE(number1+number2, result);
}

QTEST_APPLESS_MAIN(PlusTest)

#include "tst_plustest.moc"

code link:
plus test

Categories: CPlusPlusQt

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Content Summary
: Input your strings, the tool can get a brief summary of the content for you.

X
0
Would love your thoughts, please comment.x
()
x