在CLion中设置Google测试 [英] Setup Google test in CLion

查看:320
本文介绍了在CLion中设置Google测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上网了几个小时,试图在Linux的Clion上设置Google测试,但是找不到任何东西.

I have been sitting online for hours already trying to setup Google test on Clion in Linux but have not been able to find anything.

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

Can someone guide me with setting this up please?

推荐答案

创建新项目

  1. 在我的ClionProjects文件夹中创建一个存储库
    • cd ~/ClionProjects
    • mkdir .repo
    • cd .repo
  1. Create a repository in my ClionProjects folder
    • cd ~/ClionProjects
    • mkdir .repo
    • cd .repo
  • git clone https://github.com/Crascit/DownloadProject.git

添加以下文件:

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项目中]

[We compile a library so we can include it inside the Test project]

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文件.

Note: if you work yourself on a git Project it is better to include the DownloadProject.cmake and DownloadProjects.CmakeLists.cmake.in files inside the project.

这篇关于在CLion中设置Google测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆