c++unit-testingcode-coverage

Why are .gcda files not generated for indirectly called Functions in a Code Coverage Build?


I'm facing an issue with generating .gcda files for source files that are indirectly invoked in my test setup.

Here’s the situation:

I’ve enabled code coverage in my CMake configuration with the following flags:

if (ENABLE_CODE_COVERAGE STREQUAL "ON")
    message("Code coverage enabled")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 --coverage -Wall -fprofile-arcs -ftest-coverage")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage -lgcov")
endif()

This setup generates .gcno files for all source files as expected.

During testing, .gcda files are generated for the test files (SampleTest.cpp), and files of directly invoked functions(like utils.cpp) but not for the source files (Sample.cpp), even though .gcno files exist for those source files.

The source files are not invoked directly in the test file. Instead, I am using curl to trigger the execution of those functions via HTTP requests in a separate service.

What could be causing this behavior, and how can I ensure that .gcda files are generated for all indirectly invoked functions and files during my tests?


Solution

  • The .gcda file gets written from an atexit callback - so won't be there if your server process does not exit normally (say, crashes or killed at end of test procedure). It is possible to explicitly execute the callback - but probably easier/more portable to fix your test environment.