Search code examples
qtunit-testingqtestlib

How to compose multiple unit test result in a single txt file


I am using QTestLib Library and QTest for running my unit tests. I am working on windows 7 and using Qt 4.8 When I run my test using:

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;
}

I get the output in the console:

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

How to get the same in a single text file


Solution

  • There is -o filename option to specify output file. For each test object you can redirect output to own file and later concatenate them together.

    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);