Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Devel::Cover for Dummies

by Anonymous Monk
on May 04, 2010 at 21:13 UTC ( [id://838385]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a tutorial for Devel::Cover like how to use it? Or can someone provide a simple tutorial on how to use it? I found this link Devel::Cover but I wanted something simpler.

For example, how do I get started with it? I know it mentions tests... so, if I write a test, how do I link that with Devel::Cover?

Replies are listed 'Best First'.
Re: Devel::Cover for Dummies (quick start)
by toolic (Bishop) on May 04, 2010 at 23:47 UTC
    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:

    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.

Re: Devel::Cover for Dummies
by toolic (Bishop) on May 04, 2010 at 21:23 UTC
Re: Devel::Cover for Dummies
by zwon (Abbot) on May 04, 2010 at 21:47 UTC
    how do I get started with it?

    Well, that's pretty easy. Change to your module directory and run

    cover -test
    cover_db directory will be created, with coverage.html file in it. Open that file in browser. You can follow the links and see which parts of your module aren't covered by tests. There's no need to link it with tests, it will run them itself.
Re: Devel::Cover for Dummies
by pemungkah (Priest) on May 04, 2010 at 23:05 UTC
    Devel::Cover leverages Perl's debugger hooks. Instead of stopping execution and giving you control (like the debugger), or counting the number of times you reached a statement (like Devel::NYTProf), Devel::Cover records all the places execution reached. Your test code gives you a particular place to put the code you'll be running to reach as much of your code as possible, but the fact that it runs tests is actually a side issue: what matters is how much of the program gets visited (your "coverage").

    Any code you run with -MDevel::Cover is automatically added to the coverage database. This means that sometimes you have to get creative to have Devel::Cover only record coverage for your code, instead of also recording coverage for prove, all of the Test:: modules you use, and so on.

Re: Devel::Cover for Dummies
by Hue-Bond (Priest) on May 05, 2010 at 07:44 UTC

    If you use Module::Build, there's already a ./Build target that does the coverage for you. This is an example of a brand new module bootstrapped using Module::Starter's module-starter utility:

    $ module-starter --module Foo --mb --author "some 1" --email 'some1@ex +ample.com' Created starter directories and files $ cd Foo $ perl Build.PL Checking whether your kit is complete... Looks good Checking prerequisites... Looks good Creating new 'Build' script for 'Foo' version '0.01' $ ./Build help |grep cover docs testcover html testpodcoverage
    $ ./Build testcover t/00-load.........ok 1/1# Testing Foo 0.01, Perl 5.010000, /usr/bin/pe +rl t/00-load.........ok + t/boilerplate.....ok + t/pod-coverage....ok + t/pod.............ok + All tests successful. Files=4, Tests=6, 8 wallclock secs ( 6.78 cusr + 0.45 csys = 7.23 C +PU) cover Reading database from /home/hue/tmp/Foo/cover_db ---------------------------- ------ ------ ------ ------ ------ ------ + ------ File stmt bran cond sub pod time + total ---------------------------- ------ ------ ------ ------ ------ ------ + ------ blib/lib/Foo.pm 75.0 n/a n/a 50.0 100.0 100.0 + 71.4 Total 75.0 n/a n/a 50.0 100.0 100.0 + 71.4 ---------------------------- ------ ------ ------ ------ ------ ------ + ------ Writing HTML output to /home/hue/tmp/Foo/cover_db/coverage.html ... done.
    $ ./Build testpodcoverage 1..1 ok 1 - Pod coverage on Foo

    --
     David Serrano
     (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling errors. Thank you!).

Re: Devel::Cover for Dummies
by bichonfrise74 (Vicar) on May 05, 2010 at 20:08 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://838385]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-04-18 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found