gtest是google提供的一个非常强大的单元测试工具,下载地址:https://code.google.com/p/googletest
我下载的是gtest-1.6.0.拷贝到Centos系统上面。参考:http://blog.csdn.net/butterflydog/article/details/7005045
配置过程如下:
1、解压gtest-1.6.0
2、查看文件内容,找到make文件,进行make,生成一个测试程序,包含gtest_main.a文件
3、测试程序运行如下:
4、新建一个文件夹,gtest_program,将gtest-1.6.0中的include文件拷过来。
5、在gtest_program中新建一个lib文件夹,将gtest-1.60中的make文件夹中新生成的gtest_main.a文件拷贝过来。
6、编写Makefile,一定要记得修改GTEST_DIR为自己的路径名。如下:
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 = /home/anker/gtest_program 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 Google Test headers. Usually you shouldn't change this 15 # definition. 16 GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ 17 $(GTEST_DIR)/include/gtest/internal/*.h 18 19 20 21 FINALOBJS = $(patsubst ./%.cpp, ./%.o, $(wildcard ./*.cpp)) 22 FINALOBJS += $(patsubst ./%.cc, ./%.o, $(wildcard ./*.cc)) 23 24 MODULE=Sample 25 26 TEST=${MODULE}UnitTest 27 #if there are any modules that you mocked, add their obj name to MOCKOBJS, so 28 #they can be rebuilt 29 #MOCKOBJS += $(TEST) $(BASEDIR) 30 # House-keeping build targets. 31 32 all : $(TEST) 33 34 $(TEST): MOCK $(FINALOBJS) 35 $(CXX) $(CXXFLAGS) -lpthread $(FINALOBJS) -o $@ $(GTEST_DIR)/lib/gtest_main.a 36 37 %.o:%.cpp 38 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -g -c -o $@ {1}lt; 39 MOCK: 40 rm -rf $(MOCKOBJS) 41 clean: 42 rm -f $(FINALOBJS) $(TEST) 43
7、测试结果如下:
参考:http://www.cnblogs.com/chutianyao/archive/2012/12/01.html