Perl Testing Modules

Learn about Perl's testing modules.

We'll cover the following

Testing modules

Test::More relies on a testing backend known as Test::Builder, which manages the test plan and coordinates the test output into TAP. This design allows multiple test modules to share the same Test::Builder backend. Consequently, the CPAN has hundreds of test modules available—and they can all work together in the same program:

  • Test::Fatal: This helps test whether our code throws (and does not throw) exceptions appropriately. We may also encounter Test::Exception.

  • Test::MockObject and Test::MockModule: These allow us to test difficult interfaces by mocking (emulating behavior to produce controlled results).

  • Test::WWW::Mechanize: This helps test web applications, while Plack::Test, Plack::Test::Agent and the subclass Test::WWW::Mechanize::PSGI can do so without using an external live web server.

  • Test::Database: This provides functions to test the use and abuse of databases. DBICx::TestDatabase helps test schemas built with DBIx::Class.

  • Test::Class: This offers an alternate mechanism for organizing test suites. It allows us to create classes in which specific methods group tests. We can inherit from test classes just as our code classes inherit from each other. This is an excellent way to reduce duplication in test suites. See Curtis Poe’s excellent Test::Class series. The newer Test::Routine distribution offers similar possibilities through the use of Moose.

  • Test::Differences: This tests strings and data structures for equality and displays any differences in its diagnostics. Test::LongString adds similar assertions.

  • Test::Deep: This tests the equivalence of nested data structures.

  • Devel::Cover: This analyzes the execution of our test suite to report on the amount of our code our tests actually exercise. In general, the more coverage the better—although 100% coverage is not always possible, 95% is far better than 80%.

  • Test::Most: This gathers several useful test modules into one parent module. It saves time and effort.

Note: See the Perl QA project for more information about testing in Perl.

Get hands-on with 1200+ tech skills courses.