Jun 21, 2011

How to use Devel::Cover

It's time to deploy another version of my CPAN module. I have 57 files and 698 tests: the question is

Are my tests "quite" good? Did they cover all possible cases?

Mmh, it's a difficult question, the answer depends on a lot of things ... here comes Devel::Cover to the rescue.

So we have this magic module that gives us some statistics to evaluate where are the gaps in our tests.

Yes, I know, it is still a difficult question, but at least Devel::Cover gives us some answers, for sure it is a good starting point.

Consider I am on Windows, so I have "set" instead of "export" and dmake instead of make.
 If it is not the first time you run it, launch a

C:\SVN_ROOT\PNI>cover -delete
Deleting database C:\SVN_ROOT\PNI\cover_db

Just set the environment variable HARNESS_PERL_SWITCHES=-MDevel::Cover and run your tests.
For example

C:\SVN_ROOT\PNI>set HARNESS_PERL_SWITCHES=-MDevel::Cover
C:\SVN_ROOT\PNI>prove -l

Here it is the cool part !

C:\SVN_ROOT\PNI>cover
Reading database from C:\SVN_ROOT\PNI\cover_db
...

Writing HTML output to C:\SVN_ROOT\PNI\cover_db/coverage.html ...
done.

This will produce a coverage.html file in the cover_db directory.

Noooooo, my _node-perlfunc-int.t has a red entry with a 50% ... mmh maybe because the test file is almost empty, it just checks default values.


If I click on that ugly 50.0, I can see this.


I'm going to add some tests, checking some results and for example passing wrong parameters. This is the result:

C:\SVN_ROOT\PNI>prove -l t\_node-perlfunc-int.t
t\_node-perlfunc-int.t .. 1/? Devel::Cover: Deleting old coverage for changed file t/_node-perlfunc-int.t
t\_node-perlfunc-int.t .. ok
All tests successful.
Files=1, Tests=9,  2 wallclock secs ( 0.06 usr +  0.01 sys =  0.08 CPU)
Result: PASS

et voilĂ 



Conclusion

Devel::Cover is a really powerful tool ... just use it !

No comments:

Post a Comment