better qgui tester
2016-06-16 11:29
691 查看
之前写的qtestrunner不能测试gui程序,故应将其改进为如下:
qtestrunner.h
#ifndef TESTRUNNER_H #define TESTRUNNER_H #include <QTimer> #include <QtTest> /* Taken from https://stackoverflow.com/questions/1524390/what-unit-testing-framework-should-i-use-for-qt BEWARE: there are some concerns doing so, see https://bugreports.qt.io/browse/QTBUG-23067 */ class TestRunner : public QObject { Q_OBJECT public: 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[]) { QApplication app(argc, argv); QTimer::singleShot(0, this, SLOT(run())); app.exec(); return m_overallResult == 0; } private slots: void run() { doRunTests(); QApplication::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 (QApplication::arguments().size() > 1 && QApplication::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 (QApplication::arguments().size() > 1) { if (QApplication::arguments()[1] == "-classes") { // only print test classes qDebug().noquote() << testName; continue; } else if (QApplication::arguments()[1] != testName) { continue; } else { arguments = QApplication::arguments(); arguments.removeAt(1); } } else { arguments.append(QApplication::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
相关文章推荐
- Android之使用Http协议实现文件上传功能
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- mysql集群之MMM简单搭建
- Qt 5.6更新至RC版,最终版本近在咫尺
- 通晓网络测试常用命令
- Nodejs学习笔记之测试驱动
- Qt定时器和随机数详解
- 可以测试javascript运行效果的代码
- 使用php测试硬盘写入速度示例
- python 测试实现方法
- Qt实现图片移动实例(图文教程)
- 如何测试端口通不通(四种方法)
- 举例详解PHP脚本的测试方法
- 使用Jasmine和Karma对AngularJS页面程序进行测试
- Php-Redis安装测试笔记
- mysql压力测试脚本实例
- JavaScript 组件之旅(四):测试 JavaScript 组件
- js简单网速测试方法完整实例
- Qt for Android开发实例教程