...
C/C++test ships with an extension for CMake that allows you to integrate C/C++test's code coverage analysis directly into your CMake project. The extension automatically modifies your compiler/linker command lines to use the cpptestcc
coverage utility tool when building your project. As a result, after you run the instrumented application or execute functional or unit tests, a C/C++test coverage log file (.clog) is created. The coverage log file can then be used to generate a complete code coverage report.
...
- The
<CPPTEST_INSTALL_DIR>/integration/cmake/cpptest-coverage.cmake
extension – The C/C++test extension file for CMake you need to add to your CMakeFiles.txt build file. - The
CPPTEST_COVERAGE=ON
andCPPTEST_HOME=<PATH<CPPTEST_TO_CPPTEST_INSTALL_DIR>
options – The options provided by the extension for activating the extension when building the application with CMake. - The
cpptest_coverage_report
target for Make – The target provided by the extension for enabling Make to generate generating the coverage report using the .clog file as an input.
...
Copy
<CPPTEST_INSTALL_DIR>/integration/cmake/cpptest-coverage.cmake
to your CMake project.Review the coverage configuration details in the
cpptest-coverage.cmake
file and update the options if needed. At a minimum, you must ensure that the compiler configuration specified with theCPPTEST_COMPILER_ID
option matches your compiler. See Customizing the Coverage Extension for CMake for available options.Include the
cpptest-coverage.cmake
extension to your main CMakeLists.txt build file. The extension must precede all build target definitions to ensure that the compiler/linker command lines are automatically modified.Activate the extension with the
CPPTEST_COVERAGE
andCOVERAGE and CPPTEST_HOME
options when configuring and building your CMake project:Code Block language text > cmake -DCPPTEST_COVERAGE=ON -DCPPTEST_HOME=<PATH<CPPTEST_TO_CPPTEST_INSTALL_DIR> ..
By default, C/C++test's coverage data files will be created in
<CMAKE_BINARY_DIR>/cpptest-coverage/<CMAKE_PROJECT_NAME>/.cpptest
.Run your application or execute your functional or unit tests.
By default, the C/C++test coverage log file (.clog) will be created in<CMAKE_BINARY_DIR>/cpptest-coverage/<CMAKE_PROJECT_NAME>/<CMAKE_PROJECT_NAME>.clog
.Run Make with the
cpptest_coverage_report
helper target to generate the coverage report:Code Block language text > make cpptest_coverage_report
The coverage report will be generated in the following location:
<CMAKE_BINARY_DIR>/cpptest-coverage/<CMAKE_PROJECT_NAME>/reports
.We recommend generating the coverage report using the Make
cpptest_coverage_report
target. Alternatively, you can usecpptestcli
with the Coverage test configuration and the .clog file as an input:Code Block language text > cd <CMAKE_BINARY_DIR>/cpptest-coverage/<CMAKE_PROJECT_NAME> > cpptestcli -config "builtin://Coverage" -input <CMAKE_PROJECT_NAME>.clog
...
Anchor | ||||
---|---|---|---|---|
|
...
Option Name | Description | Default |
---|---|---|
CPPTEST_COMPILER_ID | Specifies the compiler configuration that matches your compiler. | gcc_9-64 |
CPPTEST_COVERAGE_TYPE_FLAGS | Specifies the coverage type. See Command Line Reference for cpptestcc for available options. | -optimized-line-coverage |
CPPTEST_COVERAGE_WORKSPACE | Specifies the path to the workspace for the coverage utility tool where C/C++test's coverage data files are stored by default. | <CMAKE_BINARY_DIR>/cpptest-coverage/<CMAKE_PROJECT_NAME> |
CPPTEST_COVERAGE_LOG_FILE | Specifies the name and location of the C/C++test coverage log file (.clog). Important: You must ensure the coverage log directory exists before running the instrumented application. | <CPPTEST_COVERAGE_WORKSPACE>/<CMAKE_PROJECT_NAME>.clog |
CPPTEST_CPPTESTCC_OPTS | Specifies the options for the cpptestcc coverage utilitytool. |
In addition, you may want to review:
- the definition of
cpptest_coverage_report
target that specifies the parameters forcpptestcli
to generate for generating the coverage report. - the
# Build C/C++test coverage runtime library
section and theCPPTEST_LINKER_FLAGS
option, which include information about includes configuration for the C/C++test's coverage test’s runtime library (the library located in<CPPTEST_INSTALL_DIR>/runtime
is automatically built by the coverage extension). - the
CPPTEST_LINKER_FLAGS
option, which defines how the C/C++test coverage runtime will be linked.
Example of Integrating the Coverage Extension with CMake
...