This section contains step-by-step examples that show how to produce test coverage reports using the API Coverage tool. The examples are located in the following directory:
SIGTEST_HOME/examples/userguide/apicover
The appendix consists of the following examples. The examples build upon each other.
Use the API Coverage Tool to Calculate Test Coverage
Note: The examples use UNIX operating system syntax. |
Caution: Code examples are an optional part of the SigTest distribution and are not required to use the SigTest tools. You can remove the example directory ($SIGTEST_HOME |
The following procedures describes how to set up and use the API Coverage tool.
Make SIGTEST_HOME/examples/userguide/apicover
the current directory.
Make sure that the JAVA_HOME
environment variable refers to JDK 5.0 or higher.
Edit the following file:
SIGTEST_HOME/examples/userguide/apicover/build.properties
Set the junit.jar property to point to the junit.jar
file. Obtain this file from version 3.7 or higher of the JUnit test harness product.
Make sure Apache Ant 1.7 or higher is available and properly configured on your system.
Run the ant
command and specify the ”build” target.
This command builds the following:
API classes
Example application (helloWorld.jar
)
Test suite
API documentation (doc/
directory)
Run the example application to verify that it built correctly.
%java -jar helloWorld.jar
The application's output should look like this:
Good morning, Brazil! Good morning, USA! Good evening, China! Good afternoon, Czech Republic! Good afternoon, Russia! Good afternoon, United Kingdom! Good evening, India! Good afternoon, Mali! Hello, world!
Use the Ant ”sigtest” target to generate the signature file.
%ant sigtest
The ”sigtest” target executes the following command:
java -jar sigtestdev.jar setup -filename reports/greetapi.sig -package com.sun.tdk.samples.helloworld.api -classpath helloWorld.jar:$JAVA_HOME/jre/lib/rt.jar
This command generates the file reports/greetapi.sig. While the command runs you should see the following output:
sigtest: [java] Class path: "helloWorld.jar:/opt/java/jdk1.5.0_16/jre/lib/rt.jar" [java] Constant checking: on [java] Found in total: 13217 classes [java] Selected by -Package: 5 classes [java] Written to sigfile: 8 classes [java] STATUS:Passed. [java] Java Result: 95
The script executes the following command:
java -jar apicover.jar -ts test_classes -api reports/greetapi.sig -apiinclude com.sun.tdk.samples.helloworld.api -mode w -report reports/cov-worst-case.txt
This mode reports that about of 37% of the API is covered by the tests. Some members that are defined in superclasses, such as finalize() or clone(), are marked as not covered. Some fields are also marked as not covered by the tests. The output written to a file named reports/cov-worst-case.txt and should appear as shown below.
Coverage Report ========================================================================== Package classes Class members tested %% Member ========================================================================== com 4 33 14 42% -------------------------------------------------------------------------- com.sun 4 33 14 42% -------------------------------------------------------------------------- com.sun.tdk 4 33 14 42% -------------------------------------------------------------------------- com.sun.tdk.samples 4 33 14 42% -------------------------------------------------------------------------- com.sun.tdk.samples.helloworld 4 33 14 42% -------------------------------------------------------------------------- com.sun.tdk.samples.helloworld.api 4 33 14 42% Greet 3 3 100% GreetFactory 6 1 16% - clone() - equals(Object) - finalize() - hashCode() - toString() Places 15 6 40% - getCountry() - toString() - valueOf(Class,String) - valueOf(String) - China - Czech - India - Mali - UK TimeOfDay 9 4 44% - valuOf(Class,String) - AFTERNOON - EVENING - MORNING - NIGHT ========================================================================== Overall coverage 4 33 14 42% ========================================================================== Configuration ts test_classes api reports/greetapi.sig mode w
The script executes the following command:
java -jar apicover.jar -ts test_classes -api reports/greetapi.sig -apiinclude com.sun.tdk.samples.helloworld.api -mode r -report reports/cov-realworld.txt
This estimation does not take into account overridden members defined in superclasses. Overall coverage is 52%.
The report is named reports/cov-realworld.txt
and is shown below.
Coverage Report ========================================================================== Package classes Class members tested %% Member ========================================================================== com 4 25 14 56% -------------------------------------------------------------------------- com.sun 4 25 14 56% -------------------------------------------------------------------------- com.sun.tdk 4 25 14 56% -------------------------------------------------------------------------- com.sun.tdk.samples 4 25 14 56% -------------------------------------------------------------------------- com.sun.tdk.samples.helloworld 4 25 14 56% -------------------------------------------------------------------------- com.sun.tdk.samples.helloworld.api 4 25 14 56% Greet 3 3 100% GreetFactory 1 1 100% Places 13 6 46% - getCountry() - valueOf(String) - Brazil - China - Czech - India - Mali - UK TimeOfDay 8 4 50% - AFTERNOON - EVENING - MORNING - NIGHT ========================================================================== Overall coverage 4 25 14 56% ========================================================================== Configuration ts test_classes api reports/greetapi.sig mode r
The script executes the following command:
java -jar apicover.jar -ts test_classes -api reports/greetapi.sig -apiinclude com.sun.tdk.samples.helloworld.api -mode r -excludeFields -detail 5 -report reports/cov-realworld-without-fields.txt
In this case test coverage is 84%.
The report is named reports/cov-realworld-without-fields.txt
and is shown below.
Coverage Report ========================================================================== Package classes Class members tested %% Member count ========================================================================== com 4 13 11 84% -------------------------------------------------------------------------- com.sun 4 13 11 84% -------------------------------------------------------------------------- com.sun.tdk 4 13 11 84% -------------------------------------------------------------------------- com.sun.tdk.samples 4 13 11 84% -------------------------------------------------------------------------- com.sun.tdk.samples.helloworld 4 13 11 84% -------------------------------------------------------------------------- com.sun.tdk.samples.helloworld.api 4 13 11 84% Greet 3 3 100% + greetPlaces() 1 + greetWorld() 1 + setWriter(java.io.Writer) 1 GreetFactory 1 1 100% + getInstance() 1 Places 5 3 60% - getCountry() + getTz() 1 + shuffledValues() 1 - valueOf(String) + values() 2 TimeOfDay 4 4 100% + getCurrentTime(java.util.TimeZone) 1 + toString() 2 + valueOf(String) 1 + values() 2 ========================================================================== Overall coverage 4 13 11 84% ========================================================================== Legend + covered member - uncovered member Configuration ts test_classes api reports/greetapi.sig excludeFields yes mode r