Solved

How do I capture and save the results of a JUnit 4 test suite run?

Posted on 2007-12-03
3
2,307 Views
Last Modified: 2012-08-13
Hello, I have created a test suite using JUnit 4.  It uses JUnit 4 annotations so it is essentially an empty class that looks like this:

package mypackage;

import org.junit.runners.Suite;

@Suite.SuiteClasses(value={TesClass1.class,TestClass2.class})
public class MySuite
{
}

I'm developing in Eclipse and when I execute this as a JUnit test everything works.  The only problem is that ultimately I want this to run in an automated environment after installation on a machine where there will be no eclipse set up.  Essentially I want to wrap the execution of this test suite in a script (possibly using Ant, not sure yet).  Further, I want to capture the results of each test and store them in an HTML file that I can then look at the results using a browser.

Question: how can I capture the results of the tests in the suite when my test suite class does not even have a body to it?  

I looked into writing a class that extended Runner but without much success.  It seems that I am missing something.  Has anybody else done anything like this?

I have also experimented with building the suite using the JUnit 3 pattern of building a test suite, but that seems to break since my test case classes (TestClass1 and TestClass2) are using JUnit 4 annotations.  JUnit 4 does not seem to like being mixed with JUnit 3.

Thanks in advance,

Bill
0
Comment
Question by:billybobc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20398804
ant has a task to run junit tests

http://ant.apache.org/manual/OptionalTasks/junit.html
0
 
LVL 3

Expert Comment

by:humergu
ID: 20398971
maybe what you want is a build server, which integrates with ant (and JUnit) and stores the results, etc...

the abstraction of MAVEN is a bit higher than ant (but maven is no build server - a bit like ant, but at a higher level) (OpenSource Software!)
http://maven.apache.org/

CruiseControl is a build server which integrates with Ant (and therefore JUnit) (OpenSource Software!)
http://cruisecontrol.sourceforge.net/

alternative build servers:
continuum (http://maven.apache.org/continuum/) integrates with ant, maven ... OpenSource Software!
anthillAnthill (http://www.anthillpro.com/)
0
 

Accepted Solution

by:
billybobc earned 0 total points
ID: 20577789
I found a solution that utilizes the following JUnit classes:

org.junit.runner.JUnitCore
org.junit.runner.notification.RunListener:

First I created a few support classes:

RegTestResult - this is a simple bean class that contains four fields: String testMethodName,  String description, String failureMessage, long duration.

RegTestRestultRecorder - this class extends org.junit.runner.notification.RunListener and takes an empty Vector<RegTestResult> on construction.  By implementing the testFailure(), testStarted() and testFinished() methods I can capture test results and store them in the Vector<RegTestResult> passed on construction.  

RunSuite - class containing my "main" method.  Instantiates a JUnitCore class to run the JUnit test suite adds a RegTestResultRecorder instance as a RunListener using the addListener() method.  The suite is executed using JUnitCore's run() method.   When the suite is finished, the Vector<RegTestResult> passed to the RegTestResultRecorder on construction, contains the results of all the tests and I can output them in whatever fashion I want.  

My RunSuite.main() method looks like this:

        // Check that at least the test class name is specified.
        if (args.length == 0)
        {
            System.err.println("Usage: java RunSuite <testSuiteClass>");
        }

        String testSuiteClassName = args[0];
        Class testSuiteClass = null;

        try
        {
            testSuiteClass = Class.forName(testSuiteClassName);
        }
        catch (ClassNotFoundException e1)
        {
            System.err.println("Unknown test suite class '" + testSuiteClassName + "'");
            System.exit(1);
        }

        // Create a JUnitCore instance to run the tests with.
        JUnitCore core = new JUnitCore();
       
        // Create a Vector of RegTestResults in which reg test results will be stored.
        Vector<RegTestResult> testResults = new Vector<RegTestResult>();
        // Add RegTestResultRecorder (which extends RunListener) instance to JUnitCore instance
        // in order to capture test results.
        core.addListener(new RegTestResultRecorder(testResults));

       // Run the test class.  testResults will have the results of all test runs on return.
        core.run(testSuiteClass);
   
 
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 53
eclipse buid path vs tomcat lib path 10 38
runtime exception 2 50
activeMQ Queue Messages Enqueued and Messages Dequeued 1 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question