'How to use Visual Studio and CMAKE and GTEST to generate code coverage report
I'm trying to generate a code coverage report using visual studio 2022, using cmake + gtest, but my vs2022 always doesn't work. I've rummaged through google and microsoft documentation and couldn't find a solution.
If I open the project with the .sln file generated by cmake , the code coverage report is generated fine.
That is, when I open a typical vs project (.sln) I can generate the report, but the cmake project does not.
The following three files are the configuration I use, one of the simplest cmake + gtest configuration, the third file is to configure the generator of vs to Visual Studio 17 2022 (to get the .sln file).
cmake_minimum_required(VERSION 3.16.3)
project(cmake-example-test)
set(
TEST_SOURCES
"test.cpp"
)
# gtest
include(FetchContent)
FetchContent_Declare(
googletest
# URL https://file.winterwonder.top/public/package/googletest-release-1.11.0.zip
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(${PROJECT_NAME} ${TEST_SOURCES})
target_link_libraries(
${PROJECT_NAME}
gtest_main
)
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME})
// test.cpp
#include <gtest/gtest.h>
TEST(hello, world) {
EXPECT_EQ(1, 1);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
// CMakeSettings.json
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Visual Studio 17 2022",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"intelliSenseMode": "windows-msvc-x64"
}
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
