Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Devel::Cover for Dummies (quick start)

by toolic (Bishop)
on May 04, 2010 at 23:47 UTC ( [id://838416]=note: print w/replies, xml ) Need Help??


in reply to Devel::Cover for Dummies

This is an out-of-the-box quick start for Devel::Cover.

Here is a simple Perl module file named Foo.pm:

package Foo; use warnings; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(bar); sub bar { my $x = shift; if ($x > 10) { return 'big'; } else { print 'small'; } } 1;
Here is a single test, in a file named test.pl:
use warnings; use strict; use Foo qw(bar); use Test::More tests => 2; is(bar(77), 'big', 'check 77'); is(bar(22), 'big', 'check 22');

Both of these files are in the same directory. Run the test with coverage as follows:

perl -MDevel::Cover test.pl

I get this report as output:

1..2 Devel::Cover 0.65: Collecting coverage data for branch, condition, sta +tement, subroutine and time. Pod coverage is unavailable. Please install Pod::Coverage from CP +AN. Selecting packages matching: Ignoring packages matching: /Devel/Cover[./] Ignoring packages in: . C:/Perl-5.8/lib C:/Perl-5.8/site/lib C:/cpanfly-5.8/var/megalib ok 1 - check 77 ok 2 - check 22 Devel::Cover: Writing coverage database to C:/Perl/cov/cover_db/runs/1 +273015783.352.06536 ----------------------------------- ------ ------ ------ ------ ------ + ------ File stmt bran cond sub time + total ----------------------------------- ------ ------ ------ ------ ------ + ------ C:/Perl/lib/ActivePerl/Config.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/ActiveState/Path.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/AutoLoader.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/B.pm 14.6 16.7 16.7 12.5 95.7 + 15.0 C:/Perl/lib/B/Debug.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/B/Deparse.pm 0.1 0.0 0.3 0.2 3.5 + 0.1 C:/Perl/lib/Carp.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/Config.pm 52.6 40.0 n/a 40.0 0.0 + 47.1 C:/Perl/lib/Config_heavy.pl 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/Data/Dumper.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/Digest/base.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/DynaLoader.pm 70.0 46.4 66.7 50.0 0.7 + 61.2 C:/Perl/lib/Exporter.pm 52.1 42.9 32.4 44.4 0.0 + 43.5 C:/Perl/lib/Exporter/Heavy.pm 14.1 8.8 12.5 22.2 0.0 + 12.5 C:/Perl/lib/Fcntl.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/File/Basename.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/File/Glob.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/File/Path.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/FileHandle.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/IO.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/IO/File.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/IO/Handle.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/IO/Seekable.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/SelectSaver.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/Symbol.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/Win32.pm 26.9 0.0 0.0 16.7 0.0 + 17.2 C:/Perl/lib/XSLoader.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/base.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/bytes.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/lib/constant.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/overload.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/re.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/lib/strict.pm 84.6 50.0 50.0 100.0 0.0 + 73.1 C:/Perl/lib/vars.pm 44.4 27.3 0.0 100.0 0.0 + 31.9 C:/Perl/lib/warnings.pm 24.2 17.1 0.0 25.0 0.0 + 18.2 C:/Perl/lib/warnings/register.pm 0.0 0.0 n/a 0.0 n/a + 0.0 C:/Perl/site/lib/Cwd.pm 1.4 0.0 0.0 5.0 0.0 + 0.9 C:/Perl/site/lib/File/Spec/Unix.pm 0.0 0.0 0.0 0.0 n/a + 0.0 C:/Perl/site/lib/File/Spec/Win32.pm 52.7 37.5 30.4 46.7 0.0 + 44.6 C:/Perl/site/lib/Storable.pm 0.0 n/a n/a 0.0 n/a + 0.0 C:/Perl/site/lib/Test/Builder.pm 42.5 21.9 18.2 54.1 0.0 + 35.9 .../site/lib/Test/Builder/Module.pm 87.9 75.0 33.3 83.3 0.0 + 82.6 C:/Perl/site/lib/Test/More.pm 12.1 0.6 2.0 20.8 0.0 + 8.8 Foo.pm 90.0 50.0 n/a 100.0 0.0 + 86.7 test.pl 100.0 n/a n/a 100.0 0.0 + 100.0 Total 10.2 5.1 3.2 10.9 100.0 + 7.9 ----------------------------------- ------ ------ ------ ------ ------ + ------

It is a little verbose, but if you focus on the Foo.pm line, you'll see that only 50% of its branches were covered. This corresponds to the if branch inside the bar sub.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://838416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found