Search code examples
c++qtunit-testingcmakeqt-creator

How to group Qt Test cmake targets in QtCreator?


My project structure looks like this

 - application
   - main.cpp
   - something.h
   - something.cpp
   - something_else.h
 - unit_tests
   - test_something.cpp
   - test_something_else.cpp
 - CMakeLists.txt

Until now i was using Catch2 for testing, and i had 2 nice projects, one for the app, one for the tests. But now also some signal and slot based classes need to be tested, and that is much easier with Qt Test. So I'm switching to that.

My CMakeFile looks like this:

cmake_minimum_required(VERSION 3.21)
project(application_name LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt6 REQUIRED COMPONENTS Core)
find_package(Qt6 REQUIRED COMPONENTS Test)
enable_testing(true)
add_executable(application_name application/main.cpp application/something.h ...)
target_link_libraries(application_name PRIVATE Qt::Core)

enable_testing(true)

add_executable(test_something unittests/test_something.cpp)
add_test(NAME test_something COMMAND test_something)
target_link_libraries(test_something PUBLIC Qt::Test)

add_executable(test_something_else unittests/test_something_else.cpp)
add_test(NAME test_something_else COMMAND test_something_else)
target_link_libraries(test_something_else PUBLIC Qt::Test)

This works, i can run the tests form QtCreator, in the test pane or manually via the Build->Run.

What bothers me, are the following:

  1. Every single unit test shows up in the project view as a separate project with one file.
  2. The run configuration editor also shows every single test.

My actual project has several non-qt-test targets, and so the additional targets are cluttering up these two panes.

I would like to group the tests in the project view and ideally in the run configuration view. Is this possible? For me it would be ideal to put all tests into one project, but this doesn't seem to be the canonical way for Qt Tests. Or am i missing something?


Solution

  • It is possible to make a subdirectory, in it, create a new CMakeLists.txt and put all tests there. finally include that in the main project with add_subdirectory.

    I moved away from qt unit tests in the meanwhile as i discovered, that it's possible to test signals and slots in catch2. but I saw that the above solution works.