1

I want to test a complex shared library (cxsc) with the help of cmake. After a lot of trial and error i managed to create a html coverage report but the lines I test, with the boost testing framework, are still red.

This is the script I use to create the coverage report:

rm -fr build_coverage
mkdir build_coverage
cd build_coverage
cmake \
    -DCMAKE_BUILD_TYPE=Coverage \
    ..
make
cd ../tests
make clean
make
cd ../build_coverage
make cxsc_coverage

The cmake part where the coverage report gets created:

# Cleanup lcov
COMMAND ${LCOV_PATH} --zerocounters --directory ${CMAKE_CURRENT_SOURCE_DIR}/build_coverage/CMakeFiles/cxsc.dir/src
COMMAND ${LCOV_PATH} --capture --initial --no-external --directory ${CMAKE_CURRENT_SOURCE_DIR}/build_coverage/CMakeFiles/cxsc.dir/src --output-file ${_outputname}.before

# Run tests
COMMAND LD_LIBRARY_PATH=${CMAKE_CURRENT_SOURCE_DIR}/build_coverage ${_testrunner} ${ARGV3}

# Capturing lcov counters and generating report
COMMAND ${LCOV_PATH} --capture --no-checksum --no-external --directory ${CMAKE_CURRENT_SOURCE_DIR}/build_coverage/CMakeFiles/cxsc.dir/src --output-file ${_outputname}.after
COMMAND ${LCOV_PATH} --add-tracefile ${_outputname}.before --add-tracefile ${_outputname}.after --output-file ${_outputname}.info

COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info

Makefile of the test binary (I think this is where the error is):

g++ \
    -o test_runner \
    main.cpp \
    test_interval.cpp \
    -I./../src \
    -I./../src/rts \
    -I./../src/asm \
    -I./../src/fi_lib \
    -I./../build_coverage \
    -L./../build_coverage \
    -lcxsc \
    -Wall -Winline \
    -lboost_unit_test_framework

This is what happens:

  1. cmake -DCMAKE_BUILD_TYPE=Coverage .. build_coverage directory is created with a target.dir directory and *.gcno files in it
  2. cd tests && make Test executable is created and linked against the shared library in the build_coverage directory (Maybe here is my mistake)
  3. make coverage Coverage data is collected and tests are executed

Edit:

To clarify my problem, there are some lines covered but only global consts or one helper function which is used pretty often. But not the functions/methods I call in my tests.

Files in build_coverage/CMakeFiles/cxsc.dir/src/:

  1. After cmake -DC...: A few *.cmake and *.make files
  2. After make in build_coverage dir: *.gcno files
  3. After lcov -c -i ...: Still *.gcno files
  4. After running tests: *.gcda and *.gcno files
  5. After lcov -c ...: A lot of movement in the directory but still the same filenames
Tim
  • 929
  • 1
  • 14
  • 28
  • 1
    Sometimes templates confuses code-coverage. I don't know the circumstances which lead to this behavior. – usr1234567 Jan 27 '16 at 12:00
  • 1
    MIght be related: http://stackoverflow.com/questions/9666800/getting-useful-gcov-results-for-header-only-libraries – usr1234567 Jan 27 '16 at 12:38
  • There are some templates but not in the one class I want to test at the moment. – Tim Jan 28 '16 at 09:59

1 Answers1

0

I don't see any gcov flags in your makefile. Try this one for test_runner :

g++ \
   -Wall -Winline -fprofile-arcs -ftest-coverage \
   main.cpp \
   test_interval.cpp \
   -I./../src \
   -I./../src/rts \
   -I./../src/asm \
   -I./../src/fi_lib \
   -I./../build_coverage \
   -L./../build_coverage \
   -lcxsc \
   -lboost_unit_test_framework \
   -lgcov \
   -o test_runner

Your shared library need those flags too :

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov")

See this post with a similar problem.

NB : "--coverage" can substitute "-fprofile-arcs -ftest-coverage" and "-lgcov"

Community
  • 1
  • 1
cromod
  • 1,721
  • 13
  • 26
  • The Makefile is only for the test executable. The shared library itself is compiled with `-fprofile-arcs -ftest-coverage`. But I'm not sure if it is linked with -lgcov...I will check it out. Thanks! – Tim Jan 27 '16 at 19:41
  • Does your test execution generate *.gcda files ? – cromod Jan 27 '16 at 20:54
  • Did you try to add gcov flags in the makefile of your binary test ? You can remove unwanted files from your report with lcov --remove. – cromod Jan 28 '16 at 10:39
  • I already exclude anwanted directories/files, thanks for the tip. What flags do I have to specify in the makefile? I only found flags for the main exe/library, not for the test runner. – Tim Jan 28 '16 at 12:46