开发者

How to handle code coverage of both Perl scripts and modules?

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 gettin开发者_高级运维g 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.


Gathering coverage statistics

To gather coverage statistics you need to use Devel::Cover. (If you can't directly change the source core of included scripts you can just specify -MDevel::Cover as a 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜