Build with GCC and flags -fprofile-arcs -ftest-coverage. Then run ctest normally to run all the tests. Then run ctest -T Coverage to collect coverage results. The two ctest calls can be combined as ctest -T Test -T Coverage.
I’ve opened CMake Issue 19942 to propose the addition of a guide to the documentation.
Just to add some info for whoever may find it useful, ctest --test-dir build/ -T Test -T Coverage works from command line fine, but it looks like a ctest script needs to be pointed at gcov on the system:
Without find_program and CTEST_COVERAGE_COMMAND, ctest -V -S MyCustomCTestJob.cmake complains that gcov is not found and does not run the coverage step.
Also, here I used a “Coverage” build type that is defined in CMakeLists.txt following the example from “Professional CMake” in the section “33.2.1. gcov-based Coverage”: