1

I got the following JUnit stuff running well with ant, but so far I have not found how to use my formatter from within my main() method (when not using ant).

Here's the ant task:

<junit printsummary="true" errorProperty="test.failed" failureProperty="test.failed">
    <classpath refid="test.class.path" />

    <!-- use multiple formatters -->
    <formatter type="brief" usefile="false" />
    <!-- console log -->
    <formatter type="xml" classname="x.selenium.output.SeleniumFrameworkFormatter" />
    <!-- file log -->

    <test name="x.selenium.SeleniumFramework" haltonfailure="no" outfile="result" todir="${build.temp.dir}" />
<junit>

In Java code, I have this as an alternative, but this prints to the console, and not using my formatter to create a nice html file:

JUnitCore junit = new JUnitCore();
Result result = junit.run(classes);

Does anyone know how to do a similar thing in Java code? Thanks!

Steven De Groote
  • 2,187
  • 5
  • 32
  • 52

1 Answers1

2

There isn't a nice way to produce formatted output from pure JUnit. The easiest way to get the same output as ant is to run ant itself. You can either run ant directly, using something like:

Process p = Runtme.getRuntime.exec("/path/to/ant target");

or similar, or you can invoke the org.apache.tools.ant.Main method directly, but be aware that this calls System.exit().

See also the following answers on running Ant from Java: Run ant from Java, and Invoke ant from java, then return to java after ant termination.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171