c++ cmake 项目 引入 googletest

背景

在letcode练习c++算法题时,每次都要修改main方法调用写的方法,这样对于一个大型项目来说是不合理的,单元测试不应该通过修改main入口函数来测试。
于是研究了下googletest,终于做出了第一个demo,github地址

googletest使用

目录结构:

.
├── CMakeLists.txt
├── googletest
│ ├── 省略
├── main.cpp
├── src
│ ├── CMakeLists.txt
│ └── util
│  ├── operate.cpp
│  └── operate.h
└── test
 ├── CMakeLists.txt
 └── src
  ├── CMakeLists.txt
  └── util
            ├── CMakeLists.txt
            ├── operate_test.cpp
            └── util_test.cpp

要测试的类为operate.cpp,于是test目录下创建了与src同级的目录,添加operate_test.cpp单元测试类。

引入googletest步骤:

  1. 从github下载googletest并且解压
  2. 将解压后的googletest文件复制到工程目录下
  3. 创建test文件,测试用例在test文件下编写
  4. 创建src文件,项目文件在src下编写
  5. 在src创建util目录,模拟单元模块
  6. util目录下创建了一个class,其内容如下

operate.h

#ifndef LETCODE_OPERATE_H
#define LETCODE_OPERATE_H

class operate {
public:
    int twoSum(int one, int two);
};

#endif //LETCODE_OPERATE_H

operate.cpp

#include "operate.h"

int operate::twoSum(int one, int two) {
   return one + two;
}
  1. 在src下面编写 CMakeLists.txt 文件,将util下的类打包成一个lib,叫做src
aux_source_directory(. src_files)

aux_source_directory(util src_files)

add_library(src ${src_files})
  1. 在工程根路径下创建main.cpp,为项目的入口函数
#include <iostream>
#include "src/util/operate.h"

int main() {
    operate f1;
    int result = f1.twoSum(1, 2);
    std::cout << "result: " << result << std::endl;
    return 0;
}
  1. 再在工程根路径创建CMakeLists.txt文件(如果想要执行看看结果,把)add_subdirectory("test")注释掉就可以运行了)
# cmake_minimum_required(VERSION <specify CMake version here>)
project(letcode)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory("src")
add_subdirectory("test")
add_subdirectory("googletest")

#FILE(GLOB_RECURSE TEST_SRC "${CMAKE_SOURCE_DIR}/*.cpp" "${CMAKE_SOURCE_DIR}/*.h")

enable_testing()
include_directories(${CMAKE_SOURCE_DIR}/googletest/googletest/include)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} src gtest_main) 
  1. 在test目录下创建src/util文件夹,等会儿测试用例将会编写在test/src/util下
  2. 在test/src/util目录创建CMakeLists.txt、operate_test.cpp、util_test.cpp文件,其中operate_test.cpp里面编写的是测试用例,util_test.cpp是测试用力的入口函数

util_test.cpp

#include <gtest/gtest.h>

int main(int argc,char **argv) {
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}

operate_test.cpp

#include <gtest/gtest.h>
#include "src/util/operate.h"

TEST(utilTestCase, testTwoSum){
    operate op;
    EXPECT_EQ(op.twoSum(2,3),5);
}

CMakeLists.txt

project(SrcUtilTest)

set(CMAKE_CXX_STANDARD 11)

include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/googletest/googletest/include)

# 读取当前目录及子目录的所有头文件和源文件,存储到 TEST_SRC_UTIL 变量中
FILE(GLOB_RECURSE TEST_SRC_UTIL "*.cpp" "*.h")

add_executable(${PROJECT_NAME} ${TEST_SRC_UTIL})

target_link_libraries(${PROJECT_NAME} src gtest_main)
  1. 在 test/src下创建CMakeLists.txt文件,将test/src/util下的文件添加进项目
add_subdirectory("util")
  1. 在 test目录下创建CMakeLists.txt,将test/src下的文件添加进项目
add_subdirectory("src")
  1. 在工程根目录创建 build 文件夹
  2. cd build
  3. cmake ..
  4. make
  5. ./test/src/util/SrcUtilTest 执行编译好的二进制文件,执行当前目录下的所有单元测试用例,结果如下,测试用例执行成功。
    在这里插入图片描述
    如果不想手敲代码,见github地址
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值