Linux下Gtest测试框架应用实例 .

Linux下Gtest测试框架应用实例

分类: Linux/Unix C++ 1468人阅读 评论(1) 收藏 举报
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  
#ifndef FUNC_H
#define FUNC_H
int fac(int nInput);
#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. }  
#include "func.H"
int fac(int nInput)
{
	if(nInput < 0)
	{
		return -1;
	}

	int nRev = 1;
	for(int i = 1; i <= nInput; ++i)
	{
		nRev *= i;
	}
	return nRev;
}

主程序文件:主程序文件: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. }  
#include <limits>
#include "func.H"
#include "gtest/gtest.h"

TEST(Fac_test, input_negative){
	EXPECT_EQ(-1, fac(-1));
	EXPECT_EQ(-1, fac(-2));
	EXPECT_EQ(-1, fac(-5));
}

TEST(Fac_test, input_zero){
	EXPECT_EQ(1, fac(0));
}

TEST(Fac_test, input_positive){
	EXPECT_EQ(1, fac(1));
	EXPECT_EQ(2, fac(2));
	EXPECT_EQ(6, fac(3));
}
将这三个文件都放在$/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 $@  
# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ../bin/gtest-1.6.0

# Where to find user code.
USER_DIR = ./

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = fac_test

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.

all : $(TESTS)

clean :
	rm -f $(TESTS) gtest.a gtest_main.a *.o

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
	$(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
	$(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

func.o : $(USER_DIR)/func.C $(USER_DIR)/func.H $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/func.C

fac_test.o : $(USER_DIR)/fac_test.C $(USER_DIR)/func.H $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/fac_test.C

fac_test : func.o fac_test.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

然后make,生成测试用例fac_test


5. 运行,检查运行结果












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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值