如何在单个txt文件中组合多个单元测试结果

时间:2014-07-17 10:19:48

标签: qt unit-testing qtestlib

我正在使用QTestLib Library和QTest来运行我的单元测试。我在Windows 7上工作并使用Qt 4.8当我使用:

运行我的测试时
int main(int argc, char *argv[])
{
    // Test gui widgets - 2 Spinboxes and 1 Combobox
    QApplication a(argc, argv);
    TestSpinBox  testSpinBoxObj;
    TestComboBox testComboBoxObj;

    QTest::qExec(&testComboBoxObj, argc,argv);
    QTest::qExec(&testSpinBoxObj, argc,argv);

    return 0;
}

我在控制台中获得输出:

Starting D:\Projects\Qt Learning\TestGui (1)\TestGui\debug\TestGui.exe...
********* Start testing of TestComboBox *********
Config: Using QTest library 4.8.1, Qt 4.8.1
PASS   : TestComboBox::initTestCase()
PASS   : TestComboBox::testComboBoxStepUp()
PASS   : TestComboBox::testComboBoxStepDown()
PASS   : TestComboBox::cleanupTestCase()
Totals: 4 passed, 0 failed, 0 skipped
********* Finished testing of TestComboBox *********
********* Start testing of TestSpinBox *********
Config: Using QTest library 4.8.1, Qt 4.8.1
PASS   : TestSpinBox::initTestCase()
PASS   : TestSpinBox::testSpinBoxStepUp()
PASS   : TestSpinBox::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestSpinBox *********
D:\Projects\Qt Learning\TestGui (1)\TestGui\debug\TestGui.exe exited with code 0

如何在单个文本文件中获取相同内容

1 个答案:

答案 0 :(得分:1)

There is -o filename option to specify output file。对于每个测试对象,您可以将输出重定向到自己的文件,然后将它们连接在一起。

QList<QObject *> objects;
objects << new TestSpinBox << new TestComboBox;

QString result;
foreach (QObject *o, objects) {
    QTemporaryFile f;
    f.open();
    QStringList args = app.arguments();
    args << "-o" << f.fileName();
    QTest::qExec(o, args);
    result += "\r\n" + f.readAll();
}
qDeleteAll(objects);