The Artima Developer Community
Sponsored Link

Create an XML Reporter for Your Unit Tests
by Bill Venners
February 24, 2003

<<  Page 5 of 8  >>

Advertisement

Implement the runStarting Method

Now that the no-arg constructor, setConfiguration, and dispose methods are behind you, you need only implement the 11 event handler methods. The custom way you implement these methods determines the custom way your Reporter will present test results to the user. In XMLReporter, the event handler methods write XML to the standard output.

A good place to start implementing is the runStarting method, the first method Runner invokes for each test run. In XMLReporter, all event handler methods have the same basic structure. Each method checks the configuration to see if it is supposed to report the event. If so, it prints some XML to the standard output. For example, the runStarting method checks to make sure the PRESENT_RUN_STARTING configuration Character is contained in the current configuration. If so, it writes an XML header <?xml version="1.0"?> and the element <run> to the standard output.

In XMLReporter, I ignore runStarting's testCount parameter, which indicates the number of tests expected in the run. In your custom Reporters, you may wish to present this information to the user. I do make sure that testCount is non-negative, which is required by runStarting method contract as defined in interface Reporter.

Here's XMLReporter's runStarting method:

    /**
     * Prints information indicating that a run with an expected <code>testCount</code>
     * number of tests is starting, if
     * the current configuration includes <code>Reporter.PRESENT_RUN_STARTING</code>.
     * If <code>Reporter.PRESENT_RUN_STARTING</code> is not included in the the
     * current configuration, this method prints nothing.
     *
     * @param testCount the number of tests expected during this run.
     *
     * @exception IllegalArgumentException if <code>testCount</code> is less than zero.
     */
    public void runStarting(int testCount) {

        if (testCount < 0) {
            throw new IllegalArgumentException();
        }

        if (configuration.contains(Reporter.PRESENT_RUN_STARTING)) {

            pw.println("<?xml version=\"1.0\"?>");
            pw.println("<run>");
        }
    }

Implement the runCompleted Method

The final method invoked by Runner during a test run is either runCompleted, runStopped or runAborted. You may wish to implement these methods next in your custom Reporter. When a run completes normally, Runner invokes runCompleted.

In XMLReporter's runCompleted method, I need to print out a closing </run> element to match the opening <run> element printed out in runStarting. First, I check to make sure the PRESENT_RUN_COMPLETED configuration Character is contained in the current configuration. If so, I write the element </run> to the standard output. I flush pw's buffers to make sure that at the end of the run, the complete XML document shows up at the standard output.

Here's XMLReporter's runCompleted method:

    /**
     * Prints information indicating a run has completed, if
     * the current configuration includes <code>Reporter.PRESENT_RUN_COMPLETED</code>.
     * If <code>Reporter.PRESENT_RUN_COMPLETED</code> is not included in the the
     * current configuration, this method prints nothing.
     */
    public synchronized void runCompleted() {

        if (configuration.contains(Reporter.PRESENT_RUN_COMPLETED)) {

            pw.println("</run>");
            pw.flush();
        }
    }

Implement the runStopped Method

If a run is stopped—for example, if the user presses the stop button on the GUI—Runner invokes runStopped instead of runCompleted. In your custom Reporter, you may wish to implement the runStopped method next. In XMLReporter, the runStopped method looks much like the runCompleted method, except that I also print out an extra <runStopped/> element before the </run> element. I print the extra <runStopped/> so that users can tell by looking at the XML document that the run stopped rather than completed normally.

Here's XMLReporter's runStopped method:

    /**
     * Prints information indicating a runner has stopped running a suite of tests prior to completion, if
     * the current configuration includes <code>Reporter.PRESENT_RUN_STOPPED</code>.
     * If <code>Reporter.PRESENT_RUN_STOPPED</code> is not included in the the
     * current configuration, this method prints nothing.
     */
    public synchronized void runStopped() {

        if (configuration.contains(Reporter.PRESENT_RUN_STOPPED)) {

            String stringToReport = "<runStopped/>\n";
            stringToReport += "</run>";

            pw.println(stringToReport);
            pw.flush();
        }
    }

<<  Page 5 of 8  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us