【问题标题】:CMake using GTest doesn't work properly使用 GTest 的 CMake 无法正常工作
【发布时间】:2018-04-06 15:54:06
【问题描述】:

我正在尝试使用 googletest 设置 CMake C++ 项目。我对 CMake 并没有真正的经验,并使用 this 指南创建自己的设置。

在尝试构建项目时,编译器会抛出错误:

.../test.cpp:12:5: error: ‘EXPECT_THAT’ was not declared in this scope
     EXPECT_THAT(a, ContainerEq(b));
     ^~~~~~~~~~~
.../test.cpp:12:5: note: suggested alternative: ‘EXPECT_GT’
     EXPECT_THAT(a, ContainerEq(b));
     ^~~~~~~~~~~
     EXPECT_GT
../test.cpp:13:31: error: ‘ContainerEq’ is not a member of ‘testing’
     EXPECT_THAT(a, ::testing::ContainerEq(b)); // doesn't work either

GTest 的某些部分似乎工作正常。如果我注释掉第二个测试,一切正常。

文件夹结构:

  • CMakeLists.txt
  • 测试/
    • CMakeLists.txt
    • gtest.cmake
    • test.cpp

顶级 CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
project (TestProject)
enable_testing()
add_subdirectory(test)

test/CMakeLists.txt

include(gtest.cmake)
add_executable(UnitTests test.cpp)
target_link_libraries(UnitTests libgtest)
add_test(NAME AllUnitTests COMMAND UnitTests)

test/gtest.cmake

find_package(Threads REQUIRED)

# Enable ExternalProject CMake module
include(ExternalProject)

# Download and install GoogleTest
ExternalProject_Add(
    gtest
    URL https://github.com/google/googletest/archive/master.zip
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/gtest
    # Disable install step
    INSTALL_COMMAND ""
)

# Get GTest source and binary directories from CMake project
ExternalProject_Get_Property(gtest source_dir binary_dir)

# Create a libgtest target to be used as a dependency by test programs
add_library(libgtest IMPORTED STATIC GLOBAL)
add_dependencies(libgtest gtest)

# Set libgtest properties
set_target_properties(libgtest PROPERTIES
    "IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a"
    "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
)

# Create a libgmock target to be used as a dependency by test programs
add_library(libgmock IMPORTED STATIC GLOBAL)
add_dependencies(libgmock gtest)

# Set libgmock properties
set_target_properties(libgmock PROPERTIES
    "IMPORTED_LOCATION" "${binary_dir}/googlemock/libgmock.a"
    "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
)

# I couldn't make it work with INTERFACE_INCLUDE_DIRECTORIES
include_directories("${source_dir}/googletest/include"
                    "${source_dir}/googlemock/include")

test/test.cpp

#include <vector>
#include "gtest/gtest.h"

TEST(SampleTest, Equal){
    EXPECT_EQ(42, 42);
}

TEST(ContainerComparison, Equal){
    const std::vector<int> a(2, 1);
    const std::vector<int> b(2, 1);

    EXPECT_THAT(a, ContainerEq(b));
    EXPECT_THAT(a, ::testing::ContainerEq(b)); // doesn't work either
}

int main(int argc, char** argv){
    ::testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}

【问题讨论】:

    标签: c++ cmake googletest


    【解决方案1】:

    Matchers are a part of GMock。您需要包含它(并链接到它的库)。

    #include <gmock/gmock.h>
    

    GMock 曾经是一个独立的库,现在是merged into GoogleTest repository。但是它有单独的包含和库。

    您学习的教程是 2014 年的。您可以在 GoogleTest GitHub repo 中找到不错的最新教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      相关资源
      最近更新 更多