在软件行业,或者是嵌入式行业,软件的测试是非常的重要的,但是很多公司,很多人都仅仅注重软件的功能测试,不注重函数的单元测试,有经验老程序猿可能自己写一些测试用例,但是花费的时间非常多,我今天给大家介绍一款google开发的c++ 测试framework,让程序猿变成老司机。
1. 下载googletest源代码
仓库位置:https://github.com/google/googletest.git
在Linux的shell或者windos的git-bash中使用命令进行仓库克隆:
$ git clone https://github.com/google/googletest.git
2. 下载sum_makefile代码:
$ git clone https://github.com/yangang123/cpp_test/
3. 1个简单的求和函数,我们通过googletest进行对函数的单元测试,
$ cd googletest/googletest/
扫描二维码关注公众号,回复:
1810040 查看本文章
把我的的压缩包sum_makefile文件夹复制到当前目录
$cp ../../sum_makefile ./
$ cd sum_makefile
程序:
sum.cc
#include
"sum.h"
int
sum
(
int
a,
int
b) {
int
result
=
0
;
result
=
a
+
b;
return
result
=
a
+
b ;
}
sum.h
#ifndef
GTEST_SAMPLES_SAMPLE1_H_
#define
GTEST_SAMPLES_SAMPLE1_H_
int
sum
(
int
a,
int
b);
#endif
// GTEST_SAMPLES_SAMPLE1_H_
sum_unittest.c
这个仅仅测试1+2, 是否等于3!!
#include
<limits.h>
#include
"gtest/gtest.h"
#include
"sum.h"
namespace {
TEST
(sumtest, Positive) {
EXPECT_EQ
(
3
,
sum
(
1
,
2
));
}
}
// namespace
$make
$./sum_unittest