Code CoverageNot available in CodePro AnalytiX Rational Edition
 |
|
The Code Coverage tools allow you to measure how much of your code is
being executed. Probably the most common reason for measuring code
coverage is to evaluate the effectiveness of test code at exercising
all possible paths through the code.
There are two different mechanisms provided. Deciding which one is
right for your needs depends primarily on the complexity of the code
being run. The first is designed for use with stand-alone Java
applications that run in a single VM and do not make use of
specialized class loaders.
|
|

Play Movie |
|
The second mechanism is designed for arbitrary applications, but might
require a bit more work to get it set up initially, again depending on
just how complex the code is. Both mechanisms are described in
separate sections below.
|
Simple Code Coverage
You can measure the code coverage for a particular piece of code by
selecting a class that defines a main method and then selecting
"Run Code Coverage" from the "CodePro Tools"
submenu. The selected class will then be executed and the results will
be displayed in the Coverage View.
From within the Coverage View,
the results may also be exported to various report formats including
HTML, XML and text.
The information about how the class is to be run and what kind of coverage
information to gather is captured in a special kind of
launch configuration.
You can also create the launch configuration manually, but the "Run
Code Coverage" menu item makes that process easier.
Once a launch configuration has been created, you can measure the coverage
of your code either by using the "Run Code Coverage" menu item
or by directly executing the
launch configuration
from the "Run" menu.
Full Featured Code Coverage
Code coverage works by instrumenting the bytecodes being loaded by the VM
to collect data about which bytecodes have been executed. Code run using
the code coverage launch configuration is instrumented dynamically as it
is loaded by using a specialized class loader. This class loader becomes
the default class loader for the VM that is initially executed. If
additional class loaders are created by the application, or if additional
VM's are run, they will not perform the dynamic instrumentation necessary
to collect code coverage information.
The alternative is to statically instrument the code before executing the
application. CodePro provides support for instrumenting the code on a
project-by-project basis. Each project has a
Code Coverage property sheet.
This property sheet allows you to control whether the class files are
to be instrumented. It also allows you to control which class files are
instrumented. As a convenience, the "Instrument Code" and
"Uninstrument Code" menu items on the "CodePro Tools"
submenu will turn instrumentation on and off while leaving the other
project-related settings unchanged.
Once your projects have been instrumented, you will collect code coverage
information any time the code is run, using any launch configuration that
you have created. In other words, you do not need to, and should not, use
the Code Coverage launch configuration to run pre-instrumented code.
There are two other implications of using this mechanism that you need to
be aware of. The first is that if you want to run the code without
gathering code coverage data, you will need to uninstrument all of the
projects containing code that will be executed.
The second implication is similar. If you are going to distribute the
class files from your local machine, you need to be careful to
uninstrument the code before you package it for delivery. Otherwise the
code will be collecting code coverage data whenever it is executed.
Viewing the Results
Each time you measure a particular class, a coverage report will be
generated. If you measure the same class multiple times, past versions of
the report will be remembered so that those versions can be compared. The
number
of versions to be remembered can be configured. Coverage reports are
stored on disk, so they are available even after Eclipse is closed and
then re-opened.
Acknowledgements
The Code Coverage tool is based on the EMMA
code coverage engine. Detailed information on using EMMA is available from
the EMMA web site:
This feature is based on technology from the EMMA
Project.
http://emma.sourceforge.net
Copyright © 2001-2005 Vlad Roubtsov
|