Általában modulokkal kapcsolatban kerül szóba a tesztelés. Ajánlott olvasmányok kezdő tesztelőknek:

  • Test::Tutorial – A tutorial about writing really basic tests.
  • Test::Simple – Basic utilities for writing tests.
  • Test::More – Yet another framework for writing test scripts.

A Simple mindössze egyetlen assert utasítást tartalmaz, ez az ok($test_result, $test_name), amelynek egyetlen kötelező paramétere a boolean teszteredmény. Opcionális paraméter a teszt szöveges megnevezése. Íme a lényeg a doksiból:

ok() is given an expression (in this case $foo eq $bar). If it’s true, the test passed. If it’s false, it didn’t. That’s about it. ok() prints out either “ok” or “not ok” along with a test number (it keeps track of that for you). If you provide a $name, that will be printed along with the “ok/not ok” to make it easier to find your test when if fails (just search for the name). It also makes it easier for the next guy to understand what your test is for. It’s highly recommended you use test names. […] The only other constraint is you must pre-declare how many tests you plan to run. This is in case something goes horribly wrong during the test and your test program aborts, or skips a test or whatever. You do this like so: use Test::Simple tests => 23;. You must have a plan.

A More a Simple kiterjesztett változata, 100%-ban kompatibilis vele az API-ja, tehát a Simple bármikor lecserélhető More-ra. Ebben többféle assert utasítást is találunk, az is($got, $expected, $test_name) és az isnt($got, $expected, $test_name) jobban követhetővé teszik a hibákat:

Similar to ok(), is() and isnt() compare their two arguments with eq and ne respectively and use the result of that to determine if the test succeeded or failed. So why use these? They produce better diagnostics on failure. ok() cannot know what you are testing for (beyond the name), but is() and isnt() know what the test was and why it failed.

A teszteket általában t kiterjesztésű fájlokba írjuk, modulok esetében ezek egy t nevű mappába kerülnek, és a prove parancs automatikusan futtatja e mappa tesztjeit. Mit tegyünk, ha csak egy egyszerű szkriptet szeretnénk tesztelni? Például beleírhatjuk a teszteket magába a szkriptbe eképpen:

#!/usr/bin/env perl

use v5.018;
use utf8;
use strict;
use warnings;
use feature qw(unicode_strings);
use Getopt::Long;
use Test::More;

{
    GetOptions(
        # ...egyéb opciók helye...
        'selftest' => sub { run_tests() },
    );
    # ...a program lényegi részének helye...
    do_something();
}

sub do_something {
    return 42
}

sub run_tests {
    ok(1 == 1, 'This is a passing test.');
    is(do_something(), 1, 'This is a failing test.');
    done_testing(); # plan helyett használható More-ban
    exit 0
}