# Guideline for code coverage

**URL:** <https://discourse.cmake.org/t/guideline-for-code-coverage/167>\
**Category:** Usage\
**Created:** [November 7, 2019, 8:19pm UTC](https://discourse.cmake.org/t/guideline-for-code-coverage/167 "2019-11-07T20:19:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem\_Budak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/adem_budak/32/144_2.png) [@Adem\_Budak](https://discourse.cmake.org/u/Adem_Budak)\
**Post date:** [November 7, 2019, 8:19pm UTC](https://discourse.cmake.org/t/guideline-for-code-coverage/167/1 "2019-11-07T20:19:03Z")

</div>

Hello everyone,

After jumping around the some CMake documentation, Github sample project and Stackoverflow, I think it’s best to ask here

for correct usage of the CMake to get coverage report using GCov and Lcov. Any pointer (that not dangling) or advice is really

appreciated.

Thanks in advance

Adem.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [November 8, 2019, 4:02pm UTC](https://discourse.cmake.org/t/guideline-for-code-coverage/167/2 "2019-11-08T16:02:24Z")

</div>

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](https://gitlab.kitware.com/cmake/cmake/issues/19942) to propose the addition of a guide to the documentation.

---

<div class="post-metadata">

**Author:** ![Adem\_Budak](https://discourse.cmake.org/user_avatar/discourse.cmake.org/adem_budak/32/144_2.png) [@Adem\_Budak](https://discourse.cmake.org/u/Adem_Budak)\
**Post date:** [November 9, 2019, 12:57am UTC](https://discourse.cmake.org/t/guideline-for-code-coverage/167/3 "2019-11-09T00:57:12Z")

</div>

I hope this issue to be resolved, thanks for the answer.

---

<div class="post-metadata">

**Author:** ![a\_toldaiev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/a_toldaiev/32/5496_2.png) [@a\_toldaiev](https://discourse.cmake.org/u/a_toldaiev)\
**Post date:** [November 16, 2025, 10:11pm UTC](https://discourse.cmake.org/t/guideline-for-code-coverage/167/4 "2025-11-16T22:11:48Z")

</div>

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:

```auto
# MyCustomCTestJob.cmake
# Re-use CDash server config
include(${CTEST_SCRIPT_DIRECTORY}/CTestConfig.cmake)

site_name(CTEST_SITE)
set(CTEST_BUILD_NAME ${CMAKE_HOST_SYSTEM_NAME})
set(CTEST_SOURCE_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}")
set(CTEST_BINARY_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}/ctest_build")
set(CTEST_CMAKE_GENERATOR Ninja)
set(CTEST_CONFIGURATION_TYPE RelWithDebInfo)

ctest_start(Experimental)
ctest_configure(
  OPTIONS "-DCMAKE_BUILD_TYPE=Coverage"
)
ctest_build()
ctest_test()

find_program(GCOV NAMES gcov)
set(CTEST_COVERAGE_COMMAND "${GCOV}")
ctest_coverage()

# to submit to CDash:
#file(READ .mycdash_token _auth_token)
#string(STRIP ${_auth_token} _auth_token)                                                                                                                                                                   
#ctest_submit(
# HTTPHEADER "Authorization: Bearer ${_auth_token}"
#)

```

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”](https://crascit.com/professional-cmake/) in the section “33.2.1. gcov-based Coverage”:

```auto
if(CMAKE_BUILD_TYPE STREQUAL "Coverage" AND PROJECT_IS_TOP_LEVEL AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage")
  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    string(APPEND CMAKE_CXX_FLAGS_COVERAGE " -fprofile-abs-path")
  endif()
  set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage")
  set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "--coverage")
  set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "--coverage")
endif()

```
