test: add code coverage report (#3312)

This commit is contained in:
Erdem MEYDANLI
2020-08-18 16:34:45 +02:00
committed by GitHub
parent ffd03582c9
commit 9e5c3f78af
3 changed files with 23 additions and 5 deletions

1
.gitignore vendored
View File

@@ -62,6 +62,7 @@ nbproject/
.idea .idea
cmake-build-debug/* cmake-build-debug/*
cmake-build-debug-coverage/* cmake-build-debug-coverage/*
coverage-report/
# #
# Eclipse # Eclipse

View File

@@ -140,12 +140,17 @@ add_subdirectory(src)
CU_RUN_HOOK("AFTER_SRC_LOAD") CU_RUN_HOOK("AFTER_SRC_LOAD")
if( UNIT_TESTS ) if( UNIT_TESTS )
# we use this to get code coverage # we use these flags to get code coverage
if(CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Clang) set(UNIT_TEST_CXX_FLAGS "-fprofile-arcs -ftest-coverage -fno-inline")
message("Unit tests code coverage: enabling -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") # enable additional flags for GCC.
if ( CMAKE_CXX_COMPILER_ID MATCHES GNU )
set(UNIT_TEST_CXX_FLAGS "${UNIT_TEST_CXX_FLAGS} -fno-inline-small-functions -fno-default-inline")
endif() endif()
message("Unit tests code coverage: enabling ${UNIT_TEST_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${UNIT_TEST_CXX_FLAGS}")
include(src/cmake/googletest.cmake) include(src/cmake/googletest.cmake)
fetch_googletest( fetch_googletest(
${PROJECT_SOURCE_DIR}/src/cmake ${PROJECT_SOURCE_DIR}/src/cmake
@@ -154,4 +159,16 @@ if( UNIT_TESTS )
enable_testing() enable_testing()
add_subdirectory(src/test) add_subdirectory(src/test)
add_custom_target(coverage DEPENDS coverage_command)
add_custom_command(OUTPUT coverage_command
# Run unit tests.
COMMAND ctest
# Run the graphical front-end for code coverage.
COMMAND lcov --directory src --capture --output-file coverage.info
COMMAND lcov --remove coverage.info '/usr/*' '${CMAKE_BINARY_DIR}/googletest/*' '${CMAKE_CURRENT_SOURCE_DIR}/src/test/*' --output-file coverage.info
COMMAND genhtml -o ${CMAKE_CURRENT_SOURCE_DIR}/coverage-report coverage.info
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)
endif() endif()

View File

@@ -19,5 +19,5 @@ add_test(
NAME NAME
unit unit
COMMAND COMMAND
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/unit_tests ${CMAKE_BINARY_DIR}/src/test/unit_tests
) )