It's not a secret anymore!
The moose likes Testing and the fly likes Howto run specific JUnit tests in ant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Howto run specific JUnit tests in ant?" Watch "Howto run specific JUnit tests in ant?" New topic

Howto run specific JUnit tests in ant?

Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
Say I have a test class "AlanTest".. which has 5 test methods.
I'd like to be able to run a specific test method or list of
test methods from ant.

This seems not to be possible at this point.

Has anyone any ideas/come up with a solution to this before?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32302

You could write a suite() method in Java and have Ant call that.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Here is a SAMPLE:

Java, Ant, JUnit, CruiseControl 'Hello, world' example

Below is 'Hello, world' illustrating Java, Ant, JUnit and CruiseControl. Not the simplest examples but an attempt to demonstrate the basics of each tool.
Java Note that it is in the hello package, in the src directory.
$ pwd
$ mkdir -p src/hello
$ cat src/hello/

(This could be simpler but the greet method is used later when testing with JUnit.) To compile and run:
$ javac src/hello/
$ java -cp src hello.Hello
Hello, world
Now write a build.xml file:
$ cat build.xml

Note that this not only compiles with the javac task but also creates a jar file with the jar task. There is also a run target using the java task for executing the jar file.
$ ant
Buildfile: build.xml

[mkdir] Created dir: /home/ksb/hello/build
[mkdir] Created dir: /home/ksb/hello/dist

[javac] Compiling 2 source files to /home/ksb/hello/build

[jar] Building jar: /home/ksb/hello/dist/hello.jar

Total time: 2 seconds
Because we now have a jar file, created with the manifest target, we can now run the program with the java -jar flag:
$ java -jar dist/hello.jar
Hello, world
Or use the run target:
$ ant run
Buildfile: build.xml




[java] Hello, world

Total time: 1 second
First create a unit test: I'm putting this in the same dir as (so it will be compiled along with, though it could live somewhere else.
$ cat src/hello/

Now add the JUnit parts to the build.xml file, which now looks like (modified parts in bold):
$ cat build.xml

This adds the test target using the junit task and directs xml formatted output of JUnit into a junit-results dir which will later be read by CruiseControl.
Before you will be able to run the new test target, junit.jar must be available to ant in order to understand the junit task. This can be done by either adding it to your $CLASSPATH (before ant is run) or make junit.jar appear in ant's lib dir. I've done the latter via a symlink by making /usr/local/ant/lib/junit.jar -> /usr/local/share/java/classes/junit.jar.
So, now using the new test target:
$ ant test
Buildfile: build.xml


[javac] Compiling 1 source file to /home/ksb/hello/build

[junit] Testsuite: hello.TestHello
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.006 sec

Total time: 2 seconds
Modify to break the test and run ant test again to convince yourself that it is indeed testing the class.
Note also that there is now a junit-results/TEST-hello.TestHello.xml file holding the results of JUnit in xml.
Set up perforce and CruiseControl dirs
This is already done, as part of SCGQA Development process.
Delegate ant build file
Back in the cc dir (where CruiseControl will run) write a wrapper build-hello.xml file which CruiseControl will use to build and rebuild the hello project:
$ cd ..
$ cat build-hello.xml

And test this by running it directly in ant:
$ ant -f build-hello.xml

Total time: 0 second
Note, by running it again, that this will start from a completely clean slate, removing the hello dir, checking it out fresh from perforce, then running the unit test.
CruiseControl config file
Now write the CruiseControl config file config.xml:
$ cat config.xml

Briefly, and in order, this configures CruiseControl to:
1.Have one project named hello which won't be rebuilt after a failure unless new changes are ready in perforce.
2.Log beginning of build loop to logs/hello/buildstatus.txt.
3.Every 30 seconds do a build (run 'ant -f build-hello.xml build').
4.Wait for perforce project in checkout/hello to be idle for 10 seconds before building (to avoid perforce commit race conditions).
5.Log to logs/hello, merging in JUnit results.
6.Log end of build loop to logs/hello/buildstatus.txt and send out HTML formatted email about results of each build.
Run CruiseControl
Now start CruiseControl (assuming that it is on your ${PATH} and executable):
[long output deleted...]
You should see CruiseControl doing it's thing: building the hello project and sending out emails. Try checking out the hello project (not the one CruiseControl is using) and try
subject: Howto run specific JUnit tests in ant?
It's not a secret anymore!