QtTest write unit tests

Chapter 1: Writing Unit Tests

How to write unit tests.
In the first chapter, we will see how to write a simple unit test for a class and how to execute it.

Write a test

Suppose you want to test the behavior of the QString class. First, you need a class that contains test functions. This class must inherit QObject:

//testqstring.cpp
#include <QtTest/QtTest>


class TestQString : public QObject
{
    Q_OBJECT
private slots:
    void toUpper();
};

void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
    QVERIFY(str.toUpper() == "HELLO");
}

QTEST_MAIN(TestQString)
#include "testqstring.moc"

Note: You need to include the QTest header and declare the test function as a private slot so that the testing framework can find and execute it.

Then you need to implement the test function itself. The implementation can be like this:

void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}

The QVERIFY() macro evaluates the expression passed as a parameter. If the calculation result of the expression is true, then continue to execute the test function. Otherwise, a message describing the failure is appended to the test log, and the test function stops executing.

But if you want a more detailed output to the test log, you should use the QCOMPARE() macro instead:

  void TestQString::toUpper()
  {
      QString str = "Hello";
      QVERIFY(str.toUpper() == "HELLO");
  }

If the strings are not equal, the contents of the two strings are appended to the test log, making it immediately show the reason for the failure of the comparison.

Finally, to make our test case a stand-alone executable file, the following two lines of code are required:

QTEST_MAIN(TestQString)
#include "testqstring.moc"

The QTEST_MAIN() macro expands to a simple main() method, which runs all test functions. Note that if the declaration and implementation of our test class are in the .cpp file, we also need to include the generated moc file to make Qt's introspection work.

Perform a test

Now that we have finished writing the test, we want to execute it. Assuming that our test is saved as testqstring.cpp in an empty directory, we use qmake to build the test to create a project and generate a makefile.

/myTestDirectory$ qmake -project "QT += testlib"
/myTestDirectory$ qmake
/myTestDirectory$ make

Note: If you are using windows, please replace make with nmake or any build tool you use.

Test output content

The resulting executable file should give you the following output:

********* Start testing of TestQString *********
Config: Using QtTest library 5.14.0, Qt 5.14.0 (i386-little_endian-ilp32 shared (dynamic) debug build; by MSVC 2017)
PASS   : TestQString::initTestCase()
PASS   : TestQString::toUpper()
PASS   : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 1ms
********* Finished testing of TestQString *********

Guess you like

Origin blog.csdn.net/qq_32312307/article/details/111878031