• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Howto run specific JUnit tests in ant?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks,
Alan
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alan,
You could write a suite() method in Java and have Ant call that.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Hello.java: Note that it is in the hello package, in the src directory.
$ pwd
/home/ksb/hello
$ mkdir -p src/hello
$ cat src/hello/Hello.java


(This could be simpler but the greet method is used later when testing with JUnit.) To compile and run:
$ javac src/hello/Hello.java
$ java -cp src hello.Hello
Hello, world
Ant
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

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

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

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

BUILD SUCCESSFUL
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

init:

build:

dist:

run:
[java] Hello, world

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


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

init:

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

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


BUILD SUCCESSFUL
Total time: 2 seconds
Modify Hello.java 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

BUILD SUCCESSFUL
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):
$ cruisecontrol.sh
[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
 
reply
    Bookmark Topic Watch Topic
  • New Topic