DEV Community

Mark Gardner
Mark Gardner

Posted on • Originally published at phoenixtrap.com on

Perl test coverage when you don’t have a Makefile

Yesterday’s pair programming session had Gábor Szabó and I thrashing around for a bit trying to figure out how to get test coverage statistics for the application. The Devel::Cover documentation lists how to run the module several ways, but it doesn’t exactly describe how to run prove by itself rather than running a Makefile’s tests. I worked out how to do it today, and with the Baughs’ help on Twitter I worked out a few more methods.

All examples below use the bash or zsh command shells and were tested on macOS Catalina 10.15.7 running zsh 5.7.1 and Perl 5.32.1. If you’re using something very different (e.g., Microsoft Windows’ CMD or PowerShell), you may have to set environment variables differently.

Ad-hoc test coverage

If all you want to do is run one shell command, here it is:

$ prove -vlre 'perl -MDevel::Cover -Ilib' t
Enter fullscreen mode Exit fullscreen mode

This takes advantage of prove’s --exec option (abbreviated as -e) to run a different executable for tests. It recursively (-r) runs all your tests verbosely (-v) from the t directory while loading your application’s libraries (-l), while the perl executable uses (-M) Devel::Cover and the lib subdirectory. I use a similar technique when debugging tests.

$ HARNESS_PERL_SWITCHES=-MDevel::Cover prove -vlr t
Enter fullscreen mode Exit fullscreen mode

This does almost the same thing as above without running a different executable. It sets Test::HarnessHARNESS_PERL_SWITCHES environment variable for the duration of the prove command. You won’t get the text output of your test coverage at the end, though, and will still have to run Devel::Cover’s cover command to both see the coverage and generate web pages.

In a dedicated test session, window, or tab

If you have a terminal session, window, or tab dedicated solely to running your tests, set one of the environment variables above for that session:

$ export HARNESS_PERL_SWITCHES=-MDevel::Cover
Enter fullscreen mode Exit fullscreen mode

Now all of your test scripts will pick up that option. You can add more options by enclosing the environment variable’s value in 'quotes'. For example, you might also want to load Devel::NYTProf for code profiling:

$ export HARNESS_PERL_SWITCHES='-MDevel::Cover -MDevel::NYTProf'
Enter fullscreen mode Exit fullscreen mode

Why not PERL5OPT?

Setting the PERL5OPT environment variable also sets options for the perl running prove, which means that your test coverage, profiling, etc. will pick up prove’s execution as well as your test scripts.

What about yath?

I don’t know for sure; I don’t use the Test2 suite. But it looks like it has a --cover option for loading and passing option to Devel::Cover.

Top comments (0)