c++ - 用于集成 Visual Studio 单元测试的 CMake 文件

标签 c++ visual-studio unit-testing cmake

Visual Studio 2017 集成了 C++ 单元测试(native、google test、ctest 等)。我如何创建一个 CMakeLists.txt 文件来创建一个像这样的项目,它将使用集成的 IDE 测试,例如使用谷歌测试或 native 微软单元测试框架?遗憾的是,Microsoft 的所有示例都只是在 Visual Studio 中创建项目,而不是从 CMake 文件开始。

感谢任何帮助,谢谢!

最佳答案

迈克沃

我使用与集成 IDE 测试配合使用的 Google 测试项目设置了一个小示例。

创建一个空目录并保存这两个文件:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(test_me)

# GTest
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Unit Tests
# Add test cpp file
add_executable( runUnitTests tests.cpp)
# Link test executable against gtest & gtest_main
target_link_libraries(runUnitTests ${GTEST_BOTH_LIBRARIES})
add_test( runUnitTests runUnitTests )

测试.cpp

#include <gtest/gtest.h>

TEST(ABC, TEST1) {
  EXPECT_EQ(true, true);
}

在命令提示符下键入

mkdir build
cd build
cmake .. "-DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"

注意:我有vcpkg安装gtest

C:\dev\vcpkg>vcpkg.exe install gtest

确保你在 Visual Studio 2017 中安装了这个
enter image description here

在“工具”>“选项”>“适用于 Google 测试的测试适配器”中,将正则表达式设置为 .exe
enter image description here

构建解决方案并在测试资源管理器中按全部运行
enter image description here

第一次运行会找到测试用例

[12/3/2018 8:38:41 AM Informational] ------ Run test started ------
[12/3/2018 8:38:42 AM Warning] Could not locate debug symbols for 'C:\dev\cpptests\GoogleTest\build\Debug\runUnitTests.exe'. To make use of '--list_content' discovery, ensure that debug symbols are available or make use of '<ForceListContent>' via a .runsettings file.
[12/3/2018 8:38:42 AM Informational] Test Adapter for Google Test: Test execution starting...
**[12/3/2018 8:38:42 AM Informational] Found 1 tests in executable** C:\dev\cpptests\GoogleTest\build\Debug\runUnitTests.exe
[12/3/2018 8:38:42 AM Informational] Running 1 tests...
[12/3/2018 8:38:42 AM Informational] Google Test execution completed, overall duration: 00:00:00.2390446
[12/3/2018 8:38:42 AM Informational] ========== Run test finished: 1 run (0:00:01.2668844) ==========

希望这对您有所帮助?

关于c++ - 用于集成 Visual Studio 单元测试的 CMake 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53583286/

相关文章:

.net - 自动化测试.NET(Gallio?)

c# - 检测 C# 代码中的递归调用

visual-studio - 在监 window 口中显示序列

c++ - 如何访问 std::vector 的内部连续缓冲区,我可以将它与 memcpy 等一起使用吗?

c++ - 从文件夹名称获取 makefile 参数

c# - 如何在Visual Studio中创建本地数据库

unit-testing - 如何使用 LLBLGen 进行模拟

java - java 枚举的所有实例的模拟方法

c++ - 我的神经网络学习 sin x 而不是 cos x

c++ - 单例RAII