Linux下Gtest测试框架应用实例

http://blog.csdn.net/yasaken/article/details/7364862/

Gtest全称: Google C++ Testing Framework
项目链接: http://code.google.com/p/googletest/


Gtest是Google公司发布的一款非常优秀的开源C/C++单元测试框架,已被应用于多个开源项目及Google内部项目中,知名的例子包括ChromeWeb浏览器、LLVM编译器架构、ProtocolBuffers数据交换格式及工具等。至于它的优势,大家可以自己去网上搜索查看,本文主要用一个Demo描述怎么在Linux环境下使用它。


1. 下载SDK
链接:http://code.google.com/p/googletest/
我下载的版本是1.6.0

2. 解压
我解压后的位置是$HOME/bin/gtest-1.6.0

3. 编写测试用例
本例中要测试的是一个求阶乘的函数

函数头文件:func.H

  1. #ifndef FUNC_H  
  2. #define FUNC_H  
  3. int fac(int nInput);  
  4. #endif  

函数实现文件:func.C

  1. #include "func.H"  
  2. int fac(int nInput)  
  3. {  
  4.     if(nInput < 0)  
  5.     {  
  6.         return -1;  
  7.     }  
  8.   
  9.     int nRev = 1;  
  10.     for(int i = 1; i <= nInput; ++i)  
  11.     {  
  12.         nRev *= i;  
  13.     }  
  14.     return nRev;  
  15. }  

主程序文件:主程序文件:fac_test.C

  1. #include <limits>  
  2. #include "func.H"  
  3. #include "gtest/gtest.h"  
  4.   
  5. TEST(Fac_test, input_negative){  
  6.     EXPECT_EQ(-1, fac(-1));  
  7.     EXPECT_EQ(-1, fac(-2));  
  8.     EXPECT_EQ(-1, fac(-5));  
  9. }  
  10.   
  11. TEST(Fac_test, input_zero){  
  12.     EXPECT_EQ(1, fac(0));  
  13. }  
  14.   
  15. TEST(Fac_test, input_positive){  
  16.     EXPECT_EQ(1, fac(1));  
  17.     EXPECT_EQ(2, fac(2));  
  18.     EXPECT_EQ(6, fac(3));  
  19. }  
将这三个文件都放在$/HOME/demo目录下。


4. 修改Makefile文件,然后编译

拷贝Makefile文件
cp $HOME/bin/gtest-1.6.0/make/Makefile   $/HOME/demo/

需要修改的地方 
1. 变量GTEST_DIR   USER_DIR   TESTS 
2. 最后Builds的输入输出

修改后的Makefile:

  1. # Points to the root of Google Test, relative to where this file is.  
  2. # Remember to tweak this if you move this file.  
  3. GTEST_DIR = ../bin/gtest-1.6.0  
  4.   
  5. # Where to find user code.  
  6. USER_DIR = ./  
  7.   
  8. # Flags passed to the preprocessor.  
  9. CPPFLAGS += -I$(GTEST_DIR)/include  
  10.   
  11. # Flags passed to the C++ compiler.  
  12. CXXFLAGS += -g -Wall -Wextra  
  13.   
  14. # All tests produced by this Makefile.  Remember to add new tests you  
  15. # created to the list.  
  16. TESTS = fac_test  
  17.   
  18. # All Google Test headers.  Usually you shouldn't change this  
  19. # definition.  
  20. GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \  
  21.                 $(GTEST_DIR)/include/gtest/internal/*.h  
  22.   
  23. # House-keeping build targets.  
  24.   
  25. all : $(TESTS)  
  26.   
  27. clean :  
  28.     rm -f $(TESTS) gtest.a gtest_main.a *.o  
  29.   
  30. # Builds gtest.a and gtest_main.a.  
  31.   
  32. # Usually you shouldn't tweak such internal variables, indicated by a  
  33. # trailing _.  
  34. GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)  
  35.   
  36. # For simplicity and to avoid depending on Google Test's  
  37. # implementation details, the dependencies specified below are  
  38. # conservative and not optimized.  This is fine as Google Test  
  39. # compiles fast and for ordinary users its source rarely changes.  
  40. gtest-all.o : $(GTEST_SRCS_)  
  41.     $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \  
  42.             $(GTEST_DIR)/src/gtest-all.cc  
  43.   
  44. gtest_main.o : $(GTEST_SRCS_)  
  45.     $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \  
  46.             $(GTEST_DIR)/src/gtest_main.cc  
  47.   
  48. gtest.a : gtest-all.o  
  49.     $(AR) $(ARFLAGS) $@ $^  
  50.   
  51. gtest_main.a : gtest-all.o gtest_main.o  
  52.     $(AR) $(ARFLAGS) $@ $^  
  53.   
  54. # Builds a sample test.  A test should link with either gtest.a or  
  55. # gtest_main.a, depending on whether it defines its own main()  
  56. # function.  
  57.   
  58. func.o : $(USER_DIR)/func.C $(USER_DIR)/func.H $(GTEST_HEADERS)  
  59.     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/func.C  
  60.   
  61. fac_test.o : $(USER_DIR)/fac_test.C $(USER_DIR)/func.H $(GTEST_HEADERS)  
  62.     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/fac_test.C  
  63.   
  64. fac_test : func.o fac_test.o gtest_main.a  
  65.     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@  

然后make,生成测试用例fac_test


5. 运行,检查运行结果












Ok,看到这儿,你已经可以把Gtest玩起来了,至于高级的用法,快去查Google的文档吧。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值