gtest入门

来源:互联网 发布:dnf网络中断怎么解决 编辑:程序博客网 时间:2024/05/21 02:19


转自: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

[cpp] view plaincopyprint?
  1. #ifndef FUNC_H  
  2. #define FUNC_H  
  3. int fac(int nInput);  
  4. #endif  

函数实现文件:func.C

[cpp] view plaincopyprint?
  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

[cpp] view plaincopyprint?
  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:

[cpp] view plaincopyprint?
  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的文档吧。

结束语
想了解更详细的关于gtest的信息,见官网:
http://code.google.com/p/googletest/


 进阶传送门


http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html

0 0
原创粉丝点击