便捷的使用QtTest进行批量单元测试
来源:互联网 发布:sql安装教程 编辑:程序博客网 时间:2024/05/23 14:33
便捷的使用QtTest进行批量单元测试
QTestRunner类用于管理被测试类对象,便于进行单元测试:
代码如下:
qtestrunner.h
#ifndef TESTRUNNER_H#define TESTRUNNER_H#include <QList>#include <QTimer>#include <QCoreApplication>#include <QtTest>/*Taken from https://stackoverflow.com/questions/1524390/what-unit-testing-framework-should-i-use-for-qtBEWARE: there are some concerns doing so, see https://bugreports.qt.io/browse/QTBUG-23067*/class TestRunner : public QObject{ Q_OBJECTpublic: TestRunner() : m_overallResult(0) { QDir dir; if (!dir.exists(mTestLogFolder)) { if (!dir.mkdir(mTestLogFolder)) qFatal("Cannot create folder %s", mTestLogFolder); } } void addTest(QObject * test) { test->setParent(this); m_tests.append(test); } bool runTests(int argc, char * argv[]) { QCoreApplication app(argc, argv); QTimer::singleShot(0, this, SLOT(run())); app.exec(); return m_overallResult == 0; } private slots: void run() { doRunTests(); QCoreApplication::instance()->quit(); }private: void doRunTests() { // BEWARE: we assume either no command line parameters or evaluate first parameter ourselves // usage: // help: "TestSuite.exe -help" // run all test classes (with logging): "TestSuite.exe" // print all test classes: "TestSuite.exe -classes" // run one test class with QtTest parameters: "TestSuite.exe testClass [options] [testfunctions[:testdata]]... if (QCoreApplication::arguments().size() > 1 && QCoreApplication::arguments()[1] == "-help") { qDebug() << "Usage:"; qDebug().noquote() << "run all test classes (with logging):\t\t" << qAppName(); qDebug().noquote() << "print all test classes:\t\t\t\t" << qAppName() << "-classes"; qDebug().noquote() << "run one test class with QtTest parameters:\t" << qAppName() << "testClass [options][testfunctions[:testdata]]..."; qDebug().noquote() << "get more help for running one test class:\t" << qAppName() << "testClass -help"; exit(0); } foreach(QObject * test, m_tests) { QStringList arguments; QString testName = test->metaObject()->className(); if (QCoreApplication::arguments().size() > 1) { if (QCoreApplication::arguments()[1] == "-classes") { // only print test classes qDebug().noquote() << testName; continue; } else if (QCoreApplication::arguments()[1] != testName) { continue; } else { arguments = QCoreApplication::arguments(); arguments.removeAt(1); } } else { arguments.append(QCoreApplication::arguments()[0]); // log to console arguments.append("-o"); arguments.append("-,txt"); // log to file as TXT arguments.append("-o"); arguments.append(mTestLogFolder % "/" % testName % ".log,txt"); // log to file as XML arguments.append("-o"); arguments.append(mTestLogFolder % "/" % testName % ".xml,xunitxml"); } m_overallResult |= QTest::qExec(test, arguments); } } QList<QObject *> m_tests; int m_overallResult; const QString mTestLogFolder = "testLogs";};#endif // TESTRUNNER_H
使用方法如下:
int main(int argc, char* argv[]){ TestRunner testRunner; //your QTest compatible class here testRunner.addTest(new TestAdd); testRunner.addTest(new TestQString); bool pass = testRunner.runTests(argc, argv); qDebug() << "Overall result: " << (pass ? "PASS" : "FAIL"); return pass ? 0 : 1;}
参考
https://stackoverflow.com/questions/1524390/what-unit-testing-framework-should-i-use-for-qt
0 0
- 便捷的使用QtTest进行批量单元测试
- eclipse的便捷使用
- 使用EasyPusher SDK进行便捷开发
- 使用cactus进行单元测试的方法
- 使用Cactus+Jetty进行servelt的单元测试
- 使用 JUnit 进行 Java 代码的单元测试
- 使用 JUnit 进行 Java 代码的单元测试
- 使用DbUnit进行数据库操作的单元测试
- 使用Junit4进行单元测试的相关配置
- 使用 JUnit 进行 Java 代码的单元测试
- 使用Mocha进行Node.js的单元测试
- 使用VS2013进行C#程序的单元测试
- 使用MockMvc进行Spring的单元测试
- junit4 spring 便捷单元测试
- 使用NUnit进行单元测试
- 使用CppUnit进行单元测试
- 使用CppUnit进行单元测试
- 使用VSTS进行单元测试
- CF552C 进制
- 测试
- OpenCV学习转战Matlab
- Java多线程的同步与死锁
- 数据结构_P16
- 便捷的使用QtTest进行批量单元测试
- MFCC 过程理解
- eclipse快捷键大全
- mhash、mcrypt、PHP安装
- Redis常用命令集
- .NET截取指定长度的汉字超出部分用“···”代替
- django admin中的list_display全局变量
- 电磁阀
- GDB调试golang代码