【问题标题】:Ant, JUnit, and a TestSuiteAnt、JUnit 和一个测试套件
【发布时间】:2011-10-29 05:40:52
【问题描述】:

所以我正在使用 JUnit 设置自动回归测试,现在构建脚本被设置为调用一个 TestSuite,它将一堆不同的测试打包到一个 TestSuite 中并返回它。

构建文件:

<target name="test-perform-check" depends="test-compile">
        <junit printsummary="yes" fork="yes" haltonfailure="no">
            <classpath path ="${mypath}"  />
            <jvmarg value="-Djava.ext.dirs=${extstar};${extpathextended};" />
                    <jvmarg value="-Dmipav=${mipav};" />
            <sysproperty key="mipav" value="${mipav}"/>
           <formatter type="xml"/>
           <formatter type="plain" usefile="false"/>
           <test name="test.JTest"/>
        </junit>
    </target>

JTest.java:

 class JTest extends TestSuite {

    public static Test suite () {
        // set up a bunch of stuff
        TestSuite suite = new TestSuite();
        suite.addTest(new VolumeCompare());
        suite.addTest(new VolumeCompare());
        suite.addTest(new VolumeCompare());
        suite.addTest(new FileExistence());
        // do some other stuff
        return suite;
    }
}

输出:

[junit] Testcase: null took 0.002 sec
[junit]     FAILED
[junit] null
[junit] junit.framework.AssertionFailedError
[junit] 
[junit] Testcase: null took 0 sec
[junit]     FAILED
[junit] null
[junit] junit.framework.AssertionFailedError
[junit] 
[junit] Testcase: null took 0.002 sec
[junit]     FAILED
[junit] null
[junit] junit.framework.AssertionFailedError
[junit] 
[junit] Testcase: null took 0 sec
[junit]     FAILED
[junit] null
[junit] junit.framework.AssertionFailedError
[junit] 
[junit] Test test.JTest FAILED

我的问题是 - 我需要在 buildscript 中进行哪些更改才能使 ant 正确运行测试?

编辑:

VolumeCompare.java:

public class VolumeCompare extends TestCase {
    public VolumeCompare (...) {
        // ctor
    }
    @Test
    public void testVolume () {
        // this print statement is never reached
        System.out.println("testing volume");
        // compare volumes here
    }
}

【问题讨论】:

  • 您可以发布您的VolumeCompare.java 文件吗?
  • 已发布。还在摸索,如果我能解决它,我会把它扔在那里。

标签: java ant junit


【解决方案1】:

junit task documentation 开始,我认为 test 属性必须与包含单个测试(而不是套件)的类一起使用。也许你可以使用一种模式来要求 ant 运行给定包中的每个测试,如下所示:

  <batchtest fork="yes" todir="${reports.tests}">
   <fileset dir="${src.tests}">
     <include name="**/*Test*.java"/>
     <exclude name="**/AllTests.java"/>
   </fileset>
  </batchtest>

【讨论】:

  • 是的,但我不希望 ant 立即运行所有测试。在运行测试之前我有很多事情要做(处理大量数字),并且在完成之前我不想返回 TestSuite。
  • 哦,对,这就是您所说的“设置一堆东西”的意思,抱歉。你总是可以选择设置一个蚂蚁任务来做这个数字处理,然后让你的测试任务依赖这个数字处理任务——当然这取决于测试类如何使用这个数字处理......你通过了吗数据到您的测试类 ctor ?还是存储在 db/file 中?
  • 好的,所以我发现其他文章的做法和你差不多 (opensource-soa.blogspot.com/2008/07/…)。所以也许问题实际上出在你的测试本身 - 但很难用你发布的内容来判断......
【解决方案2】:

使用 TestSuite 时,您一次将一个测试用例添加到您的套件中,您的语法应该更像这样:

suite.addTest(new VolumeCompare("testCase1"));
suite.addTest(new VolumeCompare("testCase2"));
suite.addTest(new VolumeCompare("testCase3"));

基本上,您没有传递要运行的测试名称,因此它尝试运行“null”并失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    相关资源
    最近更新 更多