- 下载项目https://github.com/google/googletest
- 将项目文件放到指定位置
- unzip解压项目文件
- cmake ./
- make编译
- 然后将项目下的include文件夹,lib文件夹放到当前目录
- 写一个main.cpp文件进行测试,unit.h + unit.c实现某个功能,在头文件中函数声明,.c中具体实现。
/*************************************************************************
> File Name: unit.h
> Author:
> Mail:
> Created Time: 2020年01月12日 星期日 15时47分14秒
************************************************************************/
#ifndef _UNIT_H
#define _UNIT_H
int add(int a, int b);
#endif
/*************************************************************************
> File Name: unit.c
> Author:
> Mail:
> Created Time: 2020年01月12日 星期日 15时49分42秒
************************************************************************/
#include<stdio.h>
#include"unit.h"
int add(int a, int b){
return a + b;
}
- 在main中测试(具体参照源代码)
/*************************************************************************
> File Name: main.cpp
> Author:
> Mail:
> Created Time: 2020年01月12日 星期日 15时28分00秒
************************************************************************/
#include"unit.h"
#include<gtest/gtest.h>
#include<iostream>
using namespace std;
TEST(test, add_function){
EXPECT_EQ(add(3, 4), 7);
}
int main(int argc, char *argv[]){
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
- 在链接+编译时,终极指令g++ --std=c++11 -I./include -L./lib main.cpp unit.c -lgtest -lpthread实现编译阶段
- 最后./a.out输出结果