Coverage analysis
coverage
This module defines functions to help get the code coeverage reports.
enable_coverage() adds required compile and link options to the target,
generate_coverage_report() adds dependency on target to generate the
coverage target, run_coverage() triggers code coverage generation.
Coverage reports use gcovr (REQUIRED) and lcov (OPTIONAL) to process the coverage data and generate reports.
- enable_coverage
Automatically add compile and link options to the target:
enable_coverage(TARGET target)
The option is:
TARGET targetSpecifies target, which must be a known CMake library or executable target.
- generate_coverage_report
Automatically add dependency to the target:
generate_coverage_report(TARGET target)
The option is:
TARGET targetSpecifies target, which must be a known CMake library or executable target. When
run_coverage()is called, the targets that had been passed togenerate_coverage_report()are triggered to ensure coverage data is available.
- run_coverage
Automatically trigger targets generating coverage data, then the tools for generating the reports.
The reports are created in
${data_base_path}/coverage-reports. If not set, ${data_base_path} will be set as ${CMAKE_BINARY_DIR}For gcovr, report is named
gcov-results.xml. If gcovr version is at least 4.2, a HTML report is also generated in${data_base_path}/coverage-reports/gcovr, with main fileindex.html.For lcov, report is named
lcov.info. A HTML report is also generated in${data_base_path}/coverage-reports/lcov, with main fileindex.html.
Usage example:
include(coverage) add_library(FooLib FooLib.cc) enable_coverage(TARGET FooLib) add_executable(FooExe FooExe.cc) enable_coverage(TARGET FooExe) add_executable(FooTest FooTest.cc) generate_coverage_report(TARGET FooTest)