繁体   English   中英

您如何从头开始在 VS 2019 中设置 Google Test?

[英]How do you setup Google Test in VS 2019 from scratch?

我一直在尝试在 Visual Studio 2019 中设置 google test v1.10 已经很长时间了,但它一直没有工作,而且也发生了很多变化。 大多数教程都没有数据,Microsoft 帮助页面说要使用 google 测试适配器,但我有一个已经存在的项目,我想添加 google 测试。 我已经提到了谷歌测试安装页面( https://github.com/google/googletest/blob/master/googletest/README.md )并尝试使用 cmake 方法进行设置,但它不起作用并且cmake 方法有点复杂。 我想知道是否有人可以帮助像我这样的假人的步骤? 我觉得这也将帮助那些目前正在尝试设置但也被卡住的人。

这就是我为 cmake thingy 所做的:

  1. 在 vs 中创建了一个空项目,并添加了一个 source.cpp 文件和一个 Header.h 文件(例如)。

  2. 我创建了一个 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      ""
)
  1. 我在我的项目文件中创建了一个构建文件夹
  2. 我使用终端中的 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 项目中运行我的测试

这就是我的项目树之后的样子

在此处输入图片说明

按照此链接访问 T:https ://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php您不必使用 CMake。

在VS2019中,为了获得win32静态库和控制台应用,在新建项目部分,语言标签中选择C++,平台标签中选择Windows,类型标签中选择所有项目类型。 然后向下滚动以找到 Windows 桌面向导并选择它。 其余的根据教程进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM