【发布时间】:2012-01-02 23:56:36
【问题描述】:
我正在尝试设置一个 ANT 构建脚本来编译代码、编译测试、运行单元测试然后构建。这些都是通过具有依赖关系的单独目标完成的,即
<target name="compile">
<javac>...
</target>
<target name="compile-tests" depends="compile">
<javac>...
</target>
<target name="unittest" depends="compile-tests">
<junit...
<test ...
<fail if="tests.failed" ..
</target>
<target name="build" depends="compile, unittest">
</target>
“junit”任务中的每个“测试”都专注于应用程序的一部分(通常是逐个包)并指向一个 Junit 测试套件。这种设置允许在调用构建时运行所有测试,但这并不适合日常开发。
我希望能够做两件事:
- 在构建中运行所有测试(如上图所示)
- 从 ant 单独运行测试
我对 (2) 的解决方案是使用多个 antcall 任务,这并不是最佳实践。在这些调用期间,设置了不同的属性来运行所有测试,因为它们每个都需要不同的属性:
<!-- test package p2 with ant unittest -Dtest.p2=true -->
<target name="unittest" depends="compile-tests">
<junit...
<test if="test.p1" ...
<test if="test.p2"
<fail if="tests.failed" ..
</target>
<target name="unittestall">
<property name="test.p1" value="true"/>
...
</target>
<target name="build" depends="compile, unittest">
<antcall target="unittestall" />
<antcall target="clean" />
<antcall target="compile" />
</target>
这提供了我需要的粒度,但意味着大量工作被重复,并且 ant 的依赖特性没有被充分利用。
所以我的问题是: 如何最好地设置 ANT 和 Junit,以便所有测试都可以作为构建的一部分运行,并且可以运行单个测试?
谢谢你:)
来自约书亚英格兰
附言ANT 1.8 和 Junit 4.10 :)
【问题讨论】:
标签: unit-testing ant build junit regression