Qt 5.9.1 Reference Documentation
Chapter 1: Writing a Unit Test
Let's assume you want to test the behavior of our QString class. First, you need a class that contains your test functions. This class has to inherit from QObject:
假设你想要测试QString类的行为。首先,你需要一个包含你的测试函数的类。这个类需要继承自QObject。
#include <QtTest/QtTest>
class TestQString: public QObject
{
Q_OBJECT
private slots:
void toUpper();
};
Note: You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it.
Then you need to implement the test function itself. The implementation could look like this:
注意:你需要引入QTest头文件,并将测试函数声明为私有槽函数,这样测试框架会找到这些函数并执行。然后,你需要实现这些测试函数。一个简单的例子如下:
void TestQString::toUpper()
{
QString str = "Hello";
QVERIFY(str.toUpper() == "HELLO");
}
The QVERIFY() macro evaluates the expression passed as its argument. If the expression evaluates to true, the execution of the test function continues. Otherwise, a message describing the failure is appended to the test log, and the test function stops executing.
But if you want a more verbose output to the test log, you should use the QCOMPARE() macro instead:
void TestQString::toUpper()
{
QString str = "Hello";
QCOMPARE(str.toUpper(), QString("HELLO"));
}
If the strings are not equal, the contents of both strings are appended to the test log, making it immediately visible why the comparison failed.
Finally, to make our test case a stand-alone executable, the following two lines are needed:
如果2个字符串不相同,QCOMPARE会将2个值均打印出来。最后,要让我们的测试用例单独执行,需要添加以下2行:
QTEST_MAIN(TestQString)
#include "testqstring.moc"
The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.
/myTestDirectory$ qmake -project "QT += testlib"
/myTestDirectory$ qmake
/myTestDirectory$ make
Note: If you're using windows, replace make with nmake or whatever build tool you use.
Running the resulting executable should give you the following output:
注意:如果你使用windows,用nmake或者其他构建工具来代替make。运行测试项目对应可执行文件将会产生如下输出:
********* Start testing of TestQString *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS : TestQString::initTestCase()
PASS : TestQString::toUpper()
PASS : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********
Congratulations! You just wrote and executed your first unit test using the Qt Test framework.
Files:
tutorial1/testqstring.cpp
tutorial1/tutorial1.pro
Chapter 1: Writing a Unit Test
In this first chapter we will see how to write a simple unit test for a class, and how to execute it.
在这一章节我们将会看到如何为一个类写一个简单的单元测试并执行它。
Let's assume you want to test the behavior of our QString class. First, you need a class that contains your test functions. This class has to inherit from QObject:
假设你想要测试QString类的行为。首先,你需要一个包含你的测试函数的类。这个类需要继承自QObject。
#include <QtTest/QtTest>
class TestQString: public QObject
{
Q_OBJECT
private slots:
void toUpper();
};
Note: You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it.
Then you need to implement the test function itself. The implementation could look like this:
注意:你需要引入QTest头文件,并将测试函数声明为私有槽函数,这样测试框架会找到这些函数并执行。然后,你需要实现这些测试函数。一个简单的例子如下:
void TestQString::toUpper()
{
QString str = "Hello";
QVERIFY(str.toUpper() == "HELLO");
}
The QVERIFY() macro evaluates the expression passed as its argument. If the expression evaluates to true, the execution of the test function continues. Otherwise, a message describing the failure is appended to the test log, and the test function stops executing.
But if you want a more verbose output to the test log, you should use the QCOMPARE() macro instead:
QVERIFY宏函数可以比较一个表达式的结果是否为真。如果为真,测试函数将会继续,否则会添加一条消息描述失败情况到日志中,然后测试函数会被终止。
如果需要详尽的比较信息可以用QCOMPARE。
void TestQString::toUpper()
{
QString str = "Hello";
QCOMPARE(str.toUpper(), QString("HELLO"));
}
If the strings are not equal, the contents of both strings are appended to the test log, making it immediately visible why the comparison failed.
Finally, to make our test case a stand-alone executable, the following two lines are needed:
如果2个字符串不相同,QCOMPARE会将2个值均打印出来。最后,要让我们的测试用例单独执行,需要添加以下2行:
QTEST_MAIN(TestQString)
#include "testqstring.moc"
The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.
QTEST_MAIN() 宏函数简单地扩展了一个main函数,并在main函数中运行所有的测试函数。注意如果声明和实现都放在一个cpp文件时,需要包括.moc文件令Qt的内建机制工作。
Executing a Test
Now that we finished writing our test, we want to execute it. Assuming that our test was saved as testqstring.cpp in an empty directory, we build the test using qmake to create a project and generate a makefile.现在我们完成了单元测试的代码编写工作,我们希望执行它。假设我们的测试文件名为testqstring.cpp并位于一个空文件夹下,我们可以用qmake来创建一个工程然后产生makefile,用这个makefile来构建我们的测试。
/myTestDirectory$ qmake -project "QT += testlib"
/myTestDirectory$ qmake
/myTestDirectory$ make
Note: If you're using windows, replace make with nmake or whatever build tool you use.
Running the resulting executable should give you the following output:
注意:如果你使用windows,用nmake或者其他构建工具来代替make。运行测试项目对应可执行文件将会产生如下输出:
********* Start testing of TestQString *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS : TestQString::initTestCase()
PASS : TestQString::toUpper()
PASS : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********
Congratulations! You just wrote and executed your first unit test using the Qt Test framework.
Files:
tutorial1/testqstring.cpp
tutorial1/tutorial1.pro