How to use the testlib with header files
-
Hi everyone.
At my job I'm currently creating a serie of test for our GUI.
However according to the limited documentation I found online you're supposed to put all of your test code in a single .cpp file.
While I managed to replicate that and it works, having the entirety of our test code in a single .cpp file isn't viable. And separating our various tests across several independent .cpp file would result in in as many .exe being generated which also isn't ideal.From what I understand you need to have everything in a single .cpp file because of the Q_OBJECT and QTEST_MAIN macros that need to be in the same file (and no .h file forces us to include "my_test_class.moc" at the end of the file).
Is there a way to bypass this restriction ? Ideally I'd want to be able to separate my tests across multiple files (with or without headers) and still end up with generating a single .exe at the end.
Thanks in advance for the help :)
-
I don't really have an answer for you, but if you use CMake, you can use add_test() and then run all (or a selection) of your tests at once using ctest. With this project organization, having many test executables is not cumbersome to run and you can better isolate what each test is testing.
Also, you don't need all of your test code in a single .cpp file. You just need all of your test slots to be specified in the header.
-
Hi, thanks for your reply. Unfortunately i cannot use CMake, I must use so in-house build tools.
Seeing your last sentence I tried some new things, surprisingly they worked (I'm pretty certain I tried them before to no avail, weird).
Basically i have my matching xyz.h and xyz.cpp files, the .cpp is empty save for the QTEST_MAIN macro and the header file has the Q_OBJECT macro and the slots definitions. Then, like you said i simply add my new test functions definitons in the header and implement them in separate .cpp files.
The solution is so simple i feel dumb now, I swear i tried this exact solution before....
Anyway marking this as solved, thanks for the reply.