Perl Test Coverage When You Don't Have a Makefile
Detect bugs earlier in your Perl application by testing coverage as you develop.
Join the DZone community and get the full member experience.Join For Free
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.
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:
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.
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.
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:
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:
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.
Published at DZone with permission of Mark Gardner, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.