简介
刚接触 C++ 的测试框架,对于菜鸟来说会觉得比较麻烦,我这里的方法不用在系统上安装,跟随项目随处迁移。
操作步骤
-
创建 C/C++ 项目
-
将 googleTest 克隆下来
git clone https://github.com/google/googletest.git
-
将整个 googleTest 复制到项目里
-
配置 CMakeLists.txt,下面是示范
cmake_minimum_required(VERSION 3.9) project(GTest) set(CMAKE_CXX_STANDARD 11) set(googleTestDir ./googletest) #Add the google test subdirectory add_subdirectory(${googleTestDir}) #include googletest/include dir include_directories(${googleTestDir}/googletest/include) #include the googlemock/include dir include_directories(${googleTestDir}/googlemock/include) set(SOURCE_FILE src/add.cpp test/addTest.cpp src/add.h ) add_executable(GTest ${SOURCE_FILE}) # Link with GoogleTest target_link_libraries(GTest gtest gtest_main) #Link with GoogleMock target_link_libraries(GTest gmock gmock_main)
- 实例代码
#include "gtest/gtest.h" int add(int a, int b){ return a+b; } TEST(test1, c1){ EXPECT_EQ(3, add(1,2)); } GTEST_API_ int main(int argc, char** argv){ testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
相关地址
本项目将 googleTest 作为 submodule,下载时请使用
git clone --recursive git@gitee.com:miclewang/GTestDemo.git
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于