【Qt】QTest:编译Qt单元测试程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010168781/article/details/84324651
一、使用方法
1、测试程序源码

TestQString.pro

QT       += testlib
QT       -= gui
TARGET = tst_TestQStringTest
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += tst_TestQStringTest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

tst_TestQStringTest.cpp

#include <QString>
#include <QtTest>

class TestQStringTest : public QObject
{
	Q_OBJECT

public:
	TestQStringTest();

private Q_SLOTS:
	void toUpper_data();
	void toUpper();
};

TestQStringTest::TestQStringTest()
{
}

void TestQStringTest::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";
}

void TestQStringTest::toUpper()
{
	QFETCH(QString, string);
	QFETCH(QString, result);

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

QTEST_APPLESS_MAIN(TestQStringTest)

#include "tst_TestQStringTest.moc"
2、默认输出结果
********* Start testing of TestQStringTest *********
Config: Using QtTest library 5.6.3, Qt 5.6.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
PASS   : TestQStringTest::initTestCase()
PASS   : TestQStringTest::toUpper(all lower)
PASS   : TestQStringTest::toUpper(mixed)
PASS   : TestQStringTest::toUpper(all upper)
PASS   : TestQStringTest::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestQStringTest *********
3、命令行参数详解

$ ./tst_TestQStringTest -help
使用方法:./tst_TestQStringTest [选项] [被测函数[:测试的数据]]…
tst_TestQStringTest :是编译的单元测试程序名;
默认情况下,不指定函数名,将运行所有的测试函数。

-o 文件名,格式 : 将输出结果保存到指定格式的文件中,可以指定多个输出日志,不指定将输出到标准输出。
有效格式如下:
txt : 纯文本
csv : CSV 格式(适用于基准测试)
xunitxml : XUnit格式
xml : XML 文档格式
lightxml : XML标记流

-silent : 安静模式,只记录失败和错误;

$ ./tst_TestQStringTest -silent
Testing TestQStringTest
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted

默认打印信息如下

$ ./tst_TestQStringTest
********* Start testing of TestQStringTest *********
Config: Using QtTest library 5.6.3, Qt 5.6.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
PASS   : TestQStringTest::initTestCase()
PASS   : TestQStringTest::toUpper(all lower)
PASS   : TestQStringTest::toUpper(mixed)
PASS   : TestQStringTest::toUpper(all upper)
PASS   : TestQStringTest::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestQStringTest *********

-v1 : 打印每个函数,信息如:INFO : TestQStringTest::initTestCase() entering

$ ./tst_TestQStringTest -v1
********* Start testing of TestQStringTest *********
Config: Using QtTest library 5.6.3, Qt 5.6.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
INFO   : TestQStringTest::initTestCase() entering
PASS   : TestQStringTest::initTestCase()
INFO   : TestQStringTest::toUpper() entering
PASS   : TestQStringTest::toUpper(all lower)
PASS   : TestQStringTest::toUpper(mixed)
PASS   : TestQStringTest::toUpper(all upper)
INFO   : TestQStringTest::cleanupTestCase() entering
PASS   : TestQStringTest::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestQStringTest *********

-v2 : 除了打印v1的信息,还会打印QVERIFY/QCOMPARE/QTEST函数信息

$ ./tst_TestQStringTest -v2
********* Start testing of TestQStringTest *********
Config: Using QtTest library 5.6.3, Qt 5.6.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
INFO   : TestQStringTest::initTestCase() entering
PASS   : TestQStringTest::initTestCase()
INFO   : TestQStringTest::toUpper() entering
INFO   : TestQStringTest::toUpper(all lower) QCOMPARE(string.toUpper(), result)
  Loc: [../TestQString/tst_TestQStringTest.cpp(35)]
PASS   : TestQStringTest::toUpper(all lower)
INFO   : TestQStringTest::toUpper(mixed) QCOMPARE(string.toUpper(), result)
  Loc: [../TestQString/tst_TestQStringTest.cpp(35)]
PASS   : TestQStringTest::toUpper(mixed)
INFO   : TestQStringTest::toUpper(all upper) QCOMPARE(string.toUpper(), result)
  Loc: [../TestQString/tst_TestQStringTest.cpp(35)]
PASS   : TestQStringTest::toUpper(all upper)
INFO   : TestQStringTest::cleanupTestCase() entering
PASS   : TestQStringTest::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestQStringTest *********

-vs : 打印信号和槽信息

$ ./tst_TestQStringTest -vs
********* Start testing of TestQStringTest *********
Config: Using QtTest library 5.6.3, Qt 5.6.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
INFO   : TestQStringTest::initTestCase() Signal: QThread(7f3c80) started ()
PASS   : TestQStringTest::initTestCase()
PASS   : TestQStringTest::toUpper(all lower)
PASS   : TestQStringTest::toUpper(mixed)
PASS   : TestQStringTest::toUpper(all upper)
PASS   : TestQStringTest::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestQStringTest *********

-functions : 打印测试程序中的测试函数列表

$ ./tst_TestQStringTest -functions
toUpper()

-datatags : 打印测试程序中测试数据列表

$ ./tst_TestQStringTest -datatags
TestQStringTest toUpper all lower
TestQStringTest toUpper mixed
TestQStringTest toUpper all upper

-eventdelay ms : 设置模拟鼠标和键盘的默认延时时间,单位毫秒
-keydelay ms : 设置模拟键盘的默认延时时间,单位毫秒
-mousedelay ms : 设置模拟鼠标的默认延时时间,单位毫秒
-maxwarnings n : 设置最大输出行数,默认2000,0表示不限制
-nocrashhandler : 禁用崩溃处理程序,用于调试崩溃

基准测试相关选项:
-callgrind : 使用callgrind来计时基准测试
-perf : 使用事件来计时基准测试
-perfcounter name : 使用名为“name”的计数器
-perfcounterlist : 列出可用的计数器
-tickcounter : 使用CPU计数器计时基准测试
-eventcounter : 统计在基准测试期间收到的事件
-minimumvalue n :设置可接受的最小测量值
-minimumtotal n : 设置测试函数重复执行的最小可接受总数
-iterations n : 设置累计迭代次数.
-median n : 设置中值迭代的次数.
-vb : 打印详细的基准测试信息.
-help : help信息

二、基准测试

1、什么是基准测试

度娘:https://baike.baidu.com/item/基准测试/5876292?fr=aladdin
基准测试是 指通过设计科学的测试方法、测试工具和测试系统,实现对一类测试对象的某项性能指标进行定量的和可对比的测试。例如,对计算机CPU进行浮点运算、数据访问的带宽和延迟等指标的基准测试,可以使用户清楚地了解每一款CPU的运算性能及作业吞吐能力是否满足应用程序的要求;
再如对数据库管理系统的ACID(Atomicity, Consistency, Isolation, Durability, 原子性、一致性、独立性和持久性)、查询时间和联机事务处理能力等方面的性能指标进行基准测试,也有助于使用者挑选最符合自己需求的数据库系统。
从以上两个例子我们可以看出,可测量、可重复、可对比是基准测试的三大原则,其中可测量是指测试的输入和输出之间是可达的,也就是测试过程是可以实现的,并且测试的结果可以量化表现;可重复是指按照测试过程实现的结果是相同的或处于可接受的置信区间之内,而不受测试的时间、地点和执行者的影响;可对比是指一类测试对象的测试结果具有线性关系,测试结果的大小直接决定性能的高低。
对于可再现性,基准测试是最好的方法。
在开发阶段前期,应该使用基准测试来确定应用程序中是否出现性能倒退。基准测试可以在一个相对短的时间内收集可重复的结果。进行基准测试的最好方法是,每次测试改变一个且只改变一个参数。例如,如果想知道增加JVM内存是否会影响应用程序的性能,就逐次递增JVM内存(例如,从1024 MB增至1224 MB,然后是1524 MB,最后是2024 MB),在每个阶段收集结果和环境数据,记录信息,然后转到下一阶段。这样在分析测试结果时就有迹可循。
基准测试的关键是要获得一致的、可再现的结果。可再现的结果有两个好处:减少重新运行测试的次数;对测试的产品和产生的数字更为确信。使用的性能测试工具可能会对测试结果产生很大影响。假定测试的两个指标是服务器的响应时间和吞吐量,它们会受到服务器上的负载的影响。服务器上的负载受两个因素影响:同时与服务器通信的连接(或虚拟用户)的数目,以及每个虚拟用户请求之间的考虑时间的长短。很明显,与服务器通信的用户越多,负载就越大。同样,请求之间的考虑时间越短,负载也越大。这两个因素的不同组合会产生不同的服务器负载等级。记住,随着服务器上负载的增加,吞吐量会不断攀升,直到到达一个点。
当软件系统中增加了1个新模块,此时需要做基准测试,以判断新的模块对整个软件系统的性能影响。
基准测试程序(Benchmark)用来测量机器的硬件最高实际运行性能,以及软件优化的性能提升效果,可分为微基准测试程序(Microbenchmark)和宏基准测试程序(Macrobenchmark)。微基准测试程序用来测量一个计算机系统的某一特定方面,如CPU定点/浮点性能、存储器速度、I/O速度、网络速度或系统软件性能(如同步性能);宏基准测试程序用来测量一个计算机系统的总体性能或优化方法的通用性,可选取不同应用,如Web服务程序、数据处理程序以及科学与工程计算程序。
为了达到上述目标,基准测试程序需要满足如下条件:首先。基准测试程序包含最常见的计算、通信和访存模式,能够为实际的应用程序预测不同高性能计算系统性能排名;其次,能指导高性能计算系统和应用的改进,亦即在基准测试程序上有效的优化方法能移植到实际应用中。

2、例子源码

benchmarkTest.pro

QT       += testlib
QT       -= gui
TARGET = tst_BenchmarkTest
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += tst_BenchmarkTest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

tst_BenchmarkTest.cpp

#include <QString>
#include <QtTest>
#include <QDebug>

class BenchmarkTest : public QObject
{
	Q_OBJECT

public:
	BenchmarkTest();

private Q_SLOTS:
	void initTestCase();
	void cleanupTestCase();
	void toUpper_data();
	void toUpper();
};

BenchmarkTest::BenchmarkTest()
{
}

void BenchmarkTest::initTestCase()
{
	qDebug() << "=====start test=====";
}

void BenchmarkTest::cleanupTestCase()
{
	qDebug() << "=====stop test=====";
}

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

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

void BenchmarkTest::toUpper()
{
	QFETCH(QString, string);
	QFETCH(QString, result);

	QCOMPARE(string.toUpper(), result);

	QBENCHMARK {
		string.toUpper();
	}
}

QTEST_APPLESS_MAIN(BenchmarkTest)

#include "tst_BenchmarkTest.moc"
3、输出结果
********* Start testing of BenchmarkTest *********
Config: Using QtTest library 5.6.3, Qt 5.6.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
QDEBUG : BenchmarkTest::initTestCase() =====start test=====
PASS   : BenchmarkTest::initTestCase()
PASS   : BenchmarkTest::toUpper(all lower)
RESULT : BenchmarkTest::toUpper():"all lower":
     0.00048 msecs per iteration (total: 63, iterations: 131072)
PASS   : BenchmarkTest::toUpper(all upper)
RESULT : BenchmarkTest::toUpper():"all upper":
     0.00020 msecs per iteration (total: 54, iterations: 262144)
PASS   : BenchmarkTest::toUpper(mixed)
RESULT : BenchmarkTest::toUpper():"mixed":
     0.00020 msecs per iteration (total: 54, iterations: 262144)
QDEBUG : BenchmarkTest::cleanupTestCase() =====stop test=====
PASS   : BenchmarkTest::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of BenchmarkTest *********

猜你喜欢

转载自blog.csdn.net/u010168781/article/details/84324651
今日推荐