Ant、JUnit 和 TestSuite

发布于 2024-11-27 08:22:28 字数 2085 浏览 0 评论 0原文

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

buildfile:

<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

我的问题是 - 我需要在构建脚本中更改什么才能使 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
    }
}

So I'm setting up automatic regression testing with JUnit, and right now the build script is set up to call a TestSuite, which packs a bunch of different tests into a TestSuite and returns it.

buildfile:

<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;
    }
}

Output:

[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

My question is - what do I need to change in the buildscript to make ant run the tests properly?

Edit:

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-12-04 08:22:28

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

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

From the junit task documentation, I think the test attribute has to be used with a class that contains a single Test (rather than a Suite). Maybe you can use a pattern to ask ant to run every test in a given package, like this :

  <batchtest fork="yes" todir="${reports.tests}">
   <fileset dir="${src.tests}">
     <include name="**/*Test*.java"/>
     <exclude name="**/AllTests.java"/>
   </fileset>
  </batchtest>
灯角 2024-12-04 08:22:28

当使用 TestSuite 时,您一次向套件添加一个测试用例,您的语法应该看起来更像这样:

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

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

When using a TestSuite you add test cases to your suite one testcase at a time, your syntax should look more like this:

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

Basically you aren't passing the name of a test to run and so it tries to run "null" and fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文