【问题标题】:google test with visual studio 2019 and cmake使用 Visual Studio 2019 和 cmake 进行谷歌测试
【发布时间】:2019-06-09 00:31:05
【问题描述】:

我正在尝试使用 Visual Studio 2019 和 cmake 设置谷歌测试。

这是我的 CMakeFileLists.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 文件如下所示:

#include <gtest/gtest.h>

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

TEST(ABC, TEST2) {
    ASSERT_TRUE(2 == 2);
}

这个最小的例子来自另一个 stackoverflow 问题: CMake file for integrated Visual Studio unit testing

这就是我构建应用程序后得到的:

名为 runUnitTests 的单个测试。但是,在上述问题的答案图片中,我希望看到每个测试函数的名称。像这样的:

runUnitTests
- ABC
  - TEST1
  - TEST2

我已经使用新的 Visual Studio 解决方案对其进行了测试,并添加了一个 google 单元测试项目。将测试功能粘贴到此项目中,结果如下图:

所以这很好用。它一定与我用来处理我的 cmake 项目的 open a local folder 方法有关。

【问题讨论】:

  • 我假设问题是“如何在 studio 2019 中获取测试日志输出”?

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


【解决方案1】:

以下是您问题的可能答案:

cmake_minimum_required(VERSION 3.10)

project(test_me)

enable_testing()

find_package(GTest REQUIRED)
include(GoogleTest)

add_executable(runUnitTests tests.cpp)
target_link_libraries(runUnitTests GTest::GTest GTest::Main)

gtest_discover_tests(runUnitTests)

在您的测试二进制文件中发现完整测试列表的命令是gtest_discover_tests(),它是GoogleTest CMake 模块的一部分(您可以使用cmake --help-module GoogleTest 在本地查看文档)。这个模块已经在 CMake 3.9 中引入,但命令 gtest_discover_tests() 仅在 CMake 3.10 中添加。但是,您应该注意,如果您的测试二进制文件有很多测试用例,这会大大减慢速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多