Test coverage in Tcl

Introduction

The Tcl script presented here enables the user to register the test coverage of Tcl scripts that have been properly prepared.

Knowing the test coverage is important because:

To be precise: Testcov, the application described here, is limited to statement and branch coverage, as these are the easiest to detect. They are also the easiest to satisfy, but they do represent a very important class of testing strategies. In fact, unless most or (preferrably all) of the code is exercised, chances remain great that some horrific bug is lurking somewhere.

For more information: B. Beizer, Software Testing Techniques, 1992 gives an excellent overview.

The remainder of this document covers the following subjects:

Version and copyright

This document describes Testcov, version 0.3, july 2001.

Usage of Testcov and AwkProc is free, as long as you acknowledge the author, Arjen Markus (e-mail: arjen.markus@wldelft.nl).

There is no guarantee nor claim that the results are accurate.

Using Testcov to obtain test coverage reports

Using Testcov is simple: Consider the following example (in example.tcl):

proc SumOf { args } {
   set sum 0.0

   foreach value $args {
      set sum [expr $sum+$value]
   }

   return $sum
}

set sum [SumOf 1 2 3 4]

if { $sum < 0.0 } {
   puts "Sum is negative!"
}
exit 0
Running Testcov means, executing the following commands:

# Instrument the script
tclsh testcov.tcl -inst example.tcl
# Run the instrumented version
tclsh example.tcl_inst
# Report the coverage
tclsh testcov.tcl -rep example.tcl
The result is this:

Test coverage report
--------------------

File: example.tcl

   1: proc SumOf { args } {
Note: Procedure never called with zero extra arguments
Note: Procedure never called with one extra argument

         set sum 0.0

   4:    foreach value $args {
Note: No zero-iterations

            set sum [expr $sum+$value]
         }

         return $sum
      }

      set sum [SumOf 1 2 3 4]

   1: if { $sum < 0.0 } {
Note: Primary branch not executed

         puts "Sum is negative!"
      }
      exit 0
You should note the following:

Implementation notes

The instrumentation of Tcl source files works by inserting a number of statements into the existing code. Notably, the file "testcov.tcl" will be sourced at the start, so that the auxiliary routines (__cov_*) are defined. Also two ordinary commands, exit and source are redefined:

Limitations of the present version

Testcov has a number of limitations that you should be aware of: Despite its severe limitations, Testcov can be quite useful: