English 中文(简体)
单位检测的精度
原标题:cmake coverage on unit tests

I have a project with source files and unit test files. cmake version 3.16.3. my CMakeLists.txt has two targets/executables: one for the app and one for ut.

I want coverage turned on ONLY for the ut target. But it get s turned on for both targets.

cmake_minimum_required(VERSION 3.16)
project(devcpp-lib-template)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-O2")

# === src related
include_directories(${CMAKE_SOURCE_DIR}/lib)
set(SOURCE_FILES
        lib/library_name.h lib/library_name.cpp
        <snip other files here>
)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})

# === Unit Tests
project(ut)

enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
# these turn on coverage
set(CMAKE_CXX_FLAGS "-g -fprofile-arcs -ftest-coverage")
set(UT_FILES
        lib/version.h lib/library_name.h
        <snip ... other files here>
        ut/ut_test1.cpp
)

add_executable(ut ${UT_FILES})
target_link_libraries(${CMAKE_PROJECT_NAME}
        ${GTEST_LIBRARIES}
        pthread
        gcov
)

当我用目标“设计-校准”(即不“全”)来补充上述内容时, cm-build-debug/.../lib/有*.gcno文档(生成的覆盖面文件)。 这是错误的。

当我设定目标“ut”时,.号档案正确显示。 dir 树。

当我尝试将CMAKE_CXX_FLAGS变量与BUILD_TESING(以其他SO员额为基础)混为一谈时,无论是开发-lib-template还是ut目标,都无法获得免疫文件。

if (BUILD_TESTING)
    set(CMAKE_CXX_FLAGS "-g -fprofile-arcs -ftest-coverage")
endif ()

我如何只获得“定点”目标,以悬挂简介国旗和检测国旗?

问题回答

签字。 问题在于使用像套()这样的立克式指挥,包括“指挥”。 全球环境对所有人都有影响。

气球的更佳模式是使用目标——即所有目标——目标——对比目标。

例如,这项工作。 来文方项目没有覆盖物,没有.子档案,ut子项目有覆盖物,因此产生了.。

cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 20)

project(devcpp-lib-template)

# === src related
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
        lib/library_name.h lib/library_name.cpp
        <snip other source files...>   
)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        ${CMAKE_SOURCE_DIR}/lib)
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
        -O2)

# === Unit Tests
project(ut)

enable_testing()
find_package(GTest REQUIRED)
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
        lib/library_name.h lib/library_name.cpp
        <snip other files>                    
        ut/ut_test1.cpp
)
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
        -g -fprofile-arcs-ftest-coverage)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        ${GTEST_INCLUDE_DIR})
target_link_libraries(${CMAKE_PROJECT_NAME}
        ${GTEST_LIBRARIES}
        pthread
        gcov
)

注 目标_xx指挥必须在“可执行”指挥(尽管项目(......)首先宣布)后进行。

设计模式是:

 ...global declarations here...
project(proj1)
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PRIVATE file1, file2, ...)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE dir1 ...)
target_compile_option(${CMAKE_PROJECT_NAME} PRIVATE opt1 ...)

project(proj2)
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PRIVATE file1, file2, ...)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE dir1 ...)
target_compile_option(${CMAKE_PROJECT_NAME} PRIVATE opt1 ...)

etc.

请注意,在宣布项目时,自动更新款。





相关问题
State based testing(state charts) & transition sequences

I am really stuck with some state based testing concepts... I am trying to calculate some checking sequences that will cover all transitions from each state and i have the answers but i dont ...

Running genhtml using cygwin Perl.exe in Windows

I m trying to run genhtml using perl.exe from Cygwin in Windows. I have installed cygwin and placed genhtml in the bin directory of cygwin. I went to that directory and used the command line in ...

Highlight text from Visual Studio 2008 add-in

I m writing another code coverage tool for .NET with Visual Studio 2008 integration. Everything goes well except one thing: I can t find a way to highlight some code chunks. I need it to inform user ...

Cobertura ant script is missing Log4J classes

I tried to get Cobertura running inside my ant script, but I m stuck right at the beginning. When I try to insert the cobertura taskdef I m missing the Log4J libraries. Ant properties & ...

How to omit using python coverage lib?

I would like to omit some module that are in some particular directory : eggs and bin coverage -r -i --omit=/usr/lib/,/usr/share/,eggs,bin Name ...

Code coverage with nUnit? [closed]

Is there a way to see the code coverage when using nUnit? I know there s such a feature in Visual Studio, but can you use it with nUnit or only with the built-in vs unit tests?

run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

热门标签