知芯

Clion-googleTest-demo

2019-05-04

1. 基本步骤

  1. 创建C/C++项目
  2. 将googleTest克隆下来
    1
    git clone https://github.com/google/googletest.git
  3. 将整个googleTest复制到项目里
  4. 配置CMakeLists.txt,下面是示范
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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)
  5. 实例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #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();
    }
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章