This module provides code coverage metrics for Perl. Code coverage metrics describe how thoroughly tests exercise code. By using Devel::Cover you can discover areas of code not exercised by your tests and determine which tests to create to increase coverage. Code coverage can be considered an indirect measure of quality.

Although it is still being developed, Devel::Cover is now quite stable and provides many of the features to be expected in a useful coverage tool.

Statement, branch, condition, subroutine, and pod coverage information is reported. Statement and subroutine coverage data should be accurate. Branch and condition coverage data should be mostly accurate too, although not always what one might initially expect. Pod coverage comes from Pod::Coverage. If Pod::Coverage::CountParents is available it will be used instead. Coverage data for other criteria are not yet collected.

The cover program can be used to generate coverage reports. Devel::Cover ships with a number of different reports including various types of HTML output, textual reports, a report to display missing coverage in the same format as compilation errors and a report to display coverage information within the Vim editor.

It is possible to add annotations to reports, for example you can add a column to an HTML report showing who last changed a line, as determined by git blame. Some annotation modules are shipped with Devel::Cover and you can easily create your own.

The gcov2perl program can be used to convert gcov files to Devel::Cover databases. This allows you to display your C or XS code coverage together with your Perl coverage, or to use any of the Devel::Cover reports to display your C coverage data.

Code coverage data are collected by replacing perl ops with functions which count how many times the ops are executed. These data are then mapped back to reality using the B compiler modules. There is also a statement profiling facility which should not be relied on. For proper profiling use Devel::NYTProf. Previous versions of Devel::Cover collected coverage data by replacing perl's runops function. It is still possible to switch to that mode of operation, but this now gets little testing and will probably be removed soon. You probably don't care about any of this.

Perl 5.7 is unsupported. Perl 5.8.8 or greater is recommended. Perl 5.8.7 has problems and may crash. Whilst Perl 5.6 should mostly work you will probably miss out on coverage information which would be available using a more modern version and will likely run into bugs in perl. Devel::Cover support for unsupported Perl versions may be removed at any time, but I try to keep older versions running provided this does not cause undue difficulty i other areas.

Different versions of perl may give slightly different results due to changes in the op tree.

The ability to compile XS extensions.

This means a working C compiler and make program at least. If you built perl from source you will have these already and they will be used automatically. If your perl was built in some other way, for example you may have installed it using your Operating System's packaging mechanism, you will need to ensure that the appropriate tools are installed.

By adding use Devel::Cover; to your mod_perl startup script, you should be able to collect coverage information when running under mod_perl. You can also add any options you need at this point. I would suggest adding this as early as possible in your startup script in order to collect as much coverage information as possible.

You can specify options to some coverage criteria. At the moment only pod coverage takes any options. These are the parameters which are passed into the Pod::Coverage constructor. The extra options are separated by dashes, and you may specify as many as you wish. For example, to specify that all subroutines containing xx are private, call Devel::Cover with the option -coverage,pod-also_private-xx.

You may select the files for which you want to collect coverage data using the select, ignore and inc options. The system uses the following procedure to decide whether a file will be included in coverage reports:

If the file matches a RE given as a select option, it will be included.

Otherwise, if it matches a RE given as an ignore option, it won't be included.

Otherwise, if it is in one of the inc directories, it won't be included.

Otherwise, it will be included.

You may add to the REs to select by using +select, or you may reset the selections using -select. The same principle applies to the REs to ignore.

The inc directories are initially populated with the contents of perl's @INC array. You may reset these directories using -inc, or add to them using +inc.

Although these options take regular expressions, you should not enclose the RE within // or any other quoting characters.

The options -coverage, [+-]select, [+-]ignore and [+-]inc can be specified multiple times, but they can also take multiple comma separated arguments. In any case you should not add a space after the comma, unless you want the argument to start with that literal space.

Sometimes you have code which is uncoverable for some reason. Perhaps it is an else clause that cannot be reached, or a check for an error condition that should never happen. You can tell Devel::Cover that certain criteria are uncoverable and then they are not counted as errors when they are not exercised. In fact, they are counted as errors if they are exercised.

This feature should only be used as something of a last resort. Ideally you would find some way of exercising all your code. But if you have analysed your code and determined that you are not going to be able to exercise it, it may be better to record that fact in some formal fashion and stop Devel::Cover complaining about it, so that real problems are not lost in the noise.

There are two ways to specify a construct as uncoverable, one invasive and one non-invasive.

You can use special comments in your code to specify uncoverable criteria. Comments are of the form:

# uncoverable <criterion> [details]

The keyword "uncoverable" must be the first text in the comment. It should be followed by the name of the coverage criterion which is uncoverable. There may then be further information depending on the nature of the uncoverable construct.

If there are multiple statements (or any other criterion) on a line you can specify which statement is uncoverable by using the "count" attribute, count:n, which indicates that the uncoverable statement is the nth statement on the line.

Because of the way in which Perl short-circuits boolean operations, there are three ways in which such conditionals can be uncoverable. In the case of $x && $y for example, the left operator may never be true, the right operator may never be true, and the whole operation may never be false. These conditions may be modelled thus:

A subroutine should be marked as uncoverable at the point where the first statement is marked as uncoverable. Ideally all other criteria in the subroutine would be marked as uncoverable automatically, but that isn't the case at the moment.

Of these, only the first two are implemented at the moment. The parameter for -add_uncoverable_point is a string composed of up to seven space separated elements: "$file $criterion $line $count $type $class $note".

The -silent option is turned on when Devel::Cover is invoked via $HARNESS_PERL_SWITCHES or $PERL5OPT. Devel::Cover tries to do the right thing when $MOD_PERL is set. $DEVEL_COVER_OPTIONS is appended to any options passed into Devel::Cover.

When running Devel::Cover's own test suite, $DEVEL_COVER_DEBUG turns on debugging information, $DEVEL_COVER_GOLDEN_VERSION overrides Devel::Cover's own idea of which golden results it should test against, and $DEVEL_COVER_NO_COVERAGE runs the tests without collecting coverage. $DEVEL_COVER_DB_FORMAT may be set to "Sereal", "JSON" or "Storable" to override the default choice of DB format (Sereal, then JSON if either are available, otherwise Storable). $DEVEL_COVER_IO_OPTIONS provides fine-grained control over the DB format. For example, setting it to "pretty" when the format is JSON will store the DB in a readable JSON format. $DEVEL_COVER_CPUS overrides the automated detection of the number of CPUs to use in parallel testing.

Perl keeps track of which modules have been loaded (to avoid reloading them). Because of this, it isn't possible to get coverage for a path where a runtime import fails if the module being imported is one that Devel::Cover uses internally. For example, suppose your program has this function:

If you redefine a subroutine you may find that the original subroutine is not reported on. This is because I haven't yet found a way to locate the original CV. Hints, tips or patches to resolve this will be gladly accepted.