c++qtqt-creatorqttest

Qt Test command line options


I am very new to Qt and I am in the process of validating Qt Test unit testing framework. I am using Qt 5.5. I am able to create test cases using Qt macros and run them from Qt Creator but I am having issues when running from command prompt. Specifically, I am not able to use logging options provided by Qt Test.

Here’s what I am doing:

  1. Created a Qt Test project using Qt Creator
  2. Built the project using Qt Creator
  3. The executable is created in the “project_directory/debug” folder
  4. Open command prompt from debug folder
  5. Run the following commands:

QtTestValidation5.exe –xml
QtTestValidation5.exe -o results.txt, txt

  1. All the tests are executed but nothing is saved in the folder

I would like to log the test results as xml, csv and text file formats. Could someone please help?


Solution

  • The first option should output the test results to stdout in XML format. I've just tested it with my own library and it works. Although it outputs several concatenated XML documents, but that is probably because I'm using non-standard main() that executes several tests manually, so apparently one XML document is generated for each of those.

    The second one is probably erroneous: it should be -o results.txt,txt (no space). Works as me just as well, but only writes the last test. Again, that's probably because I'm executing several tests manually, so each one overwrites the previous and I'm seeing only the last one. If I want to save all the tests, I need to specify the format by using -xml or -txt and then redirect it to stdout:

    qztest.exe -xml > results.xml
    

    This works, but I'm still getting multiple concatenated documents in one file.

    The -csv option doesn't work, but then again Qt docs say

    This mode is only suitable for benchmarks, since it suppresses normal pass/fail messages.

    And I have no benchmarks.

    As for why the arguments work for me, here is my main():

    int main(int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
        int err = 0;
        {
            TestQuaZip testQuaZip;
            err = qMax(err, QTest::qExec(&testQuaZip, app.arguments()));
        }
        {
            TestQuaZipFile testQuaZipFile;
            err = qMax(err, QTest::qExec(&testQuaZipFile, app.arguments()));
        }
        // And so on, and so on...
        if (err == 0) {
            qDebug("All tests executed successfully");
        } else {
            qWarning("There were errors in some of the tests above.");
        }
        return err;
    }
    

    See, I create a QCoreApplication—that may be very important, and then I manually pass the arguments to QText::qExec. Since you have created your project with Qt Creator, you may wish to look at your main(). Maybe Qt Creator didn't initialize something properly. Remember for the arguments to work, the testing code should somehow be able to actually access those arguments! So you either have to pass them explicitly, or at least initialize the application so that the code can do something like QCoreApplication::instance()->arguments() internally.