I set my first steps upon the noble path of using unit tests to develop my application, but it proves to be a steep and rough one. I'm developing an application in Qt, so thought to reach for their QTestLib framework. Now, I understand how to make unit tests, but I can't seem to figure out how to incorporate unit testing into a project. Say I have a console application which just has a main.cpp and a cpp and h file for a class I want to develop, say MyClass. I guess the right thing to do would be to create a corresponding test class MyClassTest with its h and cpp files. But should I put it into the same project? Or rather create a separate project just for unit tests that will have access to files in "main" project (not sure how would I do that yet)?
And if both main application and the test are in the same project, how do I run tests without running the application or the other way round? I tried incorporating files from Qt's Tutorial 1 on unit testing into a console project, but the problem with it is that they use a macro QTEST_MAIN(TestQString)
which expands into a main(...)
function. This causes conflicts with the main(...)
in the main.cpp. Well, I can rename either one, but the problem still stands - how do I then run either the tests or the application itself? If I override QTEST_MAIN
macro (which is a bad idea anyway) I disable the tests. If I rename main(...)
function in the main.cpp, then the actual application never gets executed. I guess there should be a way to make two separate executables for the project, one running unit tests, and another launching the application, but can't figure out how to go about it.
Definitely create a separate project for your tests. If you arrange the application project and test project in side-by-side directories, you should be able to reference your code units from your application project using relative paths in your test.pro
file.
I use the very handy test runner presented on this blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With