Yesterday’s pair programming sessionhad Gábor Szabóand I thrashing around for a bit trying to figure out how to get test coveragestatistics for the application. The Devel::Coverdocumentation lists how to run the module several ways, but it doesn’t exactly describe how to run proveby itself rather than running a Makefile’s tests. I worked out how to do it today, and with the Baughs’ help on TwitterI worked out a few more methods.
All examples below use the bashor zshcommand shells and were tested on macOS Catalina 10.15.7 running zsh5.7.1 and Perl 5.32.1. If you’re using something very different (e.g., Microsoft Windows’ CMDor 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
This takes advantage of prove’s --execoption (abbreviated as -e) to run a different executable for tests. It recursively (-r) runs all your tests verbosely (-v) from the tdirectory while loading your application’s libraries (-l), while the perlexecutable uses (-M) Devel::Coverand the libsubdirectory. 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 Test::Harness’ HARNESS_PERL_SWITCHESenvironment variable for the duration of the provecommand. You won’t get the text output of your test coverage at the end, though, and will still have to run Devel::Cover’s covercommand 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
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::NYTProffor code profiling:
$ export HARNESS_PERL_SWITCHES='-MDevel::Cover -MDevel::NYTProf'
Why not PERL5OPT?
Setting the PERL5OPTenvironment variable also sets options for the perlrunning 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 Test2suite. But it looks like it has a --coveroption for loading and passing option to Devel::Cover.
