Unit testing 使用makefile进行单元测试
我想做以下工作:Unit testing 使用makefile进行单元测试,unit-testing,makefile,Unit Testing,Makefile,我想做以下工作: start: @echo start the server stop: @echo stop the server test1: start stop @echo do test1 test2: start stop @echo do test2 test-all: test1 test2 但我有两个问题: 如何在makefiles中指定pre-和post规则?所有规则似乎都是makefile中的pre-。在这种情况下,我希望在每个测试
start:
@echo start the server
stop:
@echo stop the server
test1: start stop
@echo do test1
test2: start stop
@echo do test2
test-all: test1 test2
但我有两个问题:
pre-
和post规则?所有规则似乎都是makefile中的pre-
。在这种情况下,我希望在每个测试结束时运行stop
开始
和停止
规则,即使它已经运行了规则?每个单独的测试都需要进行启动/停止循环,这样当我运行make test all
时,每个测试都会调用start
和stop
,而不是第一个需要它们的测试只调用一次谢谢你的建议。是的,我有很多类似的测试。我不喜欢这样一个事实,即它没有使用依赖系统,并且它使整个内容难以阅读。这里的依赖关系在哪里?它们是
开始
(pre)和停止
(post),但从您使用的语法(罐装配方
?)来看,这一点并不清楚。我希望有一个简单的操作符来指定这是一个必须在规则完成时运行的依赖项,另一个操作符说总是运行这个依赖项,即使它刚刚运行了一会儿,比如:test1:+start+@stop
,其中操作符@
表示在规则结束时运行此依赖项,操作符+
表示每次都运行此依赖项。我刚刚编写了这些运算符,但类似的东西会使我的测试非常可读。@jeckyll2hide:make的依赖项处理不是该项工作的合适工具,因为它根本不能以这种方式工作。在Make中还有其他方法可以做到这一点,但是如果可读性是你想要的,我认为它们不会有太大的改进(除非这个方案由于单元测试方法的复杂性而不可读)。谢谢你的评论。如果可以使用类似bash的测试描述语法,那么您还可以使用什么工具来利用单元测试?@jeckyll2hide:看起来很有趣,但是如果不适合,我会用我最喜欢的脚本语言编写解析器。
test1:
@echo start the server
@echo do test1
@echo stop the server
test2:
@echo start the server
@echo do test2
@echo stop the server
test-all: test1 test2
define start-stop
@echo start the server
$(1)
@echo stop the server
endef
test1:
$(call start-stop, @echo do test 1)
test2:
$(call start-stop, @echo do test 2)