CPAN is filled with great modules to help you test your code. Here's a short list of what's available, so you don't reinvent the wheel.
If you have a module to add to the list,
please email andy@petdance.com
.
Assertion-like testing while your program runs.
Compares two functions to ensure one is faster than another:
use Test::Benchmark; is_faster(-10, sub {...}, sub {...}, "this is faster than that")
xUnit style testing that works well with Test::Builder
based modules (the majority of other testing modules). This means it can integrate into an existing setup relatively easily.
Testing of scripts/programs by their external interface rather than innards.
Comprises 4 submodules that provide a number of functions to test the contents of arrays, hashes, scalars and functions. These make it easy to test various boundaries and edge cases without writing much code.
Emits a trace of the test script to file, for perusal in the event of failures.
Extremely flexible and powerful deep comparisons of data structures. Ideal for testing that a given structure not only is equal to an ideal model, but also for determining whether they are similar. Also good for checking lightly nested structures (e.g. that a response from an address book query has well formed records).
Tests long strings and data structures, giving a readable output (unlike testing long strings with Test::More
's is
function).
Another xUnit-ish testing framework. Very easy to use. Able to output both xUnit style and traditional Perl style.
Provides a CGI interface to writing and running test suites.
Tests file attributes (empty, of particular sizes, readable, executable, writeable, etc.).
Allows you to embed your tests in your POD in your program file, thus allowing you to test while you document, or test while you code, or both. One sideeffect is that your sample code can be test code.
LectroTest is an automatic, specification-based testing tool for Perl. The project was inspired by Koen Claessen and John Hughes's QuickCheck for the Haskell programming language.
Testing a hypothetical square function:
Property { ##[ x <- Int #]## square( $x ) == $x * $x; }, name => "Finding $x's square";
Testing of long strings, allowing one to see precisely where the inequality, or non-match, is with ease.
Combinatorial subroutine argument testing. Lets you test every combination of arguments to a function.
Easily override subroutines in a module for unit testing.
Lets you create objects that respond to methods in particular ways, thus letting you test how your own code handles objects with such return values.
The most common testing module to use. Appropriate feature set for the majority of test cases and now standard in Perl (from 5.6.2 and 5.8.0).
Module to let you send make test results to the CPAN testers. Includes the handy cpantest program to make command-line reporting easy.
The basic testing module, which you'll rarely want to use on its own, since Test::More
completely supercedes it.
Unit test wrapper functions around the standard testing functions. Allows you to specify setup functions, plus teardown functions to get run in the case of failure.
Framework for testing distributions of Perl itself. Not normally used by the general public.
JUnit testing for Perl.
Runs verbose tests on a test file. Seems to be obviated by the prove program now included with Test::Harness
.
DBD::Mock is a DBI-compliant database driver that allows you to prepare and execute SQL statements, seed resultsets, and inspect the results of all calls.
An example from the SYNOPSIS:
use DBI; # ...connect as normal, using 'Mock' as your driver name my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) || die "Cannot create handle: $DBI::errstr\n"; # ...create a statement handle as normal and execute with parameters my $sth = $dbh->prepare( 'SELECT this, that FROM foo WHERE id = ?' ); $sth->execute( 15 ); # Now query the statement handle as to what has been done with it my $params = $sth->{mock_params}; print "Used statement: ", $sth->{mock_statement}, "\n", "Bound parameters: ", join( ', ', @{ $params } ), "\n";
Given a DBI database handle, can test to see if the database contains tables with particular rows. Useful for testing that database modifications went well.
# SQL-based test row_ok( sql => "SELECT * FROM contacts WHERE cid = '123'", tests => [ name => "trelane" ], label => "contact 123's name is trelane"); # test with shortcuts row_ok( table => "contacts", where => [ cid => 123 ], tests => [ name => "trelane" ], label => "contact 123's name is trelane");
Tests that one's environment has appropriate data.
Tests files and directories have particular contents.
Verify ISBNs and parts of ISBNs against Business::ISBN
.
Testing that email is sent and received properly.
Helper functions for testing XML for syntactic equivalency, even if the strings themselves are different.
Allows you to run tests for Apache-based software. Also lets you run tests inside Apache. Not limited to mod_perl.
http://www.modperlcookbook.org/~geoff/slides/ApacheCon/2003/apache-test-printable.ppt.gz
Tests HTML has (or hasn't) particular links, text, tags, comments and such. Very useful for testing websites (see also WWW::Mechanize
and Test:HTML::Lint
).
Checks that HTML is well formed as per HTML::Lint
.
Checks that HTML is well formed as per HTML::Tidy
.
Ensures HTTP responses have appropriate values.
Subclass of WWW::Mechanize
with convenience methods for automated testing.
Runs tests on other Test::
modules based on Test::Builder
.
Performs basic Kwalitee checking on modules: checks that they have well formed POD, they compile, they all define the same $VERSION
, they have their prerequisites in the Makefile.PL and that some standard files exist.
Testing that bits of code die or don't die appropriately, and with the right errors.
Executes your tests in a particular non-alphabetic order without having to name them with numeric prefixes.
Test for making sure that your test didn't emit any warnings.
Checks a given POD or Perl file for syntactic validity. Also has a function to let you easily check all files in your distribution at once.
Checks a given POD or Perl file for syntactic validity. Also has a function to let you easily check all files in your distribution at once.
Checks the portability the names of the files in a distribution. The tests use the advice listed in perlport, section "Files and Filesystems".
Checks that your Makefile.PL has the proper prerequisites specified.
Verifies that the signature of a module, as created by Module::Signature
, is correct.
Runs tests on other Test::
modules based on Test::Builder
.
Checks module files for proper version information.
Handy module for testing code that might throw a warning. The testing function captures any warnings, so you can check them against a regex.
For example, this code checks that WWW::Mechanize
's warn()
method does indeed throw a warning.
my $m = WWW::Mechanize->new; isa_ok( $m, 'WWW::Mechanize' ); warning_like { $m->warn( "Something bad" ); } qr/Something bad.+line \d+/, "Passes the message, and includes the line number";
Allows you to deliberately hide modules from a program even though they are installed. This is mostly useful for testing modules that have a fallback when a certain dependency module is not installed.
The following people have helped contribute to this page: Andy Lester, Dominic Mitchell Philipe 'BooK' Bruhat, Chris Winters, Per Christian Nødtved, and the late, great Iain Truskett.