【发布时间】:2020-10-30 13:00:31
【问题描述】:
我已经尝试在 Visual Studio 2019 中设置 google test v1.10 很长时间了,但它一直没有工作,而且发生了很多变化。大多数教程都没有数据,微软帮助页面说要使用谷歌测试适配器,但我有一个已经存在的项目,我想添加谷歌测试。我参考了google测试安装页面(https://github.com/google/googletest/blob/master/googletest/README.md)并尝试使用cmake方法进行设置,但它不起作用并且cmake方法有点复杂。我想知道是否有人可以帮助像我这样的假人的步骤?我觉得这也将帮助那些正在尝试设置它并且也被卡住的人。
这就是我为 cmake 做的事情:
-
在vs中创建了一个空项目,并添加了一个source.cpp文件和一个Header.h文件(示例)。
-
我创建了一个 CMakeLists.txt 和一个 CMakeLists.txt.in 文件并将其复制粘贴到 第一个(为我的项目做了一些调整)
# 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()
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(test_ source.cpp)
target_link_libraries(test_ gtest_main)
add_test(NAME example_test COMMAND test)
和 CMakeLists.txt.in
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 ""
)
- 我在我的项目文件中创建了一个构建文件夹
- 我使用终端中的 cmake 命令将其构建到构建文件夹中 像这样
cmake -S "path to src code" -B "path to build"
这些是我收到的警告:
No project() command is present. The top level CMakeLists.txt file must contain a literal, direct call to the project() command. And a line of code such as
project(projectName)
near the top of the CMake file, but after cmake_minimum_required().
CMake is pretending there is a "project(projectName)" command on the first line.
This warning is for project developers. Use who-dev to supress it.
这就是 /build 中的内容
但是我知道这之后该怎么办?如何开始创建测试?如果可能的话,我想在我当前的解决方案中创建另一个项目,以便我可以单独运行测试
这是什么意思
如果可能的话,我想在 googletest 项目中运行我的测试
这就是我的项目树之后的样子
【问题讨论】:
-
什么不起作用?您在此过程的哪个阶段收到了哪些错误消息?
-
@starturtle 我添加了更多信息,你能帮忙吗?
-
例如,我缺少的是
enable_testing()电话。此外,您自己的 CMakeLists.txt 的开头应包含cmake_minimum_required(VERSION 2.8.2)(或您需要的任何内容)和project(Project24)调用(这是您的警告)。您的 add_executable 调用应该是add_executable(myTest test_source.cpp),更改为add_test(NAME example_test COMMAND myTest)。这就是我到目前为止所看到的。遗憾的是,我只熟悉 CMake 或 gtest,而不熟悉同一个项目中的两者。
标签: c++ unit-testing installation visual-studio-2019 googletest