How to write the better Makefile for cpputest


最近给其他几个团队培训UT工具cpputest。自己没有写PPT,而是从网上找了一份比较不错的CppUTest课件,这份课件来自于一家叫Odd-e的敏捷咨询公司。

里面的内容比较基础,也很清晰,非常适合帮助初学者较快的入门。

由于我之前一直是在Windows下用VS使用这款UT工具,Linux下的实践仅仅限于在学习cpputest源码中的过程中,利用工具附带的example工程上增加修改一些单元测试。

当有同学按照PPT上的简单实例,在Linux下尝试时,发现PPT上的Makefile根本就编译不过。我尝试之后确实。

其实原因也很清楚,因为编译需要包含的cpputest的引用关系比较复杂,并不是在PPT中那么简单,下面贴出PPT中的Quick Start代码:

单元测试代码如下:










按照上述的Makefile是不行的,如果自己编写一个可行的Makefile对于一个Makefile功力不够的初学者是很难的。不过,幸好cpputest工具的发布者随代码提供了一份可用的Makefile。

如果你使用这个工具之前先研究过附带的examples例子的话,会发现这个例子的Makefile是引用了build目录下的MakefileWorker.mk来完成整个编译、链接和执行的。

下面贴出这份MakefileWorker的开头注释部分:

# Include this helper file in your makefile
# It makes
#    A static library
#    A test executable
#
# See this example for parameter settings 
#    examples/Makefile
#
#----------
# Inputs - these variables describe what to build
#
#   INCLUDE_DIRS - Directories used to search for include files.
#                   This generates a -I for each directory
# SRC_DIRS - Directories containing source file to built into the library
#   SRC_FILES - Specific source files to build into library. Helpful when not all code 
# in a directory can be built for test (hopefully a temporary situation)
# TEST_SRC_DIRS - Directories containing unit test code build into the unit test runner
# These do not go in a library. They are explicitly included in the test runner
# TEST_SRC_FILES - Specific source files to build into the unit test runner
# These do not go in a library. They are explicitly included in the test runner
# MOCKS_SRC_DIRS - Directories containing mock source files to build into the test runner
# These do not go in a library. They are explicitly included in the test runner
#----------


MakefileWorker.mk对外提供了很好的接口,只要你自己编写一个简单的Makefile把需要传入的参数定义并传入就OK了。


同样是上面的那个例子,重新编写Makefile如下:
我的UT代码所在的文件夹Ut_Test和cpputest的根目录同级。UT代码同PPT中的代码。

#--- Inputs ----#
COMPONENT_NAME = Ut_Test
CPPUTEST_HOME = ../cpputest-3.4

CPPUTEST_USE_EXTENSIONS = Y
CPP_PLATFORM = Gcc

SRC_DIRS = 

TEST_SRC_DIRS = .

INCLUDE_DIRS =\
  .\
  $(CPPUTEST_HOME)/include\

include $(CPPUTEST_HOME)/build/MakefileWorker.mk

运行结果:




如果你不希望看到如此多的编译细节,可以在Makefile中增加以下定义:
#Set this to @ to keep the makefile quiet
ifndef SILENCE
SILENCE = @
endif

运行结果:



至此,相信你应该可以很easy的编写cpputest的Makefile了吧!






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章