Devel::Cover - An Introduction
Braga, Portugal -- 1st September 2005
Paul Johnson
www.pjcj.net
What is Devel::Cover
- Testing is important
- But how do you know if you are doing it correctly?
- Code coverage can help
- Devel::Cover is the code coverage tool for Perl
- Let's look at how it is used
Preparation
- Devel::Cover is on CPAN
- Download and install it
Tools you will need
- A C compiler
- Perl 5.6.1 at least
Using Devel::Cover
Using Devel::Cover
-
cover -delete
-
HARNESS_PERL_SWITCHES=-MDevel::Cover make test
-
cover
A worked example
- Shell::Source
- Sources shell scripts into your Perl program
- It's mine
- Download from CPAN as usual
- No need to install it
Normal Test
$ make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" \
"-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/bash....ok
t/csh.....ok
t/ksh.....ok
t/sh......ok
t/tcsh....ok
t/zsh.....ok
All tests successful.
Files=6, Tests=24, 1 wallclock secs ( 0.49 cusr + 0.09 csys = 0.58 CPU)
Normal Test
$ make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" \
"-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/bash....ok
t/csh.....ok
t/ksh.....ok
t/sh......ok
t/tcsh....ok
t/zsh.....ok
All tests successful.
Files=6, Tests=24, 1 wallclock secs ( 0.49 cusr + 0.09 csys = 0.58 CPU)
Normal Test
$ make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" \
"-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/bash....ok
t/csh.....ok
t/ksh.....ok
t/sh......ok
t/tcsh....ok
t/zsh.....ok
All tests successful.
Files=6, Tests=24, 1 wallclock secs ( 0.49 cusr + 0.09 csys = 0.58 CPU)
Coverage Run
$ cover -test
Deleting database /home/pjcj/g/perl/Shell-Source-0.01/cover_db
PERL_DL_NONLAZY=1 /usr/local/pkg/cover/c1/perl-5.8.7/bin/perl "-MExtUtils::Command::MM" \
"-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/bash....ok
t/csh.....ok
t/ksh.....ok
t/sh......ok
t/tcsh....ok
t/zsh.....ok
All tests successful.
Files=6, Tests=24, 17 wallclock secs (16.16 cusr + 1.01 csys = 17.17 CPU)
Reading database from /home/pjcj/g/perl/Shell-Source-0.01/cover_db
---------------------------- ------ ------ ------ ------ ------ ------ ------
File stmt bran cond sub pod time total
---------------------------- ------ ------ ------ ------ ------ ------ ------
blib/lib/Shell/Source.pm 96.6 65.0 66.7 90.9 n/a 100.0 86.1
Total 96.6 65.0 66.7 90.9 n/a 100.0 86.1
---------------------------- ------ ------ ------ ------ ------ ------ ------
Writing HTML output to /home/pjcj/g/perl/Shell-Source-0.01/cover_db/coverage.html ...
done.
Coverage Results
- Reminds us which database we are using
- Table showing each file
- Percentage of coverage achieved
- Fliename is a link
File Coverage
- Start with subroutine coverage
Subroutine Coverage
- Start with subroutine coverage
- ten of eleven subroutines run
- all run six times except for env
- BEGIN comes from
-
use Module LIST
- which translates to
-
BEGIN { require Module; import Module LIST }
- pod n/a => not documented
Statement Coverage
- 56 of 58 statements executed
- those in env uncovered
- testing env will bring 100% subroutine coverage and 100% subroutine coverage
- Start with subroutine coverage