Chapter 7. Reporting JUnit results
This chapter covers
- Logging from a Base Test Case
- Logging with Log4Unit
- Reporting results with Ant, including <junitreport>
- Customizing Ant’s test run reports
- Using a custom TestListener
- Counting assertions
This chapter covers various tools and techniques available for reporting JUnit test results, including extending JUnit to write your own custom reporting mechanisms. JUnit by itself provides two simple mechanisms for reporting test results: simple text output to System.out and its famous Swing and AWT “green bar” GUIs (the AWT GUI being a vestige of JUnit’s Java 1.1 support). The results reporting that JUnit provides out of the box is useful for developers at their desktops, but that is about it. You need to extend JUnit or use it with another tool if you want automated test reports in formats such as XML or HTML.
There are a slew of JUnit extensions out there, many of which revolve around subclassing TestCase. These extensions usually can be executed with the built-in JUnit test runners or with Ant’s <junit> task with no extra work. Ideally you want reporting solutions that are reusable across any of these extensions; therefore, you should extend JUnit reporting by implementing or extending standard APIs in Ant or JUnit.
JUnit is most often executed in one of three contexts, each of which provides different reporting features and opportunities for extension:
- IDE
- Command line
- Ant build script (or, increasingly, Maven target)