使用具有多个目标的 CodeCoverage.cmake 进行代码覆盖率分析

Posted

技术标签:

【中文标题】使用具有多个目标的 CodeCoverage.cmake 进行代码覆盖率分析【英文标题】:Code coverage analysis using CodeCoverage.cmake with multiple targets 【发布时间】:2019-02-14 18:11:52 【问题描述】:

我在我的项目中使用Boost Unit Test Framework 实现了单元测试,并组织成几个模块,即:

#define BOOST_TEST_MODULE Connection_test

#ifndef BOOST_TEST_DYN_LINK
#define BOOST_TEST_DYN_LINK
#endif

#ifndef BOOST_TEST_NO_MAIN
#define BOOST_TEST_NO_MAIN
#endif

#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>

#define BOOST_TEST_MODULE Connection_test


BOOST_AUTO_TEST_SUITE(Connection_test)

    BOOST_AUTO_TEST_CASE(Connection_construction__test) 

       ***
    

BOOST_AUTO_TEST_SUITE_END()

我将每个模块编译为单个可执行文件。

我想使用CodeCoverage.cmake 模块来运行代码覆盖率分析,但我遇到了问题。我应该通过SETUP_TARGET_FOR_COVERAGE_LCOV 指定一个测试可执行文件,但我没有一个。

有没有办法使用CodeCoverage.cmake 一次设置多个测试可执行文件?


编辑

我已将 add_test() 的测试添加到我的根 CMakeLists.txt 中,并像这样修改了我的覆盖目标

include(CTest)
add_test(NAME constant_neuron_test COMMAND constant_neuron_test)
add_test(NAME binary_neuron_test COMMAND binary_neuron_test)
add_test(NAME logistic_neuron_test COMMAND logistic_neuron_test)
add_test(NAME connectionFunctionGeneral_test COMMAND connectionFunctionGeneral_test)
add_test(NAME connection_Function_identity_test COMMAND connection_Function_identity_test)
add_test(NAME neural_network_test COMMAND neural_network_test)
add_test(NAME dataset_test COMMAND dataset_test)
add_test(NAME particle_swarm_test COMMAND particle_swarm_test)
add_test(NAME particle_test COMMAND particle_test)
add_test(NAME NeuralNetworkSum_test COMMAND NeuralNetworkSum_test)
add_test(NAME errorfunction_test COMMAND errorfunction_test)
add_test(NAME DESolver_test COMMAND DESolver_test)

include(CodeCoverage.cmake)
APPEND_COVERAGE_COMPILER_FLAGS()
SETUP_TARGET_FOR_COVERAGE_LCOV(
        NAME coverage                 # New target name
        EXECUTABLE ctest -C $ROOT_DIR/CTestTestfile.cmake # Executable in PROJECT_BINARY_DIR
        DEPENDENCIES $Boost_LIBRARIES         # Dependencies to build first
)

ctest 本身运行正确:

Test project /home/martin/4Neuro
      Start  1: constant_neuron_test
 1/12 Test  #1: constant_neuron_test ................   Passed    0.04 sec
      Start  2: binary_neuron_test
 2/12 Test  #2: binary_neuron_test ..................   Passed    0.04 sec
      Start  3: logistic_neuron_test
 3/12 Test  #3: logistic_neuron_test ................   Passed    0.05 sec
      Start  4: connectionFunctionGeneral_test
 4/12 Test  #4: connectionFunctionGeneral_test ......   Passed    0.04 sec
      Start  5: connection_Function_identity_test
 5/12 Test  #5: connection_Function_identity_test ...   Passed    0.04 sec
      Start  6: neural_network_test
 6/12 Test  #6: neural_network_test .................   Passed    0.04 sec
      Start  7: dataset_test
 7/12 Test  #7: dataset_test ........................   Passed    0.04 sec
      Start  8: particle_swarm_test
 8/12 Test  #8: particle_swarm_test .................   Passed    0.04 sec
      Start  9: particle_test
 9/12 Test  #9: particle_test .......................   Passed    0.04 sec
      Start 10: NeuralNetworkSum_test
10/12 Test #10: NeuralNetworkSum_test ...............   Passed    0.05 sec
      Start 11: errorfunction_test
11/12 Test #11: errorfunction_test ..................   Passed    0.04 sec
      Start 12: DESolver_test
12/12 Test #12: DESolver_test .......................   Passed    0.05 sec

100% tests passed, 0 tests failed out of 12

Total Test time (real) =   0.53 sec

但是当我尝试通过make coverage 创建我的覆盖率报告时,我收到了这个错误:

Processing code coverage counters and generating report.
cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov -directory . --zerocounters
Deleting all .da files in . and subdirectories
Done.
cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov -c -i -d . -o coverage.base
Capturing coverage data from .
Found gcov version: 7.3.0
Scanning . for .gcno files ...
geninfo: WARNING: no .gcno files found in . - skipping!
Finished .info-file creation
cd /home/martin/4Neuro/build && ctest -C /home/martin/4Neuro/CTestTestfile.cmake
Test project /home/martin/4Neuro/build
No tests were found!!!
cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov --directory . --capture --output-file coverage.info
Capturing coverage data from .
Found gcov version: 7.3.0
Scanning . for .gcda files ...
geninfo: WARNING: no .gcda files found in . - skipping!
Finished .info-file creation
cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov -a coverage.base -a coverage.info --output-file coverage.total
Combining tracefiles.
Reading tracefile coverage.base
lcov: ERROR: no valid records found in tracefile coverage.base
CMakeFiles/coverage.dir/build.make:62: recipe for target 'CMakeFiles/coverage' failed
make[3]: *** [CMakeFiles/coverage] Error 255
make[3]: Leaving directory '/home/martin/4Neuro'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/coverage.dir/all' failed
make[2]: *** [CMakeFiles/coverage.dir/all] Error 2
make[2]: Leaving directory '/home/martin/4Neuro'
CMakeFiles/Makefile2:77: recipe for target 'CMakeFiles/coverage.dir/rule' failed
make[1]: *** [CMakeFiles/coverage.dir/rule] Error 2
make[1]: Leaving directory '/home/martin/4Neuro'
Makefile:132: recipe for target 'coverage' failed
make: *** [coverage] Error 2

编辑 2

我已经像这样修改了我的 CMakeLists.txt

include(CTest)
enable_testing()
add_subdirectory($SRC_DIR $PROJECT_BINARY_DIR)

# Adding Unit-tests
add_test(NAME constant_neuron_test COMMAND constant_neuron_test)
add_test(NAME binary_neuron_test COMMAND binary_neuron_test)
add_test(NAME logistic_neuron_test COMMAND logistic_neuron_test)
add_test(NAME connectionFunctionGeneral_test COMMAND connectionFunctionGeneral_test)
add_test(NAME connection_Function_identity_test COMMAND connection_Function_identity_test)
add_test(NAME neural_network_test COMMAND neural_network_test)
add_test(NAME dataset_test COMMAND dataset_test)
add_test(NAME particle_swarm_test COMMAND particle_swarm_test)
add_test(NAME particle_test COMMAND particle_test)
add_test(NAME NeuralNetworkSum_test COMMAND NeuralNetworkSum_test)
add_test(NAME errorfunction_test COMMAND errorfunction_test)
add_test(NAME DESolver_test COMMAND DESolver_test)

include(CodeCoverage.cmake)
APPEND_COVERAGE_COMPILER_FLAGS()
SETUP_TARGET_FOR_COVERAGE_LCOV(
        NAME coverage                 # New target name
        EXECUTABLE ctest -j $n_cores # Executable in PROJECT_BINARY_DIR
        DEPENDENCIES
            constant_neuron_test
            binary_neuron_test
            logistic_neuron_test
            connectionFunctionGeneral_test
            connection_Function_identity_test
            neural_network_test
            dataset_test
            particle_swarm_test
            particle_test
            NeuralNetworkSum_test
            errorfunction_test
            DESolver_test         # Dependencies to build first
)
set(COVERAGE_EXCLUDES 'external_dependencies/*')

但不幸的是,错误仍然存​​在

cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov -c -i -d . -o coverage.base
Capturing coverage data from .
Found gcov version: 7.3.0
Scanning . for .gcno files ...
geninfo: WARNING: no .gcno files found in . - skipping!
Finished .info-file creation
cd /home/martin/4Neuro/build && ctest -j 3
Test project /home/martin/4Neuro/build
No tests were found!!!
cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov --directory . --capture --output-file coverage.info
Capturing coverage data from .
Found gcov version: 7.3.0
Scanning . for .gcda files ...
geninfo: WARNING: no .gcda files found in . - skipping!
Finished .info-file creation
cd /home/martin/4Neuro/build && /usr/bin/lcov --gcov-tool /usr/bin/gcov -a coverage.base -a coverage.info --output-file coverage.total
Combining tracefiles.
Reading tracefile coverage.base
lcov: ERROR: no valid records found in tracefile coverage.base
CMakeFiles/coverage.dir/build.make:71: recipe for target 'CMakeFiles/coverage' failed
make[3]: *** [CMakeFiles/coverage] Error 255
make[3]: Leaving directory '/home/martin/4Neuro'
CMakeFiles/Makefile2:81: recipe for target 'CMakeFiles/coverage.dir/all' failed
make[2]: *** [CMakeFiles/coverage.dir/all] Error 2
make[2]: Leaving directory '/home/martin/4Neuro'
CMakeFiles/Makefile2:88: recipe for target 'CMakeFiles/coverage.dir/rule' failed
make[1]: *** [CMakeFiles/coverage.dir/rule] Error 2
make[1]: Leaving directory '/home/martin/4Neuro'
Makefile:132: recipe for target 'coverage' failed
make: *** [coverage] Error 2

【问题讨论】:

您可以使用 ctest 作为可执行文件。它将运行您的单元测试。 似乎虽然参数被声明为multivalue 参数,但进一步的处理使用提供给EXECUTABLE 的名字。也许您可以像您的测试可执行文件一样多次使用SETUP_TARGET_FOR_COVERAGE_LCOV 命令;每个覆盖目标将获得不同的名称。不确定结果会发生什么,但我猜它们会合并(没有使用过这个模块)。 @jsantander 我已经尝试使用ctest,正如你所建议的那样。到目前为止它不起作用 - 请参阅我的编辑。 @compor 恐怕它们不会合并,因为如果我理解正确,您应该一个接一个地make 在我的项目中,我有它EXECUTABLE ctest -j $PROCESSOR_COUNT,它选择了两个不同的单元测试可执行文件。 【参考方案1】:

我也遇到了这个问题,并努力寻找解决方案。对我来说,解决方法是将此 CMake 代码移到已定义的任何其他目标之上:

include(CodeCoverage.cmake)
APPEND_COVERAGE_COMPILER_FLAGS()

看起来如果首先定义目标然后编译器标志没有更新。

【讨论】:

【参考方案2】:

好的,我尝试复制您的最小设置。

我有:

.
├── CMakeLists.txt
├── CodeCoverage.cmake
├── test1.cpp
└── test2.cpp

CodeCoverage.cmake 来自https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake

CMakeLists.txt 是:

cmake_minimum_required(VERSION 3.10)
find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
include(CTest)
enable_testing()

add_executable(test1_test test1.cpp)
target_link_libraries(test1_test 
                      $Boost_FILESYSTEM_LIBRARY
                      $Boost_SYSTEM_LIBRARY
                      $Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
add_executable(test2_test test2.cpp)
target_link_libraries(test2_test
                      $Boost_FILESYSTEM_LIBRARY
                      $Boost_SYSTEM_LIBRARY
                      $Boost_UNIT_TEST_FRAMEWORK_LIBRARY)

add_test(NAME test1_test COMMAND test1_test)
add_test(NAME test2_test COMMAND test2_test)

include(CodeCoverage.cmake)
APPEND_COVERAGE_COMPILER_FLAGS()
SETUP_TARGET_FOR_COVERAGE_LCOV(
        NAME coverage                 
        EXECUTABLE ctest -j $n_cores # Executable in PROJECT_BINARY_DIR
        DEPENDENCIES
            test1_test
            test2_test)

而test1.cpp(和test2.cpp)是

#define BOOST_TEST_MODULE test1

#ifndef BOOST_TEST_DYN_LINK
#define BOOST_TEST_DYN_LINK
#endif

#define BOOST_TEST_MAIN

#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>

#define BOOST_TEST_MODULE test1


BOOST_AUTO_TEST_SUITE(test1)

    BOOST_AUTO_TEST_CASE(test1__test) 
        BOOST_CHECK_EQUAL(1, 1);
    

BOOST_AUTO_TEST_SUITE_END()

现在我做:

mkdir build
cd build
cmake ..

输出是:

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- 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
-- Boost version: 1.65.1
-- Found the following Boost libraries:
--   system
--   filesystem
--   unit_test_framework
CMake Warning at CodeCoverage.cmake:116 (message):
  Code coverage results with an optimised (non-Debug) build may be misleading
Call Stack (most recent call first):
  CMakeLists.txt:20 (include)


-- Appending code coverage compiler flags: -g -O0 --coverage -fprofile-arcs -ftest-coverage
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jsantand/t/build

终于运行make coverage

Scanning dependencies of target test1_test
[ 20%] Building CXX object CMakeFiles/test1_test.dir/test1.cpp.o
[ 40%] Linking CXX executable test1_test
[ 40%] Built target test1_test
Scanning dependencies of target test2_test
[ 60%] Building CXX object CMakeFiles/test2_test.dir/test2.cpp.o
[ 80%] Linking CXX executable test2_test
[ 80%] Built target test2_test
Scanning dependencies of target coverage
[100%] Resetting code coverage counters to zero.
Processing code coverage counters and generating report.
Deleting all .da files in . and subdirectories
Done.
Capturing coverage data from .
Found gcov version: 7.3.0
Scanning . for .gcno files ...
Found 2 graph files in .
Processing test1_test.dir/test1.cpp.gcno
Processing test2_test.dir/test2.cpp.gcno
Finished .info-file creation
Test project /home/jsantand/t/build
    Start 1: test1_test
1/2 Test #1: test1_test .......................   Passed    0.01 sec
    Start 2: test2_test
2/2 Test #2: test2_test .......................   Passed    0.01 sec

100% tests passed, 0 tests failed out of 2

Total Test time (real) =   0.02 sec
Capturing coverage data from .
Found gcov version: 7.3.0
Scanning . for .gcda files ...
Found 2 data files in .
Processing test1_test.dir/test1.cpp.gcda
Processing test2_test.dir/test2.cpp.gcda
Finished .info-file creation
Combining tracefiles.
Reading tracefile coverage.base
Reading tracefile coverage.info
Writing data to coverage.total
Summary coverage rate:
  lines......: 65.4% (229 of 350 lines)
  functions..: 68.3% (110 of 161 functions)
  branches...: no data found
Reading tracefile coverage.total
Deleted 0 files
Writing data to /home/jsantand/t/build/coverage.info.cleaned
Summary coverage rate:
  lines......: 65.4% (229 of 350 lines)
  functions..: 68.3% (110 of 161 functions)
  branches...: no data found
Reading data file /home/jsantand/t/build/coverage.info.cleaned
Found 37 entries.
Found common filename prefix "/usr/include"
Writing .css and .png files.
Generating output.
Processing file /home/jsantand/t/test1.cpp
Processing file /home/jsantand/t/test2.cpp
Processing file boost/type_index.hpp
Processing file boost/function/function_base.hpp
Processing file boost/function/function_template.hpp
Processing file boost/smart_ptr/shared_ptr.hpp
Processing file boost/smart_ptr/detail/sp_counted_base_std_atomic.hpp
Processing file boost/smart_ptr/detail/shared_count.hpp
Processing file boost/test/unit_test_suite.hpp
Processing file boost/test/unit_test.hpp
Processing file boost/test/unit_test_log.hpp
Processing file boost/test/tools/assertion_result.hpp
Processing file boost/test/tools/detail/print_helper.hpp
Processing file boost/test/tools/detail/fwd.hpp
Processing file boost/test/tools/old/impl.hpp
Processing file boost/test/tree/observer.hpp
Processing file boost/test/tree/test_unit.hpp
Processing file boost/test/tree/fixture.hpp
Processing file boost/test/tree/decorator.hpp
Processing file boost/test/utils/lazy_ostream.hpp
Processing file boost/test/utils/class_properties.hpp
Processing file boost/test/utils/trivial_singleton.hpp
Processing file boost/test/utils/wrap_stringstream.hpp
Processing file boost/test/utils/basic_cstring/bcs_char_traits.hpp
Processing file boost/test/utils/basic_cstring/basic_cstring.hpp
Processing file boost/type_index/type_index_facade.hpp
Processing file boost/type_index/stl_type_index.hpp
Processing file boost/type_traits/integral_constant.hpp
Processing file c++/7/typeinfo
Processing file c++/7/bits/ios_base.h
Processing file c++/7/bits/move.h
Processing file c++/7/bits/alloc_traits.h
Processing file c++/7/bits/stl_construct.h
Processing file c++/7/bits/stl_vector.h
Processing file c++/7/bits/atomic_base.h
Processing file c++/7/bits/allocator.h
Processing file c++/7/ext/new_allocator.h
Writing directory view page.
Overall coverage rate:
  lines......: 65.4% (229 of 350 lines)
  functions..: 68.3% (110 of 161 functions)
Lcov code coverage info report saved in coverage.info.
Open ./coverage/index.html in your browser to view the coverage report.
[100%] Built target coverage

【讨论】:

非常感谢!我不知道为什么我的代码仍然无法正常工作,因为它与这个基本相同。你介意看看我的代码(git@code.it4i.cz:bes0030/4Neuro.git,分支coverage,使用build.sh进行基本编译)吗?我很好奇它是否会在您的计算机上运行。【参考方案3】:

在 CMake 中,语句的顺序很重要,而您应该放置它们的顺序并不总是那么简单。如果其他人仍然需要对此的答案,请在定义编译器属性之后,在您的根 CMakeLists.txt开头处尝试此操作。例如:

include(CTest)
enable_testing()
set(CMAKE_MODULE_PATH $CMAKE_MODULE_PATH "$PROJECT_SOURCE_DIR/cmake")

include(CodeCoverage)
append_coverage_compiler_flags()
setup_target_for_coverage_gcovr_html(
    NAME coverage
    EXECUTABLE test_basics
    DEPENDENCIES test_basics mysqrt
)

假设您的 CodeCoverage.cmake 位于项目中的 cmake 目录下。生成测试覆盖率报告:

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE:STRING=Debug
make coverage

您的报告将在./coverage/index.html 下提供。

【讨论】:

【参考方案4】:

我已经设法获得了一个有效的设置:

    使用不同的编译器标志编译调试和发布代码 运行测试 创建代码覆盖率和分支覆盖率 html 报告

先决条件

提升:

sudo apt-get install libboost-all-dev

CMake 3.21.0:(转到 https://cmake.org/download/ 获取最新版本)

sudo apt install build-essential libssl-dev
wget https://github.com/Kitware/CMake/releases/download/v3.21.0/cmake-3.21.0.tar.gz
tar -zxvf cmake-3.21.0.tar.gz
cd cmake-3.21.0
./bootstrap
make 
sudo make install 

设置

文件夹结构:

┌ mhcl
├──┬ build
│  ├── debug
│  └── release
├──┬ src
│  ├──┬ biginteger
│  │  ├── biginteger.cpp
│  │  └── biginteger.h
│  ├── CMakeLists.txt
│  └── main.cpp
├──┬ test
│  ├── CMakeLists.txt
│  ├── CodeCoverage.cmake
│  ├── test_biginteger_add.cpp
│  ├── test_biginteger_constructor.cpp
│  ├── test_biginteger_divide.cpp
│  └── test_biginteger_multiply.cpp
└── CMakeLists.txt

src/biginteger/biginteger.cpp:

...
void BigInteger::add(const BigInteger &summand2)
    if(isNegative() == summand2.isNegative())
        add_digits(summand2);
    else
        subtract_digits(summand2);
    
    if(getNumber(false) == "0") setNegative(false);

...

src/biginteger/biginteger.h:

#ifndef BIGINTEGER_H_INCLUDED
#define BIGINTEGER_H_INCLUDED

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <stdexcept>

class BigInteger

    public:
        BigInteger(const char*);
...

mhcl/CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
add_compile_options(-Wall -Wextra -pedantic -Werror)
project(MHCL VERSION 0.1 DESCRIPTION "mhcl, a cryptographic library" LANGUAGES CXX)

set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")

add_subdirectory(test)
add_subdirectory(src) 
add_executable (demo src/main.cpp)
target_link_libraries (demo biginteger)

mhcl/src/CMakeLists.txt:

add_library(biginteger biginteger/biginteger.cpp biginteger/biginteger.h)

mhcl/src/main.cpp:

#include <iostream>
#include <string>
#include "biginteger/biginteger.h"
// this file is not really necessary for testing
int main()
    BigInteger b("3");
    return 0;

mhcl/test/CMakeLists.txt:

IF(CMAKE_BUILD_TYPE MATCHES Debug)
    find_package (Boost COMPONENTS system filesystem unit_test_framework REQUIRED) # sudo apt-get install libboost-all-dev
    include_directories ($MHCL_SOURCE_DIR/src $Boost_INCLUDE_DIRS)

    add_executable (test_biginteger_constructor test_biginteger_constructor.cpp)
    target_link_libraries (test_biginteger_constructor biginteger $Boost_FILESYSTEM_LIBRARY $Boost_SYSTEM_LIBRARY $Boost_UNIT_TEST_FRAMEWORK_LIBRARY)

    add_executable (test_biginteger_add test_biginteger_add.cpp)
    target_link_libraries (test_biginteger_add biginteger $Boost_FILESYSTEM_LIBRARY $Boost_SYSTEM_LIBRARY $Boost_UNIT_TEST_FRAMEWORK_LIBRARY)

    add_executable (test_biginteger_multiply test_biginteger_multiply.cpp)
    target_link_libraries (test_biginteger_multiply biginteger $Boost_FILESYSTEM_LIBRARY $Boost_SYSTEM_LIBRARY $Boost_UNIT_TEST_FRAMEWORK_LIBRARY)

    add_executable (test_biginteger_divide test_biginteger_divide.cpp)
    target_link_libraries (test_biginteger_divide biginteger $Boost_FILESYSTEM_LIBRARY $Boost_SYSTEM_LIBRARY $Boost_UNIT_TEST_FRAMEWORK_LIBRARY)

    include(CTest)
    enable_testing()

    add_test(NAME test_biginteger_constructor COMMAND test_biginteger_constructor)
    add_test(NAME test_biginteger_add COMMAND test_biginteger_add)
    add_test(NAME test_biginteger_multiply COMMAND test_biginteger_multiply)
    add_test(NAME test_biginteger_divide COMMAND test_biginteger_divide)

    include(CodeCoverage.cmake)
    APPEND_COVERAGE_COMPILER_FLAGS()
    setup_target_for_coverage_gcovr_html(NAME coverage EXECUTABLE ctest --schedule-random -j 4 --test-dir test EXCLUDE "/usr/*" "/mnt/v/r3dapple.github.io/encryption/mhcl/test/*" "/mnt/v/r3dapple.github.io/encryption/mhcl/src/main.cpp" DEPENDENCIES test_biginteger_constructor test_biginteger_add test_biginteger_multiply test_biginteger_divide)
ENDIF()

mhcl/test/CodeCoverage.cmake:

https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake

mhcl/test_biginteger_add.cpp:

#define BOOST_TEST_MODULE BigIntegerTestAdd
#include <boost/test/unit_test.hpp>
#include "../src/biginteger/biginteger.h"

BOOST_AUTO_TEST_SUITE(IDENTITY_ELEMENT)
BOOST_AUTO_TEST_CASE(ADDITION_ZERO_LENGTH1_184_LENGTH2_1_SIGN1_PLUS_SIGN2_PLUS)

    BigInteger b("7941336097231331222328305438625646002846406186146402399842122390970234239963370898300943198440768677925436553855703282917116959906917075742261367262117888460342170898243453976126509330");
    b.add("0");
    BOOST_CHECK_EQUAL(b.getNumber(), "7941336097231331222328305438625646002846406186146402399842122390970234239963370898300943198440768677925436553855703282917116959906917075742261367262117888460342170898243453976126509330");

BOOST_AUTO_TEST_SUITE_END()
...

用法

构建调试:

cd mhcl/build/debug && cmake -DCMAKE_BUILD_TYPE=Debug ../.. && make && make coverage

您的覆盖率报告在 mhcl/build/debug/coverage/index.html 下

构建版本:

cd mhcl/build/release && cmake -DCMAKE_BUILD_TYPE=Release ../.. && make

【讨论】:

以上是关于使用具有多个目标的 CodeCoverage.cmake 进行代码覆盖率分析的主要内容,如果未能解决你的问题,请参考以下文章

使用具有多个目标的 CodeCoverage.cmake 进行代码覆盖率分析

具有多个目标的iOS扩展

将 Swift 类添加到具有多个目标的 Objective-C 项目

如何通过程序快速打开具有多个目标坐标的地图应用程序?

将数据从 RDBMS 加载到具有多个目标的 Hadoop

具有多个数据目标的 Bootstrap v3 导航栏切换不会切换类“折叠”