views:

75

answers:

1

I have a unique requirement of handling code coverage of my Perl scripts.

I have written a few Perl scripts which in turn uses a few Perl modules. My requirement is to use run these Perl scripts with different options they support and assess the code coverage of both Perl scripts and Perl modules.

So I am using Devel::Cover, Module::Build and Test::More from CPAN. It's all woring fine if I call functions inside Perl modules directly inside test script. But it's not working if I call the scripts directly (In this case I am not getting generated with code coverage of Perl modules and scripts both).

Here is my example test script using Test::More:

use strict;
use warnings;
use Test::More; 

BEGIN { plan tests => 1 }

ok(sub {
   my @args = ("ex4200fw","-query-fw","-i","192.168.168.1");
   #print "# Executing @args \n";
   `@args`;
   my $rc = $? >> 8;
   #print "# Return code: $rc \n";
   $rc == 1
}->(),"Query Juniper EX4200 FW, incorrect IP address.\n");

Here ex4200fw (is in path) is the Perl script written by me which in turn calls dependent module updates.pm.

  • Do we have any tools to suite to this requirement?
  • Running Perl scripts and getting code coverage of both scripts and its dependent modules?
  • Can we accomplish the same using above CPAN modules?

Any sample script is much useful for me.

+4  A: 

Gathering coverage statistics

To gather coverage statistics you need to use Devel::Cover. In case then you can't directly change source core of included scripts you can just specify -MDevel::Cover as parameter to perl.

So you should rather change your "test script" to add this parameter when calling other Perl script like following:

my @args = ("perl", "-MDevel::Cover", "ex4200fw","-query-fw","-i","192.168.168.1");

Or you can specify enviroment variable PERL5OPT=-MDevel::Cover before top test script is executed. In this case you'll not need to change any script source. Here is a small shell sample:

## run tests and gather coverage statistics
export PERL5OPT=-MDevel::Cover
perl test1.pl
perl test2.pl
...

Calculate result coverage

There is the cover utility which outputs all lines which were executed. You should run it after all tests are executed. Standard modules are excluded from the report by default.

Ivan Nevostruev