Hack #68. Run Test Suites Persistently

Speed up your tests.

Large Perl applications with many interconnected modules can take a long time to start up. Perl needs to load, compile, and initialize all of the modules before it can start running your application.

Tests for a large system can be particularly slow. A test suite typically contains lots of small short-lived scripts, each of which pulls in lots of module code at start up. A few seconds of delay per script can add up to a lot of time spent waiting for your test suite to finish.

The cure for long startup times within web-based applications is to run under a persistent environment such as mod_perl or PersistentPerl. PersistentPerl works for command-line programs as well. It's usually as simple as changing the shebang line from #!/usr/bin/perl to #!/usr/bin/perperl.

Running your test suite persistently is slightly more complicated, and doesn't work for every test, but the benefit is a huge speed increase for most of your tests. Running your test suite persistently can speed up your tests by a factor of five on a slow machine.

The Hack

The first step of the hack is to make Test::Builder-based scripts compatible with PersistentPerl. There are several parts to this:

  • The script has to reset the Test::Builder counter on startup.

  • The script needs to prevent Test::Builder from duplicating STDOUT and STDERR, as this seems to be incompatible with PersistentPerl.

  • Scripts with no_plan have to register a PersistentPerl cleanup handler to ...

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.