Monday, March 28, 2011

A tiny utility - QTestLib application runner

The QTestLib framework, provided by Nokia, is a tool for unit testing Qt based applications and libraries. The framework is very easy to use and sutiable for TDD like development approach.

To make a test program, you just have to create the an unit test project in Qt Creator.

However, when the no. of test case growth, you may break down the test case into few different classes. But the code template generated by Qt Creator combined everything into a single  source file. And it do not support to run multiple test classes.

There have no a direct way to run all the test program in your project. You may have to spend extra time to reorganize the source file for this purpose. It is quite troublesome.

In order to simplify the process, I have made a tiny utility class called TestRunner. It provides a mechanism to exeucte multiple test program without any change of the program's source code. The result for each test program will be recorded and combined to a  summary report.

Example code: 

#include <QtCore/QCoreApplication>
#include "testrunner.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TestRunner runner;

    runner.run("../testa/tst_testa");
    runner.run("../testb/tst_testb");
    runner.run("../testc/tst_testc"); // not existed test program

    runner.report();

    return 0;
}
Example output:
********* Start testing of TestA *********
Config: Using QTest library 4.7.2, Qt 4.7.2
PASS : TestA::initTestCase()
PASS : TestA::simple()
FAIL! : TestA::fail() 'false' returned FALSE. ()
Loc: [../../testrunner/testa/tst_testa.cpp(26)]
PASS : TestA::cleanupTestCase()
Totals: 3 passed, 1 failed, 0 skipped
********* Finished testing of TestA *********
********* Start testing of TestB *********
Config: Using QTest library 4.7.2, Qt 4.7.2
PASS : TestB::initTestCase()
PASS : TestB::complex()
PASS : TestB::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestB *********
FAIL! : Can not start "../testc/tst_testc"
Totals: 6 passed, 2 failed, 0 skipped
********* Finished testing *********
The TestRunner is a C++ class with only very simple interface to execute the binary built with QTestLib , but it will save your time for running all the test program developed.  

The code is licensed under new BSD.(Well.. It is almost meaningless as I don't think you will bundle the class with your binary application)

The source code and example can be found at GitHub:

https://github.com/benlau/testrunner

No comments:

Sponsor