Hack #69. Simulate Hostile Environments in Your Tests
Test devastating failures with aplomb.
When you publish a CPAN module that depends on other modules, you list the prerequisite modules in your Makefile.PL or Build.PL script.
Using Build.PL:
my $builder = Module::Build->new( # ... other Build.PL options ... requires => { 'Test::More' => 0, 'CGI' => 2.0, } );
Using Makefile.PL:
WriteMakefile( # ... other Makefile.PL options ... 'PREREQ_PM' => { 'Test::More' => 0, 'CGI' => 2.0, } );
However, there are a few ways that this standard prerequisite checking can be insufficient. First, you may have optional prerequisites. For instance, your module will use Foo::Bar
if it happens to be installed, but should fail gracefully when Foo::Bar
is absent.
Second, if the behavior of a module changed between two versions, you may still want to support both versions. For example, CGI
changed how it handles PATH_INFO
in version 3.11. Your CGI::Super_Path_Info
module probably wants to be compatible with both CGI
version 3.11 and also with earlier (and later) versions.
Finally, occasionally a user will install your module by hand to bypass the prerequisite check, hoping to use an older version of Foo::Bar
than the one you require. Sometimes your module works fine (maybe with some feature limitations), but your test suite breaks because your tests assumed the presence of a new feature.
For each of these cases. you can make your module and tests more robust. For example, you can skip tests that are incompatible ...
Get Perl Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.