How do you do unit testing?



  • Hi,

    I'm trying to develop my first Qt application with unit tests, but before starting I'd like to collect some good practices, so here are some questions:

    1. If you use Qt Creator: do you put your unit test classes in the same project of your application?

    2. I'm trying to make my unit test classes separated, but I found documentation only about how to create an unique class that contains all tests and call QTEST_APPLESS_MAIN(). Does anyone have any tip and/or example about how to keep them separated, clean and well organized?

    Thanks!


  • Moderators

    There's a gorgeous tutorial here on DevNet: http://developer.qt.nokia.com/doc/qt-4.7/qtestlib-tutorial1.html Helped me a lot.

    About keeping it separate - most probably, that is a good path to take. Tests are not something an end used needs, they are for developers and people who compile from source. In Qt, I think the best way is to create a "subdir" qmake profile in your app. It will keep everything together in Qt Creator (one file to open the whole project), while giving you flexibility and separation of test cases from actual code. If you need examples/ tutorials, read qmake documentation, or look at live projects, like Qt itself ("link":https://qt.gitorious.org/qt/qtbase), or if you want a simpler example, you can look at my QtWebService ("link":https://gitorious.org/qwebservice).



  • I am using "cppunit-1.12.1 + qxrunner.0.9.2" for unit testing.
    it's very good.
    it provide a window, and you can choose which to test in the treeview like as below:

    @
    CA_TestCase
    test1
    test2
    CB_TestCase
    test1
    test2
    test3
    @



  • @
    core/src/core.pro
    A.h
    B.h
    A.cpp
    B.cpp


    core/src/test/test.pro
    A_TestCase.h
    B_TestCase.h
    A_TestCase.cpp
    B_TestCase.cpp
    @
    CA_TestCase is just to test CA.



  • Thanks folks, it's just what I need.

    I created a separated project ("tests") inside my project based on subdir template, and inside "tests" I put my test projects.

    Just one more question: is there a easy way to run all tests when I run the "tests" project? The only way I thought was creating a script myself to run all binaries created inside the build folder.



  • You might want to have a look at how Qt handles the tests.


  • Moderators

    [quote author="Esdras Beleza" date="1320203797"]Thanks folks, it's just what I need.

    I created a separated project ("tests") inside my project based on subdir template, and inside "tests" I put my test projects.

    Just one more question: is there a easy way to run all tests when I run the "tests" project? The only way I thought was creating a script myself to run all binaries created inside the build folder.[/quote]

    You're welcome. Franzk's tip is good, Qt does have tests that run automatically, but I do not know how they are making it happen. You can try to analyze the source, but it's complicated, or search the docs, or ask on "interest" mailing list (http://lists.qt-project.org/mailman/listinfo).



  • There's also the option of not using the "one app per test" approach. There is always the possibility of running through a list of registered tests in a main loop. This allows you to build all your tests into one program. QTestLib isn't fully geared towards this approach and you'll have to work your way around some limitations. In some places (particularly command line handling) QTestLib assumes there's going to be only one test to be run, and it will bail out as soon as an error condition is reached.



  • If anybody else find this post, I found nice solution:

    Mark all tests with CONFIG+=testcase in the .pro file
    then invoke make check' to run them all. (make -k check' if you want to keep running even if some of them fail)

    This can be configured as custom executable target in qtcreator to run all tests from qtcreator directly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.