在CLion中设置Google测试

46

我已经在线上花了几个小时尝试在Linux的Clion中设置Google测试,但一无所获。

有人能指导我如何设置吗?


4
这是教程:https://www.youtube.com/watch?v=8Up5eNZ0FLw。 - uta
3个回答

41

创建新项目

  1. 在我的ClionProjects文件夹中创建一个存储库
    • cd ~/ClionProjects
    • mkdir .repo
    • cd .repo
  2. 从github克隆DownloadProject
    • git clone https://github.com/Crascit/DownloadProject.git
  3. 创建一个带有src和test目录的C++项目

添加以下文件:

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

project(MyProjectName)

add_subdirectory(src)
add_subdirectory(test)

src/CMakeLists.txt

#set(core_SRCS ADD ALL SOURCE FILES HERE)

add_library(core ${core_SRCS})
add_executable(exe main.cpp)
target_link_libraries(exe core)

我们编译一个库,以便将其包含在测试项目中。

test/CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

set(REPO ~/ClionProjects/.repo)

project(Test)

project(Example)

include(CTest)
enable_testing()

#set(gtest_disable_pthreads on) #needed in MinGW
include(${REPO}/DownloadProject/DownloadProject.cmake)
download_project(
        PROJ                googletest
        GIT_REPOSITORY      https://github.com/google/googletest.git
        GIT_TAG             master
        UPDATE_DISCONNECTED 1
        )

add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)

#set(test_SRCS ADD ALL TEST SOURCE FILES HERE)
add_executable(runUnitTests gtest.cpp ${test_SRCS})
target_link_libraries(runUnitTests gtest gmock core)
#add_test(runUnitTests runUnitTests) #included in all tutorials but I don't know what it actually does.

test/gtest.cpp

#include "gtest/gtest.h"

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

注意:如果您在 Git 项目上工作,最好将 DownloadProject.cmakeDownloadProjects.CMakeLists.cmake.in 文件包含在项目内部。

2
我不得不从test/CMakeLists.txt中删除core并重新编写src/CMakeLists.txt。之后,它就可以工作了! - SyntaxRules
@SyntaxRules 谢谢。 - aristotll
我尽可能地按照这些指示进行操作,但最终出现了一堆错误:“Error:Configuration Debug 源目录 <user Dir>/CLion2016.2/system/cmake/generated/A32N-bdb25b84/bdb25b84/Debug/googletest-src 中不包含 CMakeLists.txt 文件。” - L Selter
对我来说,将test/CMakeLists.txt中的gmock更改为gtest_main是有效的。 - anat0lius
9
@Dormathal。这是一个可怕的答案,会导致不了解CMake的人搞砸他们的整个配置。您让他们重新编写CMakeLists.txt文件,却不理解更改的影响。一个好的答案应该解释每个更改的含义。例如,您可以解释为什么要创建额外的CMakeList文件。我对它有这么多赞怎么回事超出了我的理解! - ATL_DEV
这太棒了!我有一段时间没有使用CMake了,但是我对它的设置很熟悉,所以在你的指导下这变得轻而易举。明天我要面试一个主要负责编写gtests的职位,而我已经好几年没有写过gtests了,所以这个快速设置非常适合我在明天之前恢复技能。非常感谢和赞赏! - skittlebiz

18

1. 克隆Google Test C++ 测试框架

From https://github.com/google/googletest.git

2. 包括 google-test 目录

#Add the google test subdirectory
add_subdirectory(PATH_TO_GOOGLETEST)

#include googletest/include dir
include_directories(PATH_TO_GOOGLETEST/googletest/include)

#include the googlemock/include dir
include_directories(PATH_TO_GOOGLETEST/googlemock/include)

3. 将您的可执行文件与 Google Test 链接在一起(这是在创建可执行文件之后进行的操作)

#Define your executable
add_executable(EXECUTABLE_NAME ${SOURCE_FILES})

#Link with GoogleTest
target_link_libraries(EXECUTABLE_NAME gtest gtest_main)

#Link with GoogleMock
target_link_libraries(EXECUTABLE_NAME gmock gmock_main)

这个运行完美... 解释得很好(有注释的答案) - cool
这太完美了。谷歌仓库和CLion GTest文档都没有包含一个可用的示例。这个可以用。非常感谢。 - Käptn Freiversuch
完美运行!谢谢。 - ghaidaBouchaala

3
这是一个小型的C++11项目示例,使用了GoogleTest,仅依赖于打包的CMake功能(主要是ExternalProject模块),可以在CLion和*nix命令行中运行。

此版本显示了“vendored”依赖项,如果需要,可以将其放置在项目外部。所有依赖项构建的源代码和构建工件都包含在项目中,并且不会污染构建主机。但是,ExternalProject模块相当容易调整,以从远程存储库下载特定版本。

如果需要README中的澄清,请告诉我。


示例项目的链接已失效。 - Ext3h
1
更正了移动后的项目链接。不必要进行负面评价。 - Vlad Didenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接