gtest

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. }  
  1. TEST(Fac_test, input_zero){  
  2.     EXPECT_EQ(1, fac(0));  
  3. }  
  4.   
  5. TEST(Fac_test, input_positive){  
  6.     EXPECT_EQ(1, fac(1));  
  7.     EXPECT_EQ(2, fac(2));  
  8.     EXPECT_EQ(6, fac(3));  
  9. }  

将这三个文件都放在$/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 :  
  1.  rm -f $(TESTS) gtest.a gtest_main.a *.o  
  2.   
  3. # Builds gtest.a and gtest_main.a.   
  4.   
  5. # Usually you shouldn't tweak such internal variables, indicated by a   
  6. # trailing _.   
  7. GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)  
  8.   
  9. # For simplicity and to avoid depending on Google Test's   
  10. # implementation details, the dependencies specified below are   
  11. # conservative and not optimized.  This is fine as Google Test   
  12. # compiles fast and for ordinary users its source rarely changes.   
  13. gtest-all.o : $(GTEST_SRCS_)  
  14.     $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \  
  15.             $(GTEST_DIR)/src/gtest-all.cc  
  16.   
  17. gtest_main.o : $(GTEST_SRCS_)  
  18.     $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \  
  19.             $(GTEST_DIR)/src/gtest_main.cc  
  20.   
  21. gtest.a : gtest-all.o  
  22.     $(AR) $(ARFLAGS) $@ $^  
  23.   
  24. gtest_main.a : gtest-all.o gtest_main.o  
  25.     $(AR) $(ARFLAGS) $@ $^  
  26.   
  27. # Builds a sample test.  A test should link with either gtest.a or   
  28. # gtest_main.a, depending on whether it defines its own main()   
  29. # function.   
  1. func.o : $(USER_DIR)/func.C $(USER_DIR)/func.H $(GTEST_HEADERS)  
  2.     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/func.C  
  3.   
  4. fac_test.o : $(USER_DIR)/fac_test.C $(USER_DIR)/func.H $(GTEST_HEADERS)  
  5.     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/fac_test.C  
  6.   
  7. fac_test : func.o fac_test.o gtest_main.a  
  8.     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@  

然后make,生成测试用例fac_test

5. 运行,检查运行结果



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值