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
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.
If all you want to do is run one shell command, here it is:
$ prove -vlre 'perl -MDevel::Cover -Ilib' t
This takes advantage of
--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 (
Devel::Cover and the
lib subdirectory. I use a similar technique when debugging tests.
$ HARNESS_PERL_SWITCHES=-MDevel::Cover prove -vlr t
This does almost the same thing as above without running a different executable. It sets
HARNESS_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
cover command to both see the coverage and generate web pages.
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
Now all of your test scripts will pick up that option. You can add more options by enclosing the environment variable’s value in
'. For example, you might also want to load
Devel::NYTProf for code profiling:
$ export HARNESS_PERL_SWITCHES='-MDevel::Cover -MDevel::NYTProf'
PERL5OPT environment variable also sets options for the
prove, which means that your test coverage, profiling, etc. will pick up
prove’s execution as well as your test scripts.