手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下Gtest测试框架应用实例

[日期:2012-03-19] 来源:Linux社区  作者:yasaken [字体:大 中 小]

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的文档吧。

linux
相关资讯       C++  Gtest 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款