【发布时间】:2020-09-07 13:54:32
【问题描述】:
我有一个 C++ linux 项目,在 Windows 上使用 VS2019 开发并部署到远程构建机器。
我正在尝试使用 googletest 设置单元测试。
我已经使用 CMake 创建了一个示例项目,并且能够让我的测试在远程构建机器上运行,但我希望它们出现在 VS 测试资源管理器中。
我已经尝试了来自 this thread 的建议,但我认为它们适用于 Windows 部署,它们不适合我。
是否可以从部署到 linux 的项目中发现测试,如果可以,如何发现?
我的项目目录是这样设置的:
ExampleProject
| CMakeLists.txt
| CMakeLists.txt.in
| main.cpp
|__src
| CMakeLists.txt
| example.cpp
| example.h
|__test
| CMakeLists.txt
| example_add.cpp
外层的CMakeLists.txt是这样设置的:
cmake_minimum_required(VERSION 3.5)
# set the project name
project(ExampleProject)
set(CMAKE_CXX_FLAGS, "${CMAKE_CXX_FLAGS} -std=c++11")
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if (result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if (result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
add_subdirectory(src)
enable_testing()
add_subdirectory(tests)
# add the executable
add_executable(ExampleProject main.cpp)
CMakeLists.txt.in 是从 googletest 复制而来的:
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
src 的 CMakeLists.txt 是:
add_library(example example.cpp)
target_include_directories(example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
用于测试的 CMakeLists.txt 是:
add_executable(ExampleTest example_add.cpp)
target_link_libraries(ExampleTest gtest gtest_main example)
add_test(ExampleTest ExampleTest)
【问题讨论】:
-
我认为测试资源管理器有点问题。还与测试资源管理器一起工作,并试图让它来管理测试用例。看起来它需要更多的工作来完成它的工作。对我有用的是在 CMakeLists.txt 文件中创建“add_test”运行。还要记得在 CMakeLists.txt 根文件夹中添加
enable_testing。测试以尽早发出该命令。我认为 VC 会扫描文件并试图找出配置的内容。
标签: visual-studio cmake visual-studio-2019 googletest