Skip to content

Instantly share code, notes, and snippets.

@madebr
Last active August 19, 2023 20:10
Show Gist options
  • Save madebr/07ebd0de59e789cd819643b58db25df9 to your computer and use it in GitHub Desktop.
Save madebr/07ebd0de59e789cd819643b58db25df9 to your computer and use it in GitHub Desktop.
Simple CTest coverage project + script
cmake_minimum_required(VERSION 3.9)
project(cmake_coverage)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/main.c" "int main() {return 0;}")
add_executable(main
main.c
)
target_compile_options(main
PUBLIC
"-O0"
"-fprofile-arcs"
"-ftest-coverage"
)
target_link_libraries(main
gcov
)
include(CTest)
enable_testing()
add_test(NAME test
COMMAND main
)
set(CTEST_PROJECT_NAME "Simple ctest_coverage project")
# This command succeeds
$ ls && mkdir build && cd build && cmake .. && make && make test && make ExperimentalCoverage && cd ..
CMakeLists.txt CTestConfig.cmake test.ctest
-- The C compiler identification is GNU 7.2.1
-- The CXX compiler identification is GNU 7.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/maarten/programming/minumum_examples/cmake_coverage/build
Scanning dependencies of target main
[ 50%] Building C object CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
[100%] Built target main
Running tests...
Test project /home/maarten/programming/minumum_examples/cmake_coverage/build
Start 1: test
1/1 Test #1: test ............................. Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 sec
Scanning dependencies of target ExperimentalCoverage
Site: maarten-acer
Build name: Linux-c++
Create new tag: 20171028-0543 - Experimental
Performing coverage
Processing coverage (each . represents one file):
.
Accumulating results (each . represents one file):
.
Covered LOC: 1
Not covered LOC: 0
Total LOC: 1
Percentage Coverage: 100.00%
Built target ExperimentalCoverage
# This command fails
$ ctest -S test.ctest
Each . represents 1024 bytes of output
. Size of output: 0K
Each symbol represents 1024 bytes of output.
'!' represents an error and '*' a warning.
. Size of output: 0K
Cannot find file: /home/maarten/programming/minumum_examples/cmake_coverage/build/ctest/Testing/CoverageInfo/main.c
# Why is that?
set(CTEST_SOURCE_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}")
set(CTEST_BINARY_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}/build/ctest")
ctest_empty_binary_directory("${CTEST_BINARY_DIRECTORY}")
set(CTEST_SITE "${CMAKE_SYSTEM_NAME}")
set(CTEST_BUILD_NAME "experimental build")
set(CTEST_CMAKE_GENERATOR "Ninja")
set(CTEST_COVERAGE_COMMAND "gcov")
ctest_start("Experimental")
ctest_configure()
ctest_build()
ctest_test()
ctest_coverage()
@madebr
Copy link
Author

madebr commented Nov 18, 2021

Is this still a problem?

@Jihadist
Copy link

Yep, i couldn't find solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment