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:
-
If you use Qt Creator: do you put your unit test classes in the same project of your application?
-
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!
-
-
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).
-
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 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 invokemake 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.