Gtest和Gmock环境搭建

1 环境配置

1.1 获取gtest最新源码

执行以下命令下载 gtest 的源码

$ git clone https://github.com/google/googletest

没有git的可以直接下载我已经整理好的资源。
gtest源码

1.2 编译gtest源码

1)进入下载的目录 ( 我的目录是 googletest-main) ( 注意: 根据实际情况 cd + 目录名 )

$ cd googletest-main

2)依次执行以下命令生成所需要.a静态库文件 用来链接使用

$ cmake CMakeLists.txt
$ make

3)查看是否在googletest-main/lib目录下生成四个文件

libgmock.a libgmock_main.a libgtest.a libgtest_main.a

生成这是四个文件说明gtest源码已经编成了我们需要的静态库。没有生成需要查下当前环境。

1.3 把需要用到的头文件.h 和静态库.a放到指定文件夹

  1. 返回上级目录
$ cd ../
  1. 新建gmock
$ mkdir gmock
  1. 进入gmock目录
$ cd gmock
  1. 拷贝链接文件
$ cp ../googletest-main/lib/ ./ -r 
  1. 拷贝include文件
$ cp ../googletest-main/googletest/include/ ./ -r 
$ cp ../googletest-main/googlemock/include/ ./ -r

到此为止, gtest 和 gmock 环境就OK了 !

2、环境验证

2.1验证 gtest

1)在gmock文件夹下 新建 test_gtest.cpp 文件。
内容如下:

#include <iostream>
#include "gtest/gtest.h"

using namespace std;
int data = 7;

class test_gtest : public testing::Test
{
protected:
    static void SetUpTestCase()
    {
        cout << "> Before each test case are executed" << endl;
    }
    static void TearDownTestCase()
    {
        cout << "> After each test case are executed" << endl;
    }
    virtual void SetUp()
    {
        cout << "> Before all test cases are executed" << endl;
    }
    virtual void TearDown()
    {
        cout << "> After all test cases are executed" << endl;
    }
};

int main(int argc,char **argv)
{
    testing::GTEST_FLAG(output) = "xml:";   // save xml
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}

/* return a + b */
int add(int a, int b)
{
    return a + b;
}

TEST_F(test_gtest, test0)//相同数据测试不同行为
{
    EXPECT_EQ(add(3, 4), data) << "> add() error";
}

TEST_F(test_gtest, test1)
{
    EXPECT_EQ(add(2, 5), data) << "> add() error";
}

TEST(AddTest,AddTestCase)//第一个参数是测试案例的名称,通常是测试函数或测试类名。第二个参数是测试案例中的测试的名称 测试完成会以"测试用例名.测试名"输出
{
    ASSERT_EQ(2,add(1,1));//比较两个值是否相等
}

//TEST 创建一个简单测试 TEST_F进行多样测试。
//行覆盖率 多个文件执行顺序

2)编译
执行以下命令编译 生成test_gtest.o 再链接库可执行文件main

$ g++ -o test_gtest.o -c test_gtest.cpp -I./include
$ g++ -o main *.o -I./include -L./lib -lgtest -lpthread

3)执行程序

./main

4)结果
打印如下信息, 那说明 gtest 配置正确

[========] Running 1 test from 1 test suite. 
[--------] Global test environment set-up.
[--------] 1 test from test_gtest
> Before each test case are executed
[ RUN    ] test_gtest.test0
> Before all test test cases are executed
> After all test test cases are executed
[      OK]test_gtest.test0 (0 ms)
> After each test case are executed
[--------] 1 test from test_gtest (0 ms total)
[--------] Global test environment tear-down
[========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test. 

2.2验证 gmock

1)在gmock文件夹下 新建 test_gmock.cpp 文件。
内容如下:

#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <iostream>
using namespace std;
using namespace testing;

class Student
{
    public:
    virtual ~Student() {}
    virtual int getAge(string name) = 0;
};
//MOCK_METHODx x为参数个数,最大支持十个。
class MockStudent : public Student
{
    public:
    MOCK_METHOD1(getAge, int(string name));//声明类成员函数 第一个参数是函数名,第二个参数是函数类型
};

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

TEST(test_gmock, test0)
{
    MockStudent XiaoMing;//被模拟出来的类
    EXPECT_CALL(XiaoMing, getAge(_))//第一个参数是被模拟出来的对象,第二个参数是成员函数
        .Times(AtLeast(3))
        .WillOnce(Return(18))
        .WillRepeatedly(Return(19));

    cout << "2020-02-03 : " << XiaoMing.getAge("XiaoMing") << endl;
    cout << "2020-02-04 : " << XiaoMing.getAge("XiaoMing") << endl;
    cout << "2020-02-05 : " << XiaoMing.getAge("XiaoMing") << endl;
    cout << "2020-02-06 : " << XiaoMing.getAge("XiaoMing") << endl;
    cout << "2020-02-07 : " << XiaoMing.getAge("XiaoMing") << endl;
}

2)编译
执行以下命令编译 生成test_gmock.o 再链接库可执行文件main

$ g++ -o test_gmock.o -c test_gmock.cpp -I./include
$ g++ -o main test_gmock.o -I./include -L./lib -lgtest -lgmock -lpthread

3)执行程序

$ ./main

4)结果
打印如下信息, 那说明 gmock 配置正确。

[========] Running 1 test from 1 test suite. 
[--------] Global test environment set-up.
[--------] 1 test from test_gmock
[ RUN    ] test_gmock.test0
2020-02-03 : 18
2020-02-04 : 19
2020-02-05 : 19
2020-02-06 : 19
2020-02-07 : 19
[      OK]test_gmock.test0 (0 ms)
[--------] 1 test from test_gmock(0 ms total)
[--------] Global test environment tear-down
[========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.

注:1,编译C代码用gcc 要加 -lstdc++。2,cmake可以简化编译流程。(后续使用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值