第一个gtest程序

第一个gtest程序(Linux)

转自   https://www.jianshu.com/p/778f835cc18c

gtest是一个跨平台(Liunx、Mac OS X、Windows、Cygwin、Windows CE and Symbian)C++单元测试框架,由google公司发布。gtest是为在不同平台上为编写C++测试而生成的。它提供了丰富的断言、致命和非致命判断、参数化、”死亡测试”等等。 
了解了什仫是gtest之后下面让我们来学习gtest的一些使用规则吧! 

Linux下使用Google test

安装Google test

git clone https://github.com/google/googletest.git
cd googletest
mkdir build
cmake ..
make
sudo make install

以上命令会将gtest编译好,并将动态链接库放在/usr/local/lib 目录下:

huangyang@ubuntu:/usr/local/lib$ ls
libgmock.a       libgtest.a       pkgconfig  python3.5
libgmock_main.a  libgtest_main.a  python2.7

如上所示,四个.a文件是gtest的动态链接库,在编写gtest的时候只需要链接这些文件就可以使用gtest。

将头文件放在/usr/local/include目录下:

huangyang@huangyang-desktop:~/Notebook$ cd /usr/local/include/
huangyang@huangyang-desktop:/usr/local/include$ ls
gmock  gtest

使用gtest

  1. add程序

    add.cc

    #include <iostream>
    
    int add(int a, int b)
    {
        return a + b;
    }
    
    //int main()
    //{
    //    std::cout << add(3, 10);
    //}
    
  2. 为add程序编写单元测试用例

    testAdd.cc

    #include <gtest/gtest.h>
    
    extern int add(int a, int b);
    
    TEST(testCase, test0)
    {
        EXPECT_EQ(add(2, 3), 5);
    }
    
    int main(int argc, char **argv)
    {
     testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
    }
    
  3. 编译和链接程序

    huangyang@ubuntu:~/code$ g++ add.cc testAdd.cc -lgtest -lpthread
    huangyang@ubuntu:~/code$ ./a.out
    [==========] Running 1 test from 1 test case.
    [----------] Global test environment set-up.
    [----------] 1 test from testCase
    [ RUN      ] testCase.test0
    [       OK ] testCase.test0 (0 ms)
    [----------] 1 test from testCase (0 ms total)
    
    [----------] Global test environment tear-down
    [==========] 1 test from 1 test case ran. (0 ms total)
    [  PASSED  ] 1 test.
    

    使用

    g++ add.cc testAdd.cc -lgtest -lpthread

    编译和链接单元测试用例。

    -lgtest 是链接 libgtest.a库,另外也需要链接pthread。

    可以看到gtest单元测试用例正常的运行起来。

  4. gtest_main.a有什么用?

    But maybe you think that writing all those main() functions is too much work? We agree with you completely and that's why Google Test provides a basic implementation of main(). If it fits your needs, then just link your test with gtest_main library and you are good to go.

    可以不用写自己的main函数,链接 libgtest_main.a 就可以了。

    testAdd.cc

    #include <gtest/gtest.h>
    
    extern int add(int a, int b);
    
    TEST(testCase, test0)
    {
        EXPECT_EQ(add(2, 3), 5);
    }
    

    可以不写main函数。

    编译命令为:

    g++ add.cc testAdd.cc -lgtest -lgtest_main -lpthread

    将libgtest_main.a链接进入程序。

 

g++编译遇到错误

/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
 

$ g++ *.cc -lgtest -lpthread -std=c++11

 

OK
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值