3

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?

medasumanth
  • 381
  • 4
  • 10

1 Answers1

3

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.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
  • Thanks for information. I updated my commands based on your suggestions, but still the same. When I do: qztest.exe -xml > results.xml An xml document is saved but it looks more like a text file. I am guess the output is just thrown out to xml due to the usage of >. I updated the command for -o but still the file is not getting saved. Is there anything I have to do to the project? Or may be add or remove a component to make this work? – medasumanth Jan 12 '16 at 20:36
  • Not likely. I'd rather think about something with initialization code. I'll post my code to compare with yours. – Sergei Tachenov Jan 13 '16 at 04:25
  • 1
    Thanks for posting your code. The problem was that I wasn't passing `app.arguments()` to the `QText::qExec` function. I updated my main and it's working now. Thanks for solving my issue! – medasumanth Jan 13 '16 at 14:20