C/C++test CT can collect code coverage information when executing tests with the GoogleTest framework. For more information, see:

Avoid collecting code coverage from the GoogleTest source and header files. This may occur if the GoogleTest framework is being compiled as part of your build, or if GoogleTest is part of your source tree. See Fine-Tuning Code Instrumentation to learn how to configure the scope of coverage analysis and exclude GoogleTest sources from the analysis.

Example:

For an example integration, see <INSTALL_DIR>/examples/CovGoogleTest. This is a CMake-based project that uses GoogleTest to run unit tests and C/C++test CT to report code coverage. The key configuration components of this integration include:

a. Using the GoogleTest package provided with C/C++test CT in the FetchContent_Declare() function:

# FILE: <INSTALL_DIR>/examples/CovGoogleTest/CMakeLists.txt

...
set(GOOGLETEST_SUBPATH "googletest/googletest.zip")

if(DEFINED GOOGLETEST_URL)
# use the specified URL
elseif(CPPTEST_HOME)
set(GOOGLETEST_URL "${CPPTEST_HOME}/${GOOGLETEST_SUBPATH}")
elseif(DEFINED ENV{CPPTEST_HOME})
set(GOOGLETEST_URL "$ENV{CPPTEST_HOME}/${GOOGLETEST_SUBPATH}")
elseif (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../../${GOOGLETEST_SUBPATH}")
set(GOOGLETEST_URL "${CMAKE_CURRENT_LIST_DIR}/../../${GOOGLETEST_SUBPATH}")
else()
message(FATAL_ERROR "Unable to locate GoogleTest package - please set CPPTEST_HOME (or GOOGLETEST_URL).")
endif()

include(FetchContent)
FetchContent_Declare(
  googletest
  URL "${GOOGLETEST_URL}"
)
...

b. Enabling code coverage analysis by including the C/C++test CT coverage extension for CMake (cpptest-coverage.cmake):

# FILE: <INSTALL_DIR>/examples/CovGoogleTest/CMakeLists.txt

...
if(CPPTEST_COVERAGE)
  include(cpptest-coverage.cmake)
endif()
...

c. Fine-tuning the scope of the coverage analysis to exclude or ignore sources from the GoogleTest framework and the GoogleTest test cases (using the -include, -exclude, or -ignore options):

# FILE: <INSTALL_DIR>/examples/CovGoogleTest/cpptest-coverage.cmake

...
# Configure cpptestcc command line
set(CPPTEST_CPPTESTCC ${CPPTEST_HOME_DIR}/bin/cpptestcc)
set(CPPTEST_CPPTESTCC_OPTS
    -workspace "${CPPTEST_COVERAGE_WORKSPACE}"
    -compiler ${CPPTEST_COMPILER_ID}
    ${CPPTEST_COVERAGE_TYPE_INSTRUMENTATION}
    -exclude "regex:*"
    -include "regex:${CPPTEST_SOURCE_DIR}/*"
    -exclude "regex:${CPPTEST_BINARY_DIR}/*"
    -ignore "regex:*_test.cpp"
    -ignore "regex:${CPPTEST_BINARY_DIR}/*")
...


  • No labels