QTest - doesn't compile after adding second round/class of tests.
-
Good afternoon,
I decided to learn something new recently - QTest. I wrote tests for one of my classes and it did the job, allowed to find some otherwise hard to spot problems, so when I wrote another complex thing I decided to write tests for that too. But once it was ready to compile I got an error. No doubt I did something silly because I misunderstood the docs, but I am puzzled.Please bear in mind that it worked flawlessly until I added second test.
Structure:
- I have a lib called
core
where the classes to be tested come from; - original, working test is for the class
ConfigFile
. Test is intst_configfile.cpp
- additional one is for the class
HistoryClass
, test intst_historyclass.cpp
The error I get is:
suplicate symbol '_main' in: [...]Tests.dir/core/tst_historyclass.cpp.o [...]Tests.dir/core/tst_configfile.cpp.o ld: 1 duplicate symbols clang: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
Excerpt from the CMakeLists:
set(includes "../src/core/" ) set(SOURCES core/tst_configfile.cpp core/tst_historyclass.cpp ) add_executable(GemBrowserTests ${SOURCES} ) target_include_directories(GemBrowserTests PRIVATE "core") target_include_directories(GemBrowserTests PRIVATE ${includes}) add_test(NAME ConfigFile COMMAND ConfigFile) add_test(NAME HistoryClass COMMAND HistoryClass)
and includes follow, those didn't even change.
What I don't understand here?As usual, many thanks in advance.
- I have a lib called
-
You can add as many add_executable() to a CMakeLists.txt as you want.
Qt is using one test executable per class which I also consider as a good practice for unit tests. -
You can only have one QTest main, simply create two executables
-
@Christian-Ehrlicher stupid question: can I do that in the same CMake project, by means of another add_executable()?
Less silly question: what is considered a good practice in my case, where I want to test multiple classes? How do I construct tests for them? Separate tree of projects/CMakeLists.txt files?
-
You can add as many add_executable() to a CMakeLists.txt as you want.
Qt is using one test executable per class which I also consider as a good practice for unit tests. -
Awesome @Christian-Ehrlicher - I shall proceed in that direction. Thank you!
-
-
You should also take a look on add_test() - https://cmake.org/cmake/help/latest/command/add_test.html - to execute e.g. all tests at once.