Running “pure” JUnit 4 tests using ant

2019-02-01 20:04发布

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?

标签: ant junit junit4
9条回答
Deceive 欺骗
2楼-- · 2019-02-01 20:38

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

查看更多
够拽才男人
3楼-- · 2019-02-01 20:41

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>
查看更多
女痞
4楼-- · 2019-02-01 20:52

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>
查看更多
5楼-- · 2019-02-01 20:54

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>
查看更多
一纸荒年 Trace。
6楼-- · 2019-02-01 20:54

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

查看更多
爷、活的狠高调
7楼-- · 2019-02-01 20:56

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.

查看更多
登录 后发表回答