简体   繁体   English

库单元测试的覆盖率数据错误

[英]Wrong coverage data for unit tests of a library

I want to test a complex shared library (cxsc) with the help of cmake. 我想借助cmake测试一个复杂的共享库(cxsc)。 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. 经过大量的试验和错误,我设法创建了html覆盖率报告,但是使用boost测试框架测试的行仍然是红色的。

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: 创建覆盖率报告的cmake部分:

# 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): 测试二进制文件的Makefile(我认为这是错误所在):

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 cmake -DCMAKE_BUILD_TYPE=Coverage .. build_coverage目录是使用target.dir目录和* .gcno文件创建的
  2. cd tests && make Test executable is created and linked against the shared library in the build_coverage directory ( Maybe here is my mistake ) cd tests && make Test可执行文件被创建并链接到build_coverage目录中的共享库( 也许这是我的错误
  3. make coverage Coverage data is collected and tests are executed make coverage范围收集make coverage范围数据并执行测试

Edit: 编辑:

To clarify my problem, there are some lines covered but only global consts or one helper function which is used pretty often. 为了澄清我的问题,这里介绍了一些内容,但是只介绍了全局const或一个经常使用的辅助函数。 But not the functions/methods I call in my tests. 但不是我在测试中调用的函数/方法。

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

  1. After cmake -DC... : A few *.cmake and *.make files cmake -DC... :一些* .cmake和* .make文件
  2. After make in build_coverage dir: *.gcno files build_coverage目录中make之后:* .gcno文件
  3. After lcov -c -i ... : Still *.gcno files lcov -c -i ... :仍* .gcno文件
  4. After running tests: *.gcda and *.gcno files 运行测试后:* .gcda和* .gcno文件
  5. After lcov -c ... : A lot of movement in the directory but still the same filenames lcov -c ... :目录中的移动很多,但文件名仍然相同

I don't see any gcov flags in your makefile. 我在您的makefile中看不到任何gcov标志。 Try this one for test_runner : 试试这个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" 注意:“-coverage”可以替代“ -fprofile-arcs -ftest-coverage”和“ -lgcov”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM