ClionLLT搭建
1. 准备工作
a.下载googletest源码
gtest源码下载地址:
下载地址:https://github.com/google/googletest
git 仓库地址:https://github.com/google/googletest.git
b.clion工程准备
2. 目录结构
3.Cmake脚本
function.cmake
#扫描源文件 function(glob_all_source_files DIR_LIST) foreach(dir ${DIR_LIST}) ## GLOB_RECURSE 会搜索子目录 GLOB 搜索当前目录 file(GLOB SRC_C_LIST "${dir}/*.c") file(GLOB SRC_CXX_LIST "${dir}/*.cpp") list(APPEND FILE_LIST ${SRC_C_LIST} ${SRC_CXX_LIST}) endforeach() #源文件会存放到变量TRAGET_OBJ_SRC_LIST中 set(TRAGET_OBJ_SRC_LIST ${FILE_LIST} PARENT_SCOPE) endfunction() #调用函数 glob_all_source_files("${TARGET_OBJ_SRC_PATH}") #添加到动态库 #add_library(CppLLT OBJECT ${TRAGET_OBJ_SRC_LIST}) #add_executable 之后链接 #target_link_libraries(CppLLT ${CppTestObj}) #或者直接编译到执行文件中 add_executable(CppLLT main.cpp ${TRAGET_OBJ_SRC_LIST})
gtest.cmake
#googletest代码主目录 set(GOOGLETEST_MAIN_PATH ${CMAKE_SOURCE_DIR}/googletest) set(PATH_GTEST_INC_LIST ${GOOGLETEST_MAIN_PATH}/googletest/include ${GOOGLETEST_MAIN_PATH}/googletest/include/gtest ${GOOGLETEST_MAIN_PATH}/googlemock/include ${GOOGLETEST_MAIN_PATH}/googlemock/include/gmock) include_directories(${PATH_GTEST_INC_LIST}) ##set(build_gooletest "ON") 重新编译gtest ##需要在add_executable之后链接 target_link_libraries(CppTest ${gtestlibs}) ##set(build_gooletest "OFF") 直接连接准备好的静态库 if ("${build_gooletest}" STREQUAL "ON") ##编译有告警的时候,删除googletest源码里面对低版本cmake的语句 cmake_minimum_required(VERSION ...) set(GOOGLETEST_VERSION 3.15) #加入编译 add_subdirectory(${GOOGLETEST_MAIN_PATH}) set(gtestlibs gtest gtest_main gmock gmock_main) #放在add_executable之后 #target_link_libraries(CppTest ${gtestlibs}) ##下面不用 #Link with GoogleTest #target_link_libraries(CppTest gtest gtest_main) #Link with GoogleMock #target_link_libraries(CppTest gmock gmock_main) else() #静态库目录 link_directories(${PROJECT_SOURCE_DIR}/lib) set(LLT_GTEST_LIBS libgmock_maind.a libgmockd.a libgtest_maind.a libgtestd.a) link_libraries(${LLT_GTEST_LIBS}) endif()
CMakeList.txt
cmake_minimum_required(VERSION 3.20) project(CppLLT) set(CMAKE_CXX_STANDARD 17) add_compile_options(-Wall) # ON 重新编译gtest OFF,链接准备好的库 set(build_gooletest "OFF") include("${PROJECT_SOURCE_DIR}/gtest.cmake") include("${PROJECT_SOURCE_DIR}/function.cmake") #添加源代码目录 set(TARGET_OBJ_SRC_PATH ${PROJECT_SOURCE_DIR}/src) glob_all_source_files("${TARGET_OBJ_SRC_PATH}") add_executable(CppLLT main.cpp ${TRAGET_OBJ_SRC_LIST}) #链接gtest库 if ("${build_gooletest}" STREQUAL "ON") target_link_libraries(CppLLT ${gtestlibs}) endif()
注:googletest不用每次都编译,第一次编译出来后,拷贝到一个地方,后续直接去链接就行了。
我这里拷贝放到了lib目录下。
4. 测试用例
#include "gtest.h" int AddTest(int a, int b) { return a + b; } TEST(AddTest, test_t0) { EXPECT_EQ(AddTest(1, 2), 3); }
5. 主函数
#include "gtest.h" #include "gmock.h" GTEST_API_ int main(int argc, char **argv) { testing::InitGoogleMock(&argc, argv); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
6. 执行结果
常见问题:
1. googletest提示cmake版本要求较低冲突
CMake Deprecation Warning at googletest/googletest/CMakeLists.txt:56 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed from a future version of CMake. Update the VERSION argumentvalue or use a ... suffix to tell CMake that the project does not need compatibility with older versions.
这种在主目录已经指定了cmake的最低版本要求,删除googletest里面对cmake最低版本的请求即可。
2. cygwin编译器和mingw编译器,在普通的测试TEST的时候没区别,在需要用到gmock的时候,cygwin报错信息不够详细,这个时候建议用mingw
例如:测试用例
#include "gtest.h" #include "gmock.h" #include <string> #includeusing namespace std; using namespace testing; class Student { public: virtual ~Student(){}; virtual int GetAge(string name) = 0; }; class MockStudent : public Student { public: //MOCK_METHOD1(GetAge, int(string name)); MOCK_METHOD(int, GetAge, (string name), (override)); }; TEST(MockStudent, test_0) { cout<< "func begin: " << __func__ << endl; MockStudent zhang; EXPECT_CALL(zhang, GetAge(_)) .Times(::testing::AtLeast(3)) .WillOnce(::testing::Return(18)) .WillRepeatedly(::testing::Return(19)); cout<< "func end:" << __func__ << endl; }
Mingw编译器报错信息如下:
cgywin编译器报错如下,不方便定位:
后来我发现,这玩意儿是cygwin不支持gmock,切换成mingw就正常了。真是吐了!