Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running "pure" JUnit 4 tests using ant

Tags:

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?

like image 439
LiorH Avatar asked Mar 11 '09 17:03

LiorH


People also ask

Can JUnit be integrated with ant?

Learn Maven and Ant the easy way! There are a number of JUnit extensions available. If you are unfamiliar with JUnit, you should download it from www.junit.org and read its manual. This chapter shows how to execute JUnit tests by using Ant. The use of Ant makes it straight forward through the JUnit task.

What is ant JUnit?

This task is used to run tests from the JUnit testing framework. This task is depend on external libraries that are not included in Apache Ant distribution by default. The junit. jar and ant.


2 Answers

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

like image 96
Homes2001 Avatar answered Oct 05 '22 23:10

Homes2001


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.

like image 27
MikeD Avatar answered Oct 06 '22 00:10

MikeD