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 中安装了这个
在“工具”>“选项”>“适用于 Google 测试的测试适配器”中,将正则表达式设置为 .exe
第一次运行会找到测试用例
[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/