Using Rational PureCoverage
Rational PureCoverage: What it does
During the development process, software changes daily, sometimes hourly. Unfortunately, test suites do not always keep pace. Rational® PureCoverage® is a simple, easily deployed tool that identifies the portions of your code that have not been exercised by testing.
- Identify the portions of your application that your tests have not exercised
- Accumulate coverage data over multiple runs and multiple builds
- Merge data from different programs sharing common source code
- Work closely with Purify to make sure that Purify finds errors throughout your entire application
- Automatically generate a wide variety of useful reports
- Access the coverage data so you can write your own reports
PureCoverage provides the information you need to identify gaps in testing quickly, saving precious time and effort.
This chapter introduces the basic concepts involved in using PureCoverage. For complete information, see the PureCoverage online help system.
Finding untested areas of Hello World
This chapter shows you how to use PureCoverage to find the untested parts of the hello_world.c program.
- If PureCoverage has been installed on your system as a component of Rational PurifyPlus, the Rational directory contains the files purifyplus_setup.csh and purifyplus_setup.sh. Source the file that is appropriate to your shell to license PureCoverage for your use.
- Create a new working directory. Go to the new directory, and copy the hello_world.c program and related files from the <purecovhome>/example directory:
% mkdir /usr/home/pat/example % cd /usr/home/pat/example % cp <purecovhome>/example/hello* .- Examine the code in hello_world.c.
The version of hello_world.c provided with PureCoverage is slightly more complicated than the usual textbook version.
#include <stdio.h> void display_hello_world(); void display_message(); main(argc, argv) int argc; char** argv; { if (argc == 1) display_hello_world(); else display_message(argv[1]); exit(0); } void display_hello_world() { printf("Hello, World\n"); } void display_message(s) char *s; { printf("%s, World\n", s); }Instrumenting a program
A message appears, indicating the version of PureCoverage that is instrumenting the program:
- Compile and link the Hello World program, then run the program to verify that it produces the expected output:
- Instrument the program by adding purecov to the front of the compile/link command line. To have PureCoverage report the maximum amount of detail, use the -g option:
% purecov cc -g hello_world.cNote: If you compile your code without the -g option, PureCoverage provides only function-level data. It does not show line-level data.PureCoverage 4.4 Solaris 2, Copyright 1994-1999 Rational Software Corp. All rights reserved. Instrumenting: hello_world.o LinkingNote: When you compile and link in separate stages, add purecov only to the link line.Running the instrumented program
Run the instrumented Hello World program:% a.outPureCoverage displays the following:
The a.out program produces its normal output, just as if it were not instrumented. When the program completes execution, PureCoverage writes coverage information for the session to the file a.out.pcv. Each time the program runs, PureCoverage updates this file with additional coverage data.
Displaying coverage data
To display the coverage data for the program, use the command:% purecov -view a.out.pcv &This displays the PureCoverage Viewer.
In this example, there is only one source directory, so the information displayed for the directory is identical to the Total Coverage information.
Note: The default header for line statistics is ADJUSTED LINES, not just LINES. This is because PureCoverage has an adjustment feature that lets you adjust coverage statistics by excluding specific lines. Under certain circumstances, the adjusted statistics give you a more practical reflection of coverage status than the actual coverage statistics. The ADJS column in this example contains zeroes, indicating that it does not include adjustments.
Expanding the file-level detail
Click next to .../example/ to expand the file-level information for the directory.
You used only one file in the example directory to build a.out. Therefore the FUNCTIONS and ADJUSTED LINES information for the file is the same as for the directory. The number 1 in the Runs column indicates that you ran the instrumented a.out. only once.
Note: When you are examining data collected for multiple executables, or for executables that have been rebuilt with some changed files, the number of runs can be different for each file.
Examining function-level detail
Expand the hello_world.c line to show function-level information.The Viewer shows coverage information for the functions display_message, main, and display_hello_world.
PureCoverage does not list the printf function or any functions that it calls. The printf function is a part of the system library, libc. By default, PureCoverage excludes collection of data from system libraries.
Examining the annotated source
To see the source code for main annotated with coverage information, click the Annotated Source tool next to main in the Viewer. PureCoverage displays the Annotated Source window.
PureCoverage highlights code that was not used when you ran the program. In this file only two pieces of code were not used:
A quick analysis of the code reveals the reason: the program was invoked without arguments.Improving Hello World's test coverage
To improve the test coverage for Hello World:
- Without exiting PureCoverage, run the program again, this time with an argument. For example:
% a.out GoodbyePureCoverage displays the following:
**** PureCoverage instrumented a.out (pid 17331 at Wed Feb 3 10:38:07 1999) PureCoverage 4.4 Solaris 2, Copyright (C) 1994-1999 Rational Software Corp. * All rights reserved. * For contact information type: "purecov -help" * Command-line: a.out Goodbye * Options settings: -purecov \ -purecov-home=/usr/pure/purecov-4.4-solaris2 * PureCoverage licensed to Rational Software Corp. * Coverage counting enabled. Goodbye, World **** PureCoverage instrumented a.out (pid 17331) **** * Saving coverage data to /usr/home/pat/example/a.out.pcv. * To view results type: purecov -view /usr/home/pat/example/a.out.pcv- PureCoverage displays a dialog confirming that coverage data has changed for this run. Select Reload changed .pcv files and click OK.
Note: This dialog appears only if the PureCoverage Viewer is open when you run the program.
PureCoverage updates the coverage information in the Viewer and the Annotated Source window.
Note: If you still have untested lines, it is possible that your compiler is generating unreachable code.
Using report scripts
You can use PureCoverage report scripts to format and process PureCoverage data. The report scripts are located in the <purecovhome>/scripts directory.Select File > Run script to open the script dialog.
You can also run report scripts from the command line.
Report scriptsBuild-time options
You can specify build-time options on the link line when you instrument programs with PureCoverage. For example:% purecov -cache-dir=$HOME/cache -always-use-cache-dir \ cc ...Run-time options
You can specify run-time options on the link line or by using the PURECOVOPTIONS environment variable. For example:% setenv PURECOVOPTIONS \ "-counts-file=./test1.pcv 'printenv PURECOVOPTIONS'"1 Can use the conversion characters listed in Conversion characters for filenames.Analysis-time options
Use analysis-time options with analysis-time mode options. For example:% purecov -merge=result.pcv -force-merge filea.pcv fileb.pcvAnalysis-time mode options
Command-line syntax:% purecov -<mode option> [analysis-time options] \ <file1.pcv file2.pcv ...>
Analysis-time mode options Compatible options -apply-adjustments none -force-merge -force-merge,
-user-pathCopyright© 1999, 2001 Rational Software Corporation. All rights reserved.