Wednesday, April 28, 2010

Sample Multiple Unit Test Project

Long time no post...

Anyway, someone asked me if I could upload a working test project that shows my AutoTest header in use, so click here to download which includes two sample test classes and a main.cpp that shows two ways to start the tests running.

Open the file using Qt Creator, build it and let it rip and you should see output that looks something like this:

********* Start testing of Test2 *********
Config: Using QTest library 4.6.2, Qt 4.6.2
PASS : Test2::initTestCase()
PASS : Test2::test1()
FAIL! : Test2::test2() Compared strings are not the same
Actual ("Hello"): Hello
Expected ("World!"): World!
test2.cpp(14) : failure location
PASS : Test2::cleanupTestCase()
Totals: 3 passed, 1 failed, 0 skipped
********* Finished testing of Test2 *********
********* Start testing of Test1 *********
Config: Using QTest library 4.6.2, Qt 4.6.2
PASS : Test1::initTestCase()
PASS : Test1::test1()
FAIL! : Test1::test2() '1 == 0' returned FALSE. ()
test1.cpp(14) : failure location
PASS : Test1::cleanupTestCase()
Totals: 3 passed, 1 failed, 0 skipped
********* Finished testing of Test1 *********

I hope you find this useful - it has certainly made my life easier when using QTestLib.


  1. Thanks ... this was helpful.

  2. Thank you! Very helpful in understanding the concept of unit tests

  3. pretty helpful, thanks a lot!

  4. Really Awesome!!

  5. Rob, this is great, thanks.

    However, could you clarify how you set up your folders and paths when testing a project?

    Qt Creator default is for the main project and test project to live in separate folders (and for their source and build to also remain separate).

    Including the main project's headers in the test project is simple (just add to the INCLUDEPATH in the .pro file), but I'm so far unable to get the test project's linker to find the main project's .o files and the symbols therein.

    Can you help?

  6. This comment has been removed by the author.

  7. I do not understand the working of AutoTest.h file. code is complicated. Can anyone please explain.

  8. Rob, it is great.
    Could I translate this into Chinese and put it on my blog? For is blocked by the Chinese government.
    Of cause I would put the source link there.

  9. Feel free to take the article and do what you want with it.

  10. Hi Rob,
    Why did you skip line at the end of test file
    #include "some_file.moc"?
    Is it no longer necessary when we have yours implementation?

  11. Great stuff, but I am afraid that ./testBin -function returns only the test functions from the last added class file to the .pro in HEADERS.

  12. Great job Rob..Many Thanks :)

  13. Thank you! This is the very first article helping me with Qt Testing. It costed me hours of googling to find it

  14. Thanks for sharing! Just what I needed, clean and simple!

  15. Rob, what license do you provide your code with?
    Would it be possible to use it in the commercial project?

    1. Use it however you like, just don't take credit for it. Commercial projects are fine.

    2. Thank you. Really useful!
      It would be nice if you would add a license/author comment to the top of the file.

  16. Very, very helpful! Thanks alot!

  17. "Sorry, you can't view or download this file at this time. Too many users have viewed or downloaded this file recently. [...]" - Would be nice to embed the code and / or provide a different download possibility. Only managed to download them via the diversion using the "Zip Extractor" App on Google Drive...

  18. This is great, If you ever are in Amsterdam I owe you a beer

  19. Nice article Rob,
    It would be really helpfull if you tell us how to get TestRun report in xml format. For now we get only one "test1" result in xml but we want both test cases "test1" and "test2 "in single xml file.

  20. This is great.
    By the way, you can get Jenkins-Xunit-plugin compatible xml output by running this with

    ./UnitTests -xml > result.xml

    Then split the result.xml at the <?xml version tag using:

    csplit -zs -f qunittest -b %02d.xml result.xml '/<\?xml/' '{*}'

    Don't forget to select QTestlib as the origin of the test results.

  21. Hello, I am unable to access the source code on the link given. Seems it has been removed and this post is very old. Is there any way I can access that code?