原文地址:linux下使用gtest框架进行c/c++测试(二)-gmock环境搭建
前言
google mock(以下简称gmock)也是google的开源项目,可在测试时进行“狸猫换太子” ,
配合了gtest使用,测试效果可更佳。
准备gtest框架
在github网站下载gtest框架https://github.com/google/googletest
解压后,进入googltetest目录。目录下有以下内容
├── build-aux
├── CHANGES
├── cmake
├── CMakeLists.txt
├── codegear
├── configure.ac
├── CONTRIBUTORS
├── docs
├── include
├── LICENSE
├── m4
├── make
├── Makefile.am
├── msvc
├── README.md
├── samples
├── scripts
├── src
├── test
└── xcode
现在我们需要获得gmock的.a文件
进入googlemock的make文件夹,执行make,再执行
1
|
ar -rv libgmock.a gtest-all.o gmock-all.o
|
为确保成功,可以运行目录下生成的./gmock_test。
生成的libgmock.a以及该目录include下的头文件都是我们后面测试需要的。
gmock中已经包含了gtest的库,因为只需要gmock就可以使用gtest的所有测试功能。
新建测试项目
新建项目目录,比如我创建了一个TestWithGtest目录。
(已看过第一篇的可跳过新建项目部分,但是要注意其中部分头文件的改动)
同时,在该目录下创建以下三个目录
├── gmock //存放gtest框架内容
├── leetcode //存放项目源代码
├── testcase //存放测试用例代码
└── Makefile
gmock目录下有刚才所说的include头文件,lib文件。另外,我们还需要一个main函数文件。放在gmock的main下
1
2
3
4
5
6
7
8
9
10
11
12
|
/*TestAll.cpp*/
using
namespace
std;
int main(int argc,char* argv[])
{
//testing::GTEST_FLAG(output) = "xml:"; //若要生成xml结果文件
testing::InitGoogleTest(&argc,argv);
//初始化
if( RUN_ALL_TESTS())
//跑单元测试
return
0;
}
|
leetcode目录下新建src目录和include目录,分别用于存放头文件和.c文件
我在include下放了两个头文件,common.h 和leetcode_functions.h
内容分别如下:
common.h
1
2
3
4
5
|
|
leetcode_functions.h
1
2
3
4
5
6
7
8
|
//leetcode_functions.h
int* twoSum(int* nums, int numsSize, int target);
|
src目录下为项目的.c文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/**
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Note: The returned array must be malloced, assume caller calls free().
*
*/
int* twoSum(int* nums, int numsSize, int target) {
int loop =
0;
int inloop =
0;
int* result =
NULL;
result =(
int*)
malloc(
2*
sizeof(
int));
memset(result,
0,
2*
sizeof(
int));
printf(
"numsSize=%d\n",numsSize);
if(
NULL == nums || numsSize==
0)
{
return result;
}
for(loop =
0;loop < numsSize;loop++)
{
for(inloop = loop+
1;inloop < numsSize;inloop++)
{
if(*(nums+loop)+*(nums+inloop) == target)
{
if(
NULL != result)
{
*result = loop;
*(result+
1) = inloop;
}
return result;
}
}
}
return result;
}
|
testcase下新建include和src目录
src目录下有测试用例文件leetcode_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//leetcode_test.cpp
TEST(two_sum_test,twoSum001)
{
int nums[
4]={
2,
7,
11,
15};
int target =
9;
int numsSize =
4;
int* result = twoSum(nums,numsSize,target);
ASSERT_EQ(
0,*result);
ASSERT_EQ(
1,*(result+
1));
}
|
include下有头文件
googletest.h
1
2
|
#include"gmock/gmock.h"
#include"gtest/gtest.h"
|
编译链接
测试之前,需要编译并链接我们得项目代码和测试框架。
编译
1
2
3
4
5
6
|
g++ -o
001_two_sum.o -c ./leetcode/src/
001_Two_Sum.c -I ./leetcode/include/
g++ -o leetcode_test.o -c ./testcase/src/leetcode_test.cpp -I ./ -I ./leetcode/include/
g++ -o TestAll.o -c ./gmock/main/TestAll.cpp -I ./
|
链接
1
2
|
g++ -o main *.o -I./include -L./gmock/lib -lgtest -lpthread
|
最后生成main文件。
运行测试
运行生成的可执行文件main
1
2
|
./main
|
运行结果如下:
[==========] Running 1 test from 1 test case.
[———-] Global test environment set-up.
[———-] 1 test from two_sum_test
[ RUN ] two_sum_test.twoSum001
numsSize=4
[ OK ] two_sum_test.twoSum001 (0 ms)
[———-] 1 test from two_sum_test (0 ms total)
[———-] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
可以看到,共有一个用例,通过了一个用例
例子中,暂时未使用到gmock,后续补充
项目代码可以在github下载
总结
使用gmock可以很方便的对一些函数打桩,或者返回特定的返回值,以便测试继续进行。
最后,欢迎交流讨论^_^
原文链接:http://www.huyanbing.me/2017/08/06/13315.html