In this section:
C/C++test's multi-metric coverage analysis allows you to monitor code coverage when executing a standalone application or library outside C/C++test.
C/C++test ships with a standalone coverage package that consists of the following components:
<INSTALL_DIR>/bin/cpptestcc
- A coverage tool that integrates into your build process to instrument your application to collect raw coverage data.<INSTALL_DIR>/bin/engine/coverage/runtime
- A coverage runtime library that needs to be integrated with the instrumented application.Collecting coverage with cpptestcc
involves three phases:
cpptestcc
tool into your build.cpptestcc
Add the path to <INSTALL_DIR>/bin
the to the PATH
system variable to enable execution of the cpptestcc
tool.
cpptestcc
executable as a prefix to the compiler command using --
as a separator. For example:Original compilation command line
cc -I app/includes -D defines -c source.cpp |
Updated compilation command line
cpptestcc -compiler gcc_7 -line-coverage -- cc -I app/includes -D defines -c source.cpp |
At a minimum, the cpptestcc tool requires the following parameters to be configured on the command line:- a compiler identifier: - a coverage metric (for example, See Command Line Reference for cpptestcc for information about other options. |
Original command line
lxx -L app/lib app/source.o somelib.lib -o app.exe |
Updated command line
lxx -L app/lib app/source.o somelib.lib [INSTALL_DIR]/bin/engine/coverage/runtime/lib/cpptest.lib -o app.exe |
If the coverage runtime library is linked as a shared (dynamic-load) library, you must ensure that it can be loaded when the instrumented application is started. This typically requires adding |
C/C++test provides the pre-built coverage runtime library for native Windows and Linux applications. For cross-platform and embedded testing, the runtime library needs to built from sources that are available in |
cpptestcc
creates the .cpptest/cpptestcc
folder where important coverage-related data ("coverage maps") are stored. By default, the folder is located in the working directory of the current compilation. You can change the default location using the -workspace <path>
option; see Command Line Reference for cpptestcc for details. cpptest_results.clog
file.The Load Application Coverage test configuration assumes that both .cpptest/cpptestcc
folder and cpptest_results.clog
file are stored in the default location. To customize the location, configure the following execution details in the test configuration (Execution> General> Execution details):
- Coverage map files root location - default: ${project_loc}/.cpptest/cpptestcc
- Coverage log files - default: ${project_loc}/*.clog
By default, the Load Application Coverage test configuration tries to load information about all the supported coverage metrics. To customize the list of supported coverage metricts, go to Execution> General> Execution details> Instrumentation Mode> Instrumentation features> C/C++ Code Coverage metrics and select the metrics you want to report.
We recommend that you keep consistency with the metrics enabled for the cpptestcc
tool in the compilation command line.
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 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.
Support for CMake integration includes:
<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.<CPPTEST_INSTALL_DIR>/integration/cmake/cpptest.templates/*
– A set of C/C++test templates for generating C/C++test (Eclipse) projects.CPPTEST_COVERAGE=ON
and CPPTEST_HOME=<CPPTEST_INSTALL_DIR>
options – The options provided by the extension for activating the extension when building the application with CMake.cpptest_coverage_report
target – The target provided by the extension for generating the coverage report using the .clog file as an input.In addition, the <CPPTEST_INSTALL_DIR>/examples/Timer
directory includes an example project to demonstrate collecting code coverage for a CMake project.
Additional prerequisites:
c:/folder/source.cpp
or c:\folder\source.cpp
). Unix-style paths (for example, /c/folder/source.cpp
) are not supported on Windows.<CPPTEST_INSTALL_DIR>/integration/cmake/cpptest-coverage.cmake
and <CPPTEST_INSTALL_DIR>/integration/cmake/cpptest.templates/*
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 the CPPTEST_COMPILER_ID
option matches your compiler. See Customizing the Coverage Extension for CMake for available options.
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 and CPPTEST_HOME
options when configuring and building your CMake project:
> cmake -DCPPTEST_COVERAGE=ON -DCPPTEST_HOME=<CPPTEST_INSTALL_DIR> .. |
By default, C/C++test's coverage data files will be created in <CMAKE_SOURCE_DIR>/../cpptest-coverage/<CMAKE_PROJECT_NAME>/.cpptest
.
<CMAKE_SOURCE_DIR>/../cpptest-coverage/<CMAKE_PROJECT_NAME>/<CMAKE_PROJECT_NAME>.clog
.Run the cpptest_coverage_report
helper target to generate the coverage report:
> make cpptest_coverage_report |
The C/C++test target will create:
- a workspace you can open directly in the IDE: <CMAKE_SOURCE_DIR>/../cpptest-coverage
- a C/C++test (Eclipse) project in that workspace: <CMAKE_SOURCE_DIR>/../cpptest-coverage/<CMAKE_PROJECT_NAME>
- the coverage report in the reports directory: <CMAKE_SOURCE_DIR>/../cpptest-coverage/<CMAKE_PROJECT_NAME>/reports
We recommend generating the coverage report using the cpptest_coverage_report
target. Alternatively, you can use C/C++test with the Load Application Coverage test configuration to generate report from the coverage data collected in <CMAKE_SOURCE_DIR>/../cpptest-coverage/<CMAKE_PROJECT_NAME>
. See Importing the Coverage Data for details.
To customize collecting coverage with the C/C++test extension, open the cpptest-coverage.cmake
file you copied to your CMake project and modify the C/C++test options.
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 tool where C/C++test's coverage data files are stored by default. | <CMAKE_SOURCE_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 tool. |
In addition, you may want to review:
cpptest_coverage_report
target that specifies the parameters for cpptestcli
for generating the coverage report.# Build C/C++test coverage runtime library
section, which includes configuration for the C/C++test’s runtime library (the library is automatically built by the coverage extension).CPPTEST_LINKER_FLAGS
option, which defines how the C/C++test coverage runtime will be linked.This section demonstrates how to use C/C++test's coverage extension for CMake to collect coverage data for the example project located in the <CPPTEST_INSTALL_DIR>/examples/Timer
directory. The Timer project is configured to use the cpptest-coverage.cmake
extension shipped in <CPPTEST_INSTALL_DIR>/integration/cmake
.
To collect coverage for the example project:
<CPPTEST_INSTALL_DIR>/integration/cmake/cpptest-coverage.cmake
and modify the default value of the CPPTEST_COMPILER_ID
to match your compiler.Build the example project:
> cd <CPPTEST_INSTALL_DIR>/examples/Timer > mkdir build > cd build > cmake -DCPPTEST_COVERAGE=ON -DCPPTEST_HOME=<CPPTEST_INSTALL_DIR> .. > make |
Run the application:
> ./timer |
Generate the coverage report:
> make cpptest_coverage_report |
The report will be created in <CPPTEST_INSTALL_DIR>/examples/cpptest-coverage/Timer/reports
.
<CPPTEST_INSTALL_DIR>/examples/cpptest-coverage
workspace to review the coverage details in the IDE.cpptestcc
You can run the following command to print out the available options to the console:
cpptestcc -help
The following options are available:
-compiler <name|path>Specifies the name of the compiler configuration you want to use for code analysis and instrumentation. See Compilers for the list of supported compilers or use the Examples:
Configuration file format (see -psrc): -list-compilersPrints out the names of all supported compiler configurations. Configuration file format (see -psrc): -include <file|pattern> and -exclude <file|pattern>Includes into or excludes from the instrumentation scope all the file(s) that match the specified pattern. Final filtering is determined only after all include/exclude entries have been specified in the order of their specification. The following wildcards are supported:
To prevent shells from expanding
Configuration file format (see -psrc): Example 1:Sample project layout:
If your project has the above layout, the following command will exclude all the files in the
Example 2:Sample project layout:
If your project has the above layout, the following command will only instrument the header files (the source files will not be instrumented):
-ignore <pattern>Specifies the source files that will be ignored during processing. The files that match the specified pattern will be compiled, but they will not be parsed or instrumented.
You can use the The following wildcards are supported:
To prevent shells from expanding
Configuration file format (see -psrc): Example:
-line-coverageEnables collecting line coverage. Runtime coverage results are being written to the results log as the code is executed. This imposes some overhead on the tested code execution time, it but it allows you to ensure that that coverage data is collected even if the application crashes. Configuration file format (see -psrc): -optimized-line-coverageEnables collecting optimized line coverage. Runtime coverage results are stored in memory and then written to the results log either after the application finishes or on user request. This results in better performance, but results may be lost if the application crashes. Configuration file format (see -psrc): -function-coverageEnables collecting function coverage. Configuration file format (see -psrc): -optimized-function-coverageEnables collecting optimized function coverage. Configuration file format (see -psrc):
-statement-coverageEnables collecting statement coverage. Configuration file format (see -psrc):
-optimized-statement-coverageEnables collecting statement coverage. Configuration file format (see -psrc): -block-coverageEnables collecting block coverage. Configuration file format (see -psrc): -optimized-block-coverageEnables collecting optimized block coverage. Configuration file format (see -psrc): -path-coverageEnables collecting path coverage. Configuration file format (see -psrc): -decision-coverageEnables collecting decision coverage. Configuration file format (see -psrc): -optimized-decision-coverageEnables collecting optimized decision coverage. Configuration file format (see -psrc): -simple-condition-coverageEnables collecting simple condition coverage. Configuration file format (see -psrc): -optimized-simple-condition-coverageEnables collecting optimized simple condition coverage. Configuration file format (see -psrc): -mcdc-coverageEnables collecting MC/DC coverage. Configuration file format (see -psrc): -call-coverageEnables collecting call coverage. Configuration file format (see -psrc): -optimized-call-coverageEnables collecting optimized call coverage. Configuration file format (see -psrc): -coverage-early-initEnables initializing the coverage module at the beginning of the application entry point. Configuration file format (see -psrc): -coverage-auto-finalizationIf enabled, collecting coverage will be automatically finalized at the application exit. This option is enabled by default. Configuration file format (see -psrc): -optimized-coverage-corruption-detectionEnables corruption detection algorithms for optimized coverage metrics. Configuration file format (see -psrc): -template-coverageEnables collecting coverage for template classes and functions Configuration file format (see -psrc): -workspace <path>Specifies a custom directory where information about code structure will be stored during code analysis and instrumentation. The By default, the information is stored in the working directory of the current compilation. If your compilation uses more than one working directory, we recommend that you specify a custom directory to ensure that all coverage data is stored in the same location. Configuration file format (see -psrc):
|
The coverage runtime library is a collection of helper functions and services used by source code instrumentation to emit coverage information at application runtime. Instrumented applications cannot be linked without the library. The runtime library can be linked to the final testable binary in multiple ways depending on the tested project type.
In addition to providing basic services for instrumented code, the library is also used to adapt the code coverage solution to particular development environments, such as supporting non-standard transport for coverage results between tested embedded device and development host.
C/C++test ships with pre-built versions of the runtime library, which are suitable for use on the same platform on which C/C++Test is installed. In most cases, collecting code coverage information from natively developed applications can use pre-built versions of the runtime library.
All users developing cross-platform applications will need to prepare a custom build of the coverage runtime library using a suitable cross compiler and possibly linker. Source code of the code coverage runtime library is shipped with C/C++test.
The process of preparing the coverage runtime library custom build is typically limited to the compilation of coverage runtime library source code. In some situations, you may need to install some fragments of source code to adapt code coverage to a particular development platform. This process is described in the following sections.
The following binary files are included with the C/C++test:
Windows (x86 and x86-64)
File | Description |
---|---|
<INSTALL_DIR>/bin/engine/coverage/runtime/lib/cpptest.a | 32-bit import library to be used with Cygwin GNU GCC compilers. To be added to linking command line. |
<INSTALL_DIR>/bin/engine/coverage/runtime/lib/cpptest64.a | 64-bit import library to be used with Cygwin GNU GCC compilers. To be added to linking command line. |
<INSTALL_DIR>/bin/engine/coverage/runtime/lib/cpptest.lib | 32-bit import library to be used with Microsoft Visual C++ compilers. To be added to linking command line. |
<INSTALL_DIR>/bin/engine/coverage/runtime/lib/cpptest64.lib | 64-bit import library to be used with Microsoft Visual C++ compilers. To be added to linking command line. |
<INSTALL_DIR>/bin/engine/coverage/runtime/bin/cpptest.dll | 32-bit dynamic-link library. <INSTALL_DIR> /bin/engine/coverage/runtime/bin should be added to PATH environmental variable. |
<INSTALL_DIR>/bin/engine/coverage/runtime/bin/cpptest64.dll | 64-bit dynamic-link library. <INSTALL_DIR> /bin/engine/coverage/runtime/bin should be added to PATH environmental variable. |
Linux (x86 and x86-64)
File | Description |
---|---|
<INSTALL_DIR>/bin/engine/coverage/runtime/lib/libcpptest.so | 32-bit shared library. To be added linking command line. <INSTALL_DIR> /bin/engine/coverage/runtime/lib should be added to LD_LIBRARY_PATH |
<INSTALL_DIR>/bin/engine/coverage/runtime/lib/libcpptest64.so | 64 bit shared library. To be added linking command line. <INSTALL_DIR>/bin/engine/coverage/runtime/lib should be added to LD_LIBRARY_PATH |
If you need to use the runtime library in a form not provided as an out-of-the-box solution, prepare a custom build of the coverage runtime library that matches specific development environment requirements. For more details, see Customizing the Runtime Library.
Integrating the coverage runtime library with a tested application linking process usually requires modifying the linker command line and, in some cases, the execution environment. This section describes how to modify the linking process when using the pre-built versions shipped with C/C++test.
Static library for Windows Cygwin GNU GCC compilers:
Dynamic-link library for Microsoft Visual C++ compilers:
Modify the build scripts so that the coverage runtime library is specified somewhere in the linker command line - preferably after all object files. For example:
$(LXX) $(PRODUCT_OBJ) $(OFLAG_EXE)$(PROJ_EXECUTABLE) $(LXXFLAGS) $(SYSLIB) $(EXECUTABLE_LIB_LXX_OPTS) <INSTALL_DIR>/bin/engine/coverage/runtime/lib/cpptest.lib |
Ensure that the path to the lib
directory is added to the PATH
environment variable so that the library can be located when the tested program is started. You may also consider copying cpptest.dll
(or cpptest64.dll
) file to the same directory as your executable file or to another location that is scanned for dynamic-link libraries during tested application startup.
Shared library for Linux GNU GCC compilers:
Modify the build scripts so that the coverage runtime library is specified somewhere in the linker command line - preferably after all object files. For example:
$(LXX) $(PRODUCT_OBJ) $(OFLAG_EXE)$(PROJ_EXECUTABLE) $(LXXFLAGS) $(SYSLIB) $(EXECUTABLE_LIB_LXX_OPTS) -L <INSTALL_DIR>/bin/engine/coverage/runtime/lib -lcpptest |
Note that the -L
and -lcpptest
options are added.
lib
directory is added to the LD_LIBRARY_PATH
environmental variable to allow the tested executable to find the path to the shared library.You may need to customize the runtime library as a result of the following conditions:
The runtime library source code is shipped with C/C++test in the [INSTALL_DIR]/bin/engine/coverage/runtime
directory. The following table describes the structure:
Component | Description |
---|---|
include | Directory that contains the library include files. |
src | Directory that contains the library source code. |
Makefile | Basic Makefile provided for building the runtime library. |
target | Directory that contains a set of Makefile include files with compiler-specific options for preparing runtime library builds for the most popular development environments. |
channel | Directory that contains a set of Makefile include files with configuration for supported communication channels. |
The runtime library supports data collection through various communication channels. The communication channel used depends on the development environment. In most cases, storing results in a file or files is appropriate, but in other TCP/IP sockets or RS232 transport may be required. Specific communication channels can be enabled by setting the value to a dedicated macro during cpptest.c
library source file compilation. Add -D<MACRO>
to the compilation command line to set the value. The following table provides the full list of communication channel control macros:
Channel | Description |
---|---|
CPPTEST_NULL_COMMUNICATION | Empty implementation. If enabled no results will be sent. Suitable for initial test builds and debugging. |
CPPTEST_FILE_COMMUNICATION | File-based implementation. ANSI C File I/O interface is used. If enabled, results will be written to a local drive file. The following additional configuration macros are also provided:
|
CPPTEST_SPLIT_FILE_COMMUNICATION | File-based implementation. ANSI C File I/O interface is used. If enabled, results will be written into a series of local drive files. You can configure this channel with the following macros:
Ensure that all the files in the series are placed in the same directory.
|
CPPTEST_UNIX_SOCKET_COMMUNICATION | TCP/IP socket based implementation. POSIX API is used. If enabled results are sent to the specified TCP/IP port. The following additional configuration macros are provided:
|
CPPTEST_WIN_SOCKET_COMMUNICATION | As above, Windows API is used. |
CPPTEST_UNIX_SOCKET_UDP_COMMUNICATION | As above, UDP based implementation. |
CPPTEST_RS232_UNIX_COMMUNICATION | RS232 based implementation. POSIX API is used. If enabled then results are sent via the specified RS232 system device. The following additional configuration macros are provided:
|
CPPTEST_RS232_WIN_COMMUNICATION | As above. Windows API is used. |
CPPTEST_RS232_STM32F103ZE_COMMUNICATION | STM32F103x USART based implementation. STM Cortex library interface is used (ST/STM32F10x/stm32f10x.h header file is required) |
CPPTEST_HEW_SIMIO_COMMUNICATION | Renesas HEW simulator specific implementation. |
CPPTEST_LAUTERBACH_FDX_COMMUNICATION | Lauterbach TRACE32 based implementation (FDX used) |
CPPTEST_ITM_COMMUNICATION | ARM CoreSight ITM unit based communication. Requires CMSIS header files. |
CPPTEST_CUSTOM_COMMUNICATION | Enables empty template for custom implementation |
If the coverage runtime library is being built with the provided Makefile, then one of the make configuration files provided in the [INSTALL_DIR]/bin/engine/coverage/runtime/channel
directory can be used.
If none of the communication channel implementations fit into your development environment, then a custom implementation can be provided. The following instructions describe how to customize the runtime library so that it uses a custom implementation of a communication channel:
Make a copy of [INSTALL_DIR]/bin/engine/coverage/runtime/src/cpptest.c
and open the file for editing.
Locate section 1.13 "Custom Communication Implementation".
The custom communication implementation section contains empty templates for four different methods:
Function | Description |
---|---|
void cpptestInitializeStream(void) | This function is responsible initializing the communication channel, for example creating and connecting to a socket or the initialization of UART device. |
void cpptestFinalizeStream(void) | This function is responsible for finalizing the communication channel. For example, it may be responsible for closing TCP/IP socket. |
| This function is responsible for sending size bytes from a data buffer. |
void cpptestFlushData(void) | This function is responsible for flushing the data. Its meaning depends on the particular transport type. It may have a limited application in some implementations. In this case, it should be left empty. |
cpptest.c
with the following macro definition added to compilation command line:-DCPPTEST_CUSTOM_COMMUNICATION
The runtime library contains support for multithreaded applications. POSIX, Windows, and VxWorks APIs are supported. You can enable support for a specific multithreading API by adding -D<MACRO>
to the compilation command line during cpptest.c
compilation. The following table describes the full list of multithreading API support control macros:
Macro | Description |
---|---|
CPPTEST_NO_THREADS | Empty implementation. Coverage runtime is not prepared to be used together with multithreaded applications |
CPPTEST_WINDOWS_THREADS | Windows multithreading API implementation |
CPPTEST_UNIX_THREADS | POSIX multithreading API implementation |
CPPTEST_VXWORKS_THREADS | VxWorks multithreading API implementation |
If you are using C/C++test's coverage engine with multithreaded applications that do not use a supported multithreading API, you can customize the runtime library to work with your multithreading API. There are following steps required:
Make a copy of [INSTALL_DIR]/bin/engine/coverage/runtime/src/cpptest.c
and open the file for editing.
Locate the section 2.5 "Custom Multithreading Implementation".
Custom multithreading implementation section contains empty templates for two different methods:
Function | Description |
---|---|
static int cpptestLock(void) | This function ensures synchronized operations inside the coverage tool runtime library. If a thread locks access to the runtime library service, it means an atomic operation is in progress and no other thread can use runtime library services. Once the lock is released other threads can use runtime library services |
static int cpptestUnlock(void) | Releases the lock on runtime library services. |
Provide the implementation for the methods that matches your environment requirements.
Compile cpptest.c
with the following macro added to compilation command line:-DCPPTEST_CUSTOM_THREADS
If the generated object file is insufficient, you can process the file even further to meet your needs (for example, to create a shared library).
C/C++test ships with a simple Makefile (see Library Source Code Structure) which simplifies the process of building the runtime library. In many instances, however, the make file provided will not be required because the source code is already optimized for the building process. The only step that is always required is the compilation of the main cpptest.c
source file. Any additional processing of the produced object file will depend on the particular development environment and its requirements, such as providing the runtime library as a shared library.
Copy <INSTALL_DIR>/bin
/engine/coverage/runtime
to a local director.
Invoke the following command line to create a build
subdirectory that contains a single object cpptest.<OBJ_EXT>
, which can be used to link with the instrumented application.
make TARGET_CFG=<target config file name> CHANNEL_FILE=<channel config file name> |
Your command line may resemble the following:
make TARGET_CFG=gcc-static.mk CHANNEL_FILE=channel/unix-socket.mk |
Alternatively, you can provide the channel type:
make TARGET_CFG=gcc-static.mk CHANNEL_TYPE=unix-socket |
To set up a user build of coverage runtime library perform the following steps:
Copy the cpptest.c file from <INSTALL_DIR>
/bin/engine/coverage/runtime/src/cpptest.c
to your preferred location.
Modify the compilation flags to contain the compiler include a flag (typically -I
) with the following value:-I <INSTALL_DIR>/bin/engine/coverage/runtime/include
-D)
, for example:-DCPPTEST_FILE_COMMUNICATION -DCPPTEST_NO_THREADS