我已经在网上坐了好几个小时,已经尝试在Linux上的Clion上设置Google测试但是却找不到任何东西。
有人可以指导我吗?
答案 0 :(得分:36)
cd ~/ClionProjects
mkdir .repo
cd .repo
git clone https://github.com/Crascit/DownloadProject.git
cmake_minimum_required(VERSION 3.3)
project(MyProjectName)
add_subdirectory(src)
add_subdirectory(test)
#set(core_SRCS ADD ALL SOURCE FILES HERE)
add_library(core ${core_SRCS})
add_executable(exe main.cpp)
target_link_libraries(exe core)
[我们编译了一个库,因此我们可以将它包含在Test项目中]
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.
#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
文件。
答案 1 :(得分:6)
1.Git克隆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)
答案 2 :(得分:2)
Here is a small example C++11 project which uses GoogleTest仅依赖于打包的 CMake 功能(主要是ExternalProject
module,并且可以在 CLion 和* nix命令行中使用。
此版本显示" vendored"依赖关系,如果需要,可以驻留在项目之外。所有依赖关系构建'源代码和构建工件包含在项目中,不会污染构建主机。然而,ExternalProject
模块很容易调整以从远程仓库下载特定版本。
如果需要澄清README中的内容,请告诉我。