【问题标题】:Setup Google test in CLion在 CLion 中设置 Google 测试
【发布时间】:2016-02-11 19:53:36
【问题描述】:

我已经在网上坐了几个小时,已经尝试在 Linux 中的 Clion 上设置 Google 测试,但没有找到任何东西。

有人可以指导我进行设置吗?

【问题讨论】:

标签: c++ googletest linux-mint clion


【解决方案1】:

创建新项目

  1. 在我的 ClionProjects 文件夹中创建一个存储库
    • cd ~/ClionProjects
    • mkdir .repo
    • cd .repo
  2. 从github克隆DownloadProject
    • git clone https://github.com/Crascit/DownloadProject.git
  3. 创建一个包含 src 和测试目录的 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)

[我们编译了一个库,以便我们可以将它包含在测试项目中]

测试/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.

测试/gtest.cpp

#include "gtest/gtest.h"

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

注意:如果您自己从事 git 项目,最好将 DownloadProject.cmake 和 DownloadProjects.CmakeLists.cmake.in 文件包含在项目中。

【讨论】:

  • 我不得不从test/CMakeLists.txt 中取出core 并重写src/CMakeLists.txt。之后,它起作用了!
  • @SyntaxRules 谢谢。
  • 我尽可能地按照这些说明进行操作,但最终得到了Error:Configuration Debug The source directory <user Dir>/CLion2016.2/system/cmake/generated/A32N-bdb25b84/bdb25b84/Debug/googletest-src does not contain a CMakeLists.txt file.的负载
  • 对我来说,在 test/CMakeLists.txt 中将 gmock 更改为 gtest_main 是有效的
  • @Dormathal。这是一个可怕的答案,导致不懂 CMake 的人搞砸了他们的整个配置。您正在让他们重写他们的 CMakeLists.txt 文件而不了解更改的影响。一个好的答案解释了每个更改的作用。例如,您可以解释为什么要创建额外的 CMakeList 文件。这是怎么得到这么多赞成的,我想不通!
【解决方案2】:

1.Git克隆google-testC++测试框架

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)

【讨论】:

  • 这工作得很好......很好解释(评论)答案
  • 这是完美的。 google repo 和 clion gtest 文档均不包含工作示例。这行得通。非常感谢
【解决方案3】:

Here is a small example C++11 project which uses GoogleTest 仅依赖于打包的 CMake 功能(主要是 ExternalProject module,并且可以在 CLion 和 *nix 命令行中运行。

此版本显示“供应商”依赖项,如果需要,它可以驻留在项目之外。所有依赖构建的源代码和构建工件都包含在项目中,不会污染构建主机。但是,ExternalProject 模块很容易调整以从远程仓库下载特定版本。

如果需要在 README 中进行说明,请告诉我。

【讨论】:

  • 示例项目的链接已失效。
  • 更正了移动的项目链接。没有必要投反对票。
猜你喜欢
  • 2016-07-30
  • 2016-01-15
  • 2014-01-06
  • 2020-12-15
  • 2020-06-18
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多