【问题标题】:Running multiple JUnit Tests with ANT for Regression and Granularity使用 ANT 运行多个 JUnit 测试以实现回归和粒度
【发布时间】: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 测试套件。这种设置允许在调用构建时运行所有测试,但这并不适合日常开发。

我希望能够做两件事:

  1. 在构建中运行所有测试(如上图所示)
  2. 从 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


    【解决方案1】:

    这样的?

    <target name="unittest-p1"></target>
    <target name="unittest-p2"></target>
    <target name="unittest-p3"></target>
    
    <target name="unittest" depends="unittest-p1, unittest-p2, unittest-p3/>
    

    然后您可以通过传递 unittest 目标来运行所有测试:

    ant unittest
    

    (或任何依赖于 unittest 的目标)

    您可以通过调用适当的目标来运行任何单独的测试集,例如

    ant unittest-p1
    

    如果您最终会在多个 junit 目标中出现大量重复,您可以通过将所有常见的东西放入 macrodef 来整理它。

    【讨论】:

    • ant unittest-p1 将只运行第一组而不使用属性。
    • 在避免属性方面似乎是一个更好的解决方案,但这确实意味着构建脚本中有更多的junit任务和很多重复的东西,但它确实提供了很多灵活性。我会试一试,看看我的进展如何。
    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多