Simple Unit Test Project

Let’s create a new unit test project firstly.

 

Write our test functions in the field for the class.
tst_string.cpp:
C++
#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 the main function to run our test cases.
Then we can enter some commands in the shell to run our test program.
Bash
qmake
make
./tst_string
Here are some output logs.
Bash
********* 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.
C++
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.
C++
void TestQString::toUpper()
{
    QFETCH(QString, string);    // take data from table
    QFETCH(QString, result);

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

The running result:
Bash
********* 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 the data preparation function must have the 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 the main parts in the test project.
C++
#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

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page