Home > Back-end >  How to group CTests together?
How to group CTests together?

Time:10-15

I would like to make groups of CTests to improve readability with a structure like this: enter image description here

How can I do this? My current CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(Test)

set(CMAKE_CXX_STANDARD 14)

enable_testing()
add_executable(env environment.cpp)
add_test(Environment env)
add_subdirectory(unit) # Includes tests 'UserInterface' and 'Test2'

The tests in the subdirectory are not grouped together when I run it:

enter image description here

CodePudding user response:

CTest doesn't support this, as it's not a classical unit testing framework, but rather a convenient way to "run stuff" that is already configured (in most cases) to be built with CMake.

What you can do is:

  • Impose a naming scheme, e.g. prefix tests by a common identifier. For all tests that are registered with add_test(foo_...), you can run this group with ctest -R foo_ for example.
  • Associate setup- and teardown-like test with a set of tests (have a look at test properties like FIXTURES_CLEANUP). However, this doesn't give you structure per se, it only ensures a certain existing test is run as a setup/teardown before/after the test in question.
  • Related