CpSc 461, Software Engineering Techniques

Code Coverage Testing Exercise

While working with JUnit last week, one question that may have occurred to you is "How can I be sure that my tests are exercising all of the code?" There are at least three ways to be sure:
  1. Examine the code and think through each of your tests, convincing yourself that the test values exercise all of the branches. This is probably the best way but is admittedly difficult for large programs.
  2. Step through the code in a debugger, watching to see which lines are executed. This method may turn up cases that were overlooked in (1), but is labor-intensive.
  3. Use a code coverage tool to instrument the code and report on the results of tests. This is the method often chosen for large projects.

Cobertura

In this exercise, you will have a chance to use the Cobertura code coverage tool for Java.

To begin, download coverage.zip and unzip its contents to a new directory. Since Java build environments tend to require complex configuration, the .ZIP file contains a pre-configured Apache Ant build environment for JUnit and Cobertura. To build and run the initial tests, open a Command Prompt window, change to the new directory, and type:

C:\coverage> .\bin\ant
Ant should create the appropriate output directories, compile the program, instrument it, run the JUnit tests, and generate a report. When it finishes, open the file reports\index.html in a web browser to see the code coverage report. Click on the Tree class in the left pane to see exactly which lines were and were not executed by the JUnit test.

Your assignment is to create new JUnit tests to reach 100% code coverage. After adding each new test, re-run Ant and reload the report to see the results.

Build Environment

The coverage.zip file contains:
build.xml
The build instructions for Ant. Think of Ant as a Java-specific version of make, and build.xml as its Makefile
bin\ant.bat
The batch file used to run Ant.
lib\
This directory contains .JAR files required by Ant, JUnit, and Cobertura.
src\Tree.java
The Binary Tree class that you will be testing. This is an example program from a compiler class at UCLA.
src\TestTree.java
The JUnit test suite. This is the file you will need to modify in order to add tests.
A number of other files will be generated by Ant as part of the build process:
build\classes
This directory contains compiled .class files
build\instrumented
This directory contains .class files with instrumentation added by Cobertura
reports\
This directory contains the HTML files for the coverage report