Search code examples
qtvisual-studio-2013qtestlibqt5.3

How to see QTest results


I am using QTest of Qt 5.3.2 to execute some unit tests on a class. I am using VS2013 Express to build the tester. The VS project file is generated from a .pro file using qmake. For running the tests I added the QTEST_MAIN to my source file.

The project builds and the tester executeable work fine. However I have problems seeing the results (e.g. PASS output). While the test is running a console window opens receiving all the testing output. If the test is finished the console window closes immediately and I can't actually see what it displayed. Especially I can not see if some tests failed.

I suppose QTest uses stdout as default output channel. So I tried the VS debugger option "Redirect stdout to output window" but it had no effect.

So how is this supposed to work?

Should the testing output actually go to a newly opened console window? Then why isn't it kept open? Or should the entire testing output go to the VS debugger output panel? Is there a way to redirect the output?


Solution

  • Do you have the Visual Studio Add-in installed for your version of Visual Studio? If not you should download it from qt-project.org/downloads.

    I did a simple qt test and imported the *.pro file inside Visual Studio and ran the test; for me everything works fine and the window stays open showing the resume of the tests:

    enter image description here

    What I recommend is writing the example, importing it in Visual Studio using the Visual Studio Add-in and compare the project settings. Chances are a flag needs to be changed somewhere in order for the window to stay opened.

    It also could be that you need Console (/SUBSYSTEM:CONSOLE) linker option set. Right click on the project, go to the project Properties, choose Configuration Properties>Linker>System. For the Subsystem property in the right-hand pane, click the drop-down box in the right hand column. Now choose Console (/SUBSYSTEM:CONSOLE) and reran the test(Ctrl+F5 and NOT just F5)(1).

    If you run the test in Debug mode (i.e. pressing F5) your window won't stay open. If you want it to stay open you need to add a getch() call in your test destructor, and when your test finishes the window will stay up waiting for input(e.g. Enter):

    For my test code would be:

    #include <QtTest/QtTest>
    #include <conio.h>
    
    class TestQString: public QObject
    {
        Q_OBJECT
    private slots:
        void toUpper();
    
    public:
        ~TestQString()
        {
            getch();
        }
    };
    
    void TestQString::toUpper()
    {
        QString str = "Hello";
        QCOMPARE(str.toUpper(), QString("HELLO"));
    }
    
    QTEST_MAIN(TestQString)
    #include "testqstring.moc"
    

    (1) - How to keep the console window open in Visual C++?