Perl test coverage when you don’t have a Makefile

Yesterday’s pair pro­gram­ming ses­sionhad Gábor Szabóand I thrash­ing around for a bit try­ing to fig­ure out how to get test cov­er­agesta­tis­tics for the appli­ca­tion. The Devel::Coverdoc­u­men­ta­tion lists how to run the mod­ule sev­er­al ways, but it does­n’t exact­ly describe how to run proveby itself rather than run­ning 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 exam­ples below use the bashor zshcom­mand shells and were test­ed on macOS Catalina 10.15.7 run­ning zsh5.7.1 and Perl 5.32.1. If you’re using some­thing very dif­fer­ent (e.g., Microsoft Windows’ CMDor PowerShell), you may have to set envi­ron­ment vari­ables differently.

Ad-​hoc test coverage

If all you want to do is run one shell com­mand, here it is:

$ prove -vlre 'perl -MDevel::Cover -Ilib' t

This takes advan­tage of prove​’s --execoption (abbre­vi­at­ed as -e) to run a dif­fer­ent exe­cutable for tests. It recur­sive­ly (-r) runs all your tests ver­bose­ly (-v) from the tdirec­to­ry while load­ing your appli­ca­tion’s libraries (-l), while the perlexe­cutable uses (-M) Devel::Coverand the libsub­di­rec­to­ry. I use a sim­i­lar tech­nique when debug­ging tests.

$ HARNESS_PERL_SWITCHES=-MDevel::Cover prove -vlr t

This does almost the same thing as above with­out run­ning a dif­fer­ent exe­cutable. It sets Test::Harness’ HARNESS_PERL_SWITCHESenvi­ron­ment vari­able for the dura­tion of the provecom­mand. You won’t get the text out­put of your test cov­er­age at the end, though, and will still have to run Devel::Cover​’s covercom­mand to both see the cov­er­age and gen­er­ate web pages.

In a dedicated test session, window, or tab

If you have a ter­mi­nal ses­sion, win­dow, or tab ded­i­cat­ed sole­ly to run­ning your tests, set one of the envi­ron­ment vari­ables 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 enclos­ing the envi­ron­ment vari­able’s val­ue in 'quotes'. For exam­ple, you might also want to load Devel::NYTProffor code profiling:

$ export HARNESS_PERL_SWITCHES='-MDevel::Cover -MDevel::NYTProf'

Why not PERL5OPT?

Setting the PERL5OPTenvi­ron­ment vari­able also sets options for the perlrun­ning prove, which means that your test cov­er­age, pro­fil­ing, etc. will pick up prove​’s exe­cu­tion 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 load­ing and pass­ing option to Devel::Cover.


Discover more from The Phoenix Trap

Subscribe to get the latest posts sent to your email.

Mark Gardner Avatar

Hi, I’m Mark.

Hi, I’m Mark Gard­ner, and this is my personal blog. I show software developers how to level up by building production-ready things that work. Clear code, real projects, lessons learned.

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.