The UML Profile for Framework Architectures

6.4 Reporting test results

To complete the JUnit cookbook, the recipe 'JUnit 2: How to report test results' describes how to modify the default mechanism of collecting and presenting test results. The design of the TestResult class relies on the Separation construction principle. Bear in mind the abstractly coupled JUnit classes TestCase and TestResult (see Figure 6.6). JUnit provides several subclasses of TestResult featuring textual or graphical output. However, other variants might be required, for example to save results to a database.

The definition of another TestResult variant is probably only done once in a JUnit adaptation. As this chapter concentrates mainly on demonstrating the feasibility of the cookbook approach in combination with the UML-F profile, we keep this recipe short and refer the interested reader to a more detailed description (Beck and Gamma, 1998a). Table 6.7 describes this recipe it reuses the generic recipe for the Separation construction principle given in Chapter 5.

Table 6.7. TestResult adaptation recipe

Recipe 'JUnit 2: How to report test results'
Intent Providing addtional mechanisms for reporting tests.
Classes TestResult.
Steps to Apply
  1. Understand how the test reporting structure works.

  2. Determine whether there is already a subclass that provides the desired or a similar reporting mechanism. Can an existing subclass be used 'as is' or used as a base class for inheritance?

  3. Build a new subclass of the TestResult class.

    • Implement the hook methods startTest(), addFailure(), addError(), and/or endTest().

    • Determine whether you can, or should, reuse the provided default implementation, e.g. through calling the inherited method.

Discussion This recipe should be applied rarely. In most cases the default features provided by JUnit are sufficient.

Figure 6.13 shows the UML-F class diagram of a TestResult adaptation for the generation of HTML files. If an error or a failure occurs, a line is added to the HTML file. The notes attached to the hook methods describe the method behavior.

Figure 6.13. Generating HTML files for the presentation of test results

Figure 6.14 shows a browser that displays an HTML file that reports a failure when testing the ComplexNumber class with an assertion that (0, 1) added to (1, 1) would wrongly sum up to (1, 1).

Figure 6.14. Display of a sample HTML file that reports a failure

Категории