20

Are there any good (and preferably free) code coverage tools out there for Perl?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Kurt W. Leucht
  • 4,725
  • 8
  • 33
  • 45

4 Answers4

28

As usual, CPAN is your friend: Have a look at Devel::Cover

arhak
  • 2,488
  • 1
  • 24
  • 38
Sherm Pendley
  • 13,556
  • 3
  • 45
  • 57
  • 1
    Please don't link to a specific version of a module. Try http://search.cpan.org/perldoc?Devel::Cover instead. – cjm Oct 23 '08 at 15:21
  • I still think the perldoc link is better in this case, since it shows you the documentation. But the place to debate the merits of these link methods is http://stackoverflow.com/questions/43758/how-to-link-to-cpan-modules-in-answers – cjm Oct 23 '08 at 19:44
  • In this particular case, I think linking to the distribution is more appropriate, because there are a huge number of modules within it, as well as a tutorial. – Sherm Pendley Oct 23 '08 at 20:45
26

Yes, Devel::Cover is the way to go.

If you develop a module, and use Module::Build to manage the installation, you even have a testcover target:

 perl Build.PL
 ./Build testcover

That runs the whole test suite, and makes a combined coverage report in nice HTML, where you can browse through your modules and watch their coverage.

arhak
  • 2,488
  • 1
  • 24
  • 38
moritz
  • 12,710
  • 1
  • 41
  • 63
  • thanks, I am just a bit puzzled , 3 replies to the same question in one day, two of them almost identical – FantomX1 Mar 16 '21 at 09:55
12

As noted, Devel::Cover is your friend, but you'll want to google for it, too. It's documentation is a bit sparse and if you change your environment radically, you'll need to reinstall it because it builds Devel::Cover::Inc with a bunch of information pulled from your environment at the time you install it. This has caused plenty of problems for us at work as we have a shared CPAN environment and if one developer installs Devel::Cover and a different developer tries to run it, strange (and incorrect) results are common.

If you use this module, also check out Devel::CoverX::Covered. This module will capture much of the information which Devel::Cover throws away. It's very handy.

arhak
  • 2,488
  • 1
  • 24
  • 38
Ovid
  • 11,580
  • 9
  • 46
  • 76
5

Moritz discusses how modules built with Module::Build can use Devel::Cover easily.

For modules using ExtUtils::MakeMaker, an extension module exists to invoke the same functionality. Adding the following code before the call to WriteMakefile():

eval "use ExtUtils::MakeMaker::Coverage";
if( !$@ ) {
    print "Adding testcover target\n";
}

... will allow one to run the command 'make testcover' and have Devel::Cover perform its magic.

dland
  • 4,319
  • 6
  • 36
  • 60