在CLion中设置Google测试
Setup Google test in CLion
我已经在线坐了几个小时,试图在Linux中的Clion上设置谷歌测试,但什么都找不到。
有人能指导我设置这个吗?
创建新项目
- 在我的ClionProjects文件夹中创建存储库
cd ~/ClionProjects
mkdir .repo
cd .repo
- 从github克隆下载项目
git clone https://github.com/Crascit/DownloadProject.git
- 创建一个带有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)
[我们编译了一个库,以便将其包含在测试项目中]
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.
测试/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
文件。
1.Git克隆谷歌测试C++测试框架
From https://github.com/google/googletest.git
2.包括谷歌测试目录
#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.将可执行文件与谷歌测试链接(这是在创建可执行文件之后)
#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)
这里是一个使用GoogleTest的小示例C++11项目,该项目仅依赖于封装的CMake功能(主要是ExternalProject
模块,可从CLion内部和*nix命令行工作。
此版本显示"vendored"依赖项,如果需要,这些依赖项可以驻留在项目之外。所有依赖构建的源代码和构建工件都包含在项目中,不会污染构建主机。然而,ExternalProject
模块很容易进行调整以下载远程回购的特定版本。
如果有什么需要在自述中澄清,请告诉我。
相关文章:
- 在macOS Mojave上发布BOOST unit_test_framework/Google测试
- Xcode Test Navigator 如何在纯C++项目中显示 Google 测试
- 使用Mingw-w64编译Google测试
- 使用cmake构建google测试单元测试时出现链接器错误
- 在 Google 测试中,我可以从构造函数调用 GetParam() 吗?
- 如何匹配C Google测试中双向量的元素
- 在Google测试框架中,如何期望函数调用或其他功能调用
- Cmake,子目录和Google测试(模拟)无法正确链接.找不到测试
- Google测试 - 使用2个不同的项目时未定义的恢复
- 如何在 Google 测试中使用不同模板测试多个模板化类的相同行为?
- 如何从Visual Studio解决方案中完全删除Google测试
- STL 类型的 Google 测试和运算符<<重载
- 为什么Google测试样本将测试放在匿名名称空间中
- Expect_throw-实际:它抛出不同类型的Google测试
- GTEST/Google测试C 在Visual Studio 2017上构建错误
- 如何在Google测试(gtest)中使用fixture成员值运行参数化测试
- 我如何自定义Google测试故障消息
- VCPKG不适用于Google测试
- 如何使用Eclipse CDT中的Google测试来管理测试输出
- 使用MSVC 2015上的Windows上使用Google测试