用于集成Visual Studio单元测试的CMake文件 [英] CMake file for integrated Visual Studio unit testing
问题描述
Visual Studio 2017已集成C ++单元测试(本机,google测试,ctest等).我如何创建一个CMakeLists.txt文件,该文件将创建一个像这样的项目,该项目将使用集成的IDE测试,例如使用google测试或本机Microsoft单元测试框架?不幸的是,所有Microsoft的示例都只是在Visual Studio中创建项目,而不是从CMake文件开始.
Visual Studio 2017 has integrated C++ unit testing (native, google test, ctest, etc.). How can I create a CMakeLists.txt file that will create a project like this that will use the integrated IDE testing, for example using either google test or the native microsoft unit testing framework? All of Microsoft's examples unfortunately just create the project in Visual Studio, rather than starting from a CMake file.
感谢您的帮助,谢谢!
推荐答案
迈克,
我使用与集成IDE测试一起使用的Google Test项目设置了一个小示例.
I setup a small example using Google Test project that works with integrated IDE testing.
创建一个空目录并保存以下两个文件:
Create an empty directory and save these two files:
CMakeLists.txt
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 )
tests.cpp
tests.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
Note: I had vcpkg install gtest
C:\dev\vcpkg>vcpkg.exe install gtest
确保已在Visual Studio 2017中安装了此程序
Make sure you have this installed in Visual Studio 2017
在工具>选项> Google Test的测试适配器中,将正则表达式设置为.exe
In Tools > Options > Test Adapter for Google Test set the regex to .exe
构建解决方案,然后在测试资源管理器"中按全部运行"
Build the solution and press Run all in the Test Explorer
它第一次运行时会找到测试用例
The first time it runs it will find the test case
[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) ==========
我希望这会有所帮助吗?
I hope this helps?
这篇关于用于集成Visual Studio单元测试的CMake文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!