创建
QTestLib框架提供了一个简单易用的单元测试框架,需要在工程文件中添加Qt+=testlib,或在新建项目是选择“其他项目-qt单元测试”,详细帮助请看qt4.8官方文档,Qt5官方文档
基本操作
class Untitled2Test : public QObject {
Q_OBJECT
public:
Untitled2Test();
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testCase1();
};
Untitled2Test::Untitled2Test() {
}
void Untitled2Test::initTestCase() {
}
void Untitled2Test::cleanupTestCase() {
}
void Untitled2Test::testCase1() {
QVERIFY2(true, "Failure");
}
QTEST_APPLESS_MAIN(Untitled2Test)
#include "tst_untitled2test.moc"
注意:
1、每一个测试类(Case)中的每一项测试条目必须在Qt槽中,否则无法调用“private Q_SLOTS:”“private slots:”均可
2、最后两行必须有,单元测试不具有Main函数,使用此函数实现自动添加并调用所有槽
1、initTestCase()
will be called before the first testfunction is executed.initTestCase()会在第一个测试函数执行前调用。
2、cleanupTestCase()
will be called after the last testfunction was executed.cleanupTestCase()会在最后一个测试函数执行后调用。
3、init()
will be called before each testfunction is executed.init()会在每一个测试函数执行前调用。
4、cleanup()
will be called after every testfunction.cleanup()会在每一个测试函数执行后调用。
高级操作
命令行操作-可以输出调试结果到文本
打开cmd,通过cd命令将目录指向到项目的debug目录下的debug,在此文件内应该可以直接看到测试项目的可执行exe文件,此时出入tst_qttesttest -xml -vs -lightxml -o testres.txt指令即可导出测试结果保存到texstres.txt文件中。
其中tst_qttesttest为测试项目对应的可执行文件名。
还可以使用以下命令行参