运行“纯粹” 使用 ant 进行 JUnit 4 测试

发布于 2024-07-15 19:20:05 字数 405 浏览 6 评论 0原文

我们已迁移到 JUnit 4 和 ant 1.7

测试在 eclipse 中运行良好,但使用 ant 运行测试时会忽略注释。

根据Ant junit任务文档

它还适用于 JUnit 4.0,包括仅使用注释而不使用 JUnit4TestAdapter 的“纯”JUnit 4 测试。

但文档没有详细说明应该如何配置。

junit任务需要什么特殊设置吗? 我错过了什么吗? 我们有扩展 TestCase(即 3.8 风格)的测试和“纯”Junit 4 测试,这可能是问题所在吗?

We have migrated to both JUnit 4 and ant 1.7

The tests runs fine in eclipse, but the annotations are ignored when running the tests using ant.

According to the Ant junit task documentation:

It also works with JUnit 4.0, including "pure" JUnit 4 tests using only annotations and no JUnit4TestAdapter.

But the documentation doesn't elaborate on how it should be configured.

Is there any special setting required for the junit task? Am I missing something?
We have both Tests that extends TestCase (i.e. 3.8 style) and "pure" Junit 4 tests, could that be the problem?

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

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

发布评论

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

评论(9

我不在是我 2024-07-22 19:20:06

将此注释应用于其他类 org.junit.Ignore

Apply this annotation to the other classes org.junit.Ignore

薯片软お妹 2024-07-22 19:20:06

我还尝试在没有 JUnit4TestAdapter 的情况下使用 JUnit 4.0 进行测试,即没有方法
公共静态junit.framework.测试套件(){
返回新的 JUnit4TestAdapter(SomeTestClass.class);

使用ant 1.9.4。
运行 ant test verbose (ant -v ) 显示

[junit] 在同一虚拟机中运行多个测试
[junit] 隐式添加 /usr/share/java/junit.jar:/usr/sharejava/ant-launcher.jar:/usr/share/java/ant.jar:/usr/share/java/ant/ant-junit .jar 到 CLASSPATH

啊哈,但仍然有一些 ant-junit-task。
另外下载这个节目
/usr/share/java/ant/ant-junit4.jar 不是隐式添加的。
我只是明确地添加了它:

<junit printsummary="yes" 
    fork="yes" 
    forkmode="once"
    maxmemory="1023m" 
    showoutput="no">
    ...
   <classpath>
     <pathelement path="...:${junitJar}:${hamcrestJar}:/usr/share/java/ant/ant-junit4.jar" />
   </classpath>
...
</junit>

并且它起作用了。 无:没有。
我知道这个解决方案一点也不漂亮......

I also tried to do tests with JUnit 4.0 without JUnit4TestAdapter, i.e. without method
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(SomeTestClass.class);
}

I use ant 1.9.4.
Running ant test verbose (ant -v ) shows

[junit] Running multiple tests in the same VM
[junit] Implicitly adding /usr/share/java/junit.jar:/usr/sharejava/ant-launcher.jar:/usr/share/java/ant.jar:/usr/share/java/ant/ant-junit.jar to CLASSPATH

Aha, but still there is some ant-junit-task.
Downloading this shows in addition
/usr/share/java/ant/ant-junit4.jar which is not added implicitly.
I just added it explicitly:

<junit printsummary="yes" 
    fork="yes" 
    forkmode="once"
    maxmemory="1023m" 
    showoutput="no">
    ...
   <classpath>
     <pathelement path="...:${junitJar}:${hamcrestJar}:/usr/share/java/ant/ant-junit4.jar" />
   </classpath>
...
</junit>

and it worked. Without: no.
I am aware that this solution is not beautiful at all...

红ご颜醉 2024-07-22 19:20:06

我最终所做的是将 Ant 添加到任务使用的定义之一>。 等等瞧。

What I ended up doing was adding an Ant to one of my definitions that is used by the task>. Et voila.

瑕疵 2024-07-22 19:20:05

我正在使用纯 JUnit4 测试和 Ant。

这是我的构建文件中有趣的部分:

<junit printsummary="yes" haltonfailure="yes">
    <formatter type="xml"/>
    <classpath refid="path.test"/>
    <batchtest fork="yes" todir="${dir.report.unittests.xml}">
        <fileset dir="src">
            <include name="**/*Test*.java"/>
        </fileset>
    </batchtest>
</junit>

确保 Ant 的 lib 目录中有最新版本的 junit.jar 文件。 据我所知,所需的版本是与 ant 1.7 或更高版本一起提供的......

I am using pure JUnit4 tests with Ant.

Here is the interesting part of my build file:

<junit printsummary="yes" haltonfailure="yes">
    <formatter type="xml"/>
    <classpath refid="path.test"/>
    <batchtest fork="yes" todir="${dir.report.unittests.xml}">
        <fileset dir="src">
            <include name="**/*Test*.java"/>
        </fileset>
    </batchtest>
</junit>

Make sure you have the latest version of the junit.jar file in the lib directory of Ant. As far as I know the required version is delivered with ant 1.7 or higher versions...

天邊彩虹 2024-07-22 19:20:05

Ant 默认情况下附带 JUnit 3 版本。 JUnit 3 不支持测试注释。

要使用 junit 任务中的 JUnit 4 注释,请确保在 junit 任务的嵌套类路径元素中提供 JUnit 4 jar 的位置(请参阅 ant FAQ 中的此条目)。

<junit showoutput="yes" fork="true">
    <classpath>
        <!-- The location of the JUnit version that you want to use -->
        <pathelement location="lib/junit-4.9b1.jar"/>
    </classpath>

    <formatter type="plain" usefile="false" />

    <batchtest>
        <fileset dir="${tests.dir}"/>
    </batchtest>
</junit>

这是覆盖 ANT_HOME/lib 中的 ant-junit.jar 的更好解决方案,因为这意味着您可以将 JUnit jar 与代码一起保留在源代码管理中,从而可以直接升级到更高版本。

请注意,虽然我没有在上面的文件集中指定任何包含模式,但这确实意味着 junit 任务将尝试针对该目录结构中的所有类运行 JUnit,这可能会导致包含许多不包含的类任何测试取决于您如何构建源文件。

Ant ships with a version of JUnit 3 by default. JUnit 3 has no support for test annotations.

To use the JUnit 4 annotations from the junit task make sure that you provide the location of a JUnit 4 jar in a nested classpath element of the junit task (see this entry in the ant FAQ).

<junit showoutput="yes" fork="true">
    <classpath>
        <!-- The location of the JUnit version that you want to use -->
        <pathelement location="lib/junit-4.9b1.jar"/>
    </classpath>

    <formatter type="plain" usefile="false" />

    <batchtest>
        <fileset dir="${tests.dir}"/>
    </batchtest>
</junit>

This is a preferable solution to overwriting the ant-junit.jar in ANT_HOME/lib as it means you can keep your JUnit jar in source control alongside your code making upgrades to later versions straightforward.

Note that whilst I haven't specified any include pattern in my fileset above this does mean that the junit task will attempt to run JUnit against all the classes in that directory structure which might result in a number of classes being included that don't contain any tests depending on how you have structured your source files.

魔法唧唧 2024-07-22 19:20:05

您终于只能使用 ant 1.9.3+ 中添加的 skipNonTests 参数来查找并执行测试!

这是上面接受的答案中的代码片段(除了新的 skipNonTests 参数并删除文件名要求中的“Test”):

<junit printsummary="yes" haltonfailure="yes">
    <formatter type="xml"/>
    <classpath refid="path.test"/>
    <batchtest skipNonTests="true" fork="yes" todir="${dir.report.unittests.xml}">
        <fileset dir="src">
            <include name="**/*.java"/>
        </fileset>
    </batchtest>
</junit>

You can finally only find and execute tests with the skipNonTests parameter added in ant 1.9.3+!

This is the code snippet from the accepted answer above (except for the new skipNonTests parameter and getting rid of the "Test" in the filename requirement):

<junit printsummary="yes" haltonfailure="yes">
    <formatter type="xml"/>
    <classpath refid="path.test"/>
    <batchtest skipNonTests="true" fork="yes" todir="${dir.report.unittests.xml}">
        <fileset dir="src">
            <include name="**/*.java"/>
        </fileset>
    </batchtest>
</junit>
<逆流佳人身旁 2024-07-22 19:20:05

这发生在我身上,因为我既使用了注释又扩展了 TestCase。

public class TestXXX extends TestCase {

    @Test
    public void testSimpleValidCase() {
        // this was running
    }

    @Test
    public void simpleValidCase() {
        // this wasn't running
    }
}

当您扩展 TestCase 时,您将采用 JUnit3 样式,因此 JUnit4 注释将被忽略。

解决方案是停止扩展 TestCase。

This happened to me and it was because I was both using annotations and extending TestCase.

public class TestXXX extends TestCase {

    @Test
    public void testSimpleValidCase() {
        // this was running
    }

    @Test
    public void simpleValidCase() {
        // this wasn't running
    }
}

When you extend TestCase you are assuming JUnit3 style so JUnit4 annotations are ignored.

The solution is to stop extending TestCase.

浴红衣 2024-07-22 19:20:05

验证您的类路径定义...
这解决了我的问题。

<path id="classpath" description="Classpath do Projeto">
    <fileset dir="${LIB}">
        <include name="**/*.jar" />
        <exclude name="**/.SVN/*.*"/>
    </fileset>
</path>

Verify your classpath definition...
this solved my problem.

<path id="classpath" description="Classpath do Projeto">
    <fileset dir="${LIB}">
        <include name="**/*.jar" />
        <exclude name="**/.SVN/*.*"/>
    </fileset>
</path>
感性不性感 2024-07-22 19:20:05

这是我的通用蚂蚁脚本的相关部分......不确定这是否会对你有帮助......

 <junit fork="true"
        forkmode="once"
        haltonfailure="false"
        haltonerror="false"
        failureproperty="tests.failures"
        errorproperty="tests.errors"
        includeantruntime="true"
        showoutput="true"
        printsummary="true">
     <classpath>
         <path refid="path-id.test.classpath.run"/>
     </classpath>

     <formatter type="xml"/>

     <batchtest fork="yes"
                todir="${dir.build.testresults}">
         <fileset dir="${dir.src.tests}">
             <include name="**/*Test.java"/>
         </fileset>
     </batchtest>
 </junit>

This is the relevant part of my generic ant script... not sure if that'll help you or not..

 <junit fork="true"
        forkmode="once"
        haltonfailure="false"
        haltonerror="false"
        failureproperty="tests.failures"
        errorproperty="tests.errors"
        includeantruntime="true"
        showoutput="true"
        printsummary="true">
     <classpath>
         <path refid="path-id.test.classpath.run"/>
     </classpath>

     <formatter type="xml"/>

     <batchtest fork="yes"
                todir="${dir.build.testresults}">
         <fileset dir="${dir.src.tests}">
             <include name="**/*Test.java"/>
         </fileset>
     </batchtest>
 </junit>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文