Question

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

QTest::qExec(TestDateDD/whateverTestClass);

I get the output in the console:

********* Start testing of TestDateDD *********
    Config: Using QTest library 4.8.0, Qt 4.8.0
    PASS   : TestDateDD::initTestCase()
    PASS   : TestDateDD::testValidity()
    FAIL!  : TestDateDD::testMonth(2012/7/10) Compared values are not the same
    Actual (date.longMonthName(date.month())): July
    Expected (monthname): June
    ..\UnitTestingPlugiin\TestDateDD.cpp(38) : failure location
    PASS   : TestDateDD::cleanupTestCase()
    Totals: 3 passed, 1 failed, 0 skipped
    ********* Finished testing of TestDateDD *********

However my requirement is to display this message in my GUI for each test slot. I did some research and found that the qtestlog.cpp uses its own messageHandler that manages the test outuput and displays the result as PASS or FAIL with the failure message,line number etc. Is there any way that I can handle this messageHandler of QTest for my GUI Application?

Was it helpful?

Solution

There's no way of changing the logger's functionality. The QAbstractTestLogger is hardcoded to either output to file or stdout, and you cannot supply your own implementation to QTestLog.

You could redirect stdout to your textbox, or you could specify -o filename as an argument when you call QTest::qExec and read back the file as it's being written out.

However, I'd instead recommend that you compile your tests into separate executables and run them using QProcess. It inherits QIODevice and it shouldn't be hard to send its data to your window.

A couple other benefits of using QProcess are that a crash or an assert in the unit test won't bring down your GUI, and that you'll be able to run multiple tests at the same time without muddling your output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top