Visual Studio 测试资源管理器 - 使用 CMake 和远程构建的 GTest

Visual Studio Test Explorer - GTest with CMake and remote build

我有一个 C++ linux 项目,使用 VS2019 在 Windows 上开发并部署到远程构建机器。
我正在尝试使用 googletest 设置单元测试。
我已经使用 CMake 创建了一个示例项目,并且能够在远程构建机器上对 运行 进行测试,但我希望它们出现在 VS 测试资源管理器中。

我已经尝试了 的建议,但我认为它们适用于 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)

据 Danae Xiao [MSFT]

the [CMAKE] tests are not supported in Test Explorer under WSL/Linux configurations.

请参阅此讨论 https://developercommunity.visualstudio.com/idea/955742/googletest-cmake-project-wsl.html