ZetCode

Perl Test::More

last modified October 20, 2026

Test::More is the standard core module for writing tests in Perl. It provides functions such as ok, is, is_deeply, and like that compare values and report the result in the Test Anything Protocol (TAP) format. In this article we write small test programs, read their output, and run them with prove.

A first test with ok and is

A test script declares how many tests it runs, either with tests => N in the use line or with done_testing at the end. Each assertion prints one line starting with ok or not ok.

basic_test.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;
use Test::More tests => 6;

sub add { return $_[0] + $_[1] }

ok(defined &add, 'add is defined');
is(add(2, 3), 5, 'add two positive numbers');
is(add(-1, 1), 0, 'add negative and positive');
isnt(add(1, 1), 3, 'result is not 3');
like('Hello, World', qr/World/, 'string contains World');
is_deeply([ map { $_ * 2 } 1 .. 3 ], [2, 4, 6], 'doubling a list');

ok checks that its argument is true. is compares two strings with eq, and isnt is its negation. The like function matches a regular expression, and is_deeply compares nested array and hash structures recursively, which is cannot do for references. The last argument of every function is an optional description that appears in the output.

$ perl basic_test.pl
1..6
ok 1 - add is defined
ok 2 - add two positive numbers
ok 3 - add negative and positive
ok 4 - result is not 3
ok 5 - string contains World
ok 6 - doubling a list

Reading a failing test

When an assertion fails, Test::More prints the line as not ok and then a diagnostic that shows the value it got and the value it expected. The exit status of the script equals the number of failed tests, so a continuous integration system can detect failures.

failing_test.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;
use Test::More tests => 3;

sub mul { return $_[0] * $_[1] }

is(mul(3, 4), 12, 'multiply 3 by 4');
is(mul(2, 2), 5, 'multiply 2 by 2 (wrong expectation)');
ok(mul(0, 9) == 0, 'zero times nine');

The second assertion expects the wrong value, so the diagnostic reports both the value got and the value expected. The description given in the call tells us which line to look at.

$ perl failing_test.pl
1..3
ok 1 - multiply 3 by 4
not ok 2 - multiply 2 by 2 (wrong expectation)
ok 3 - zero times nine
#   Failed test 'multiply 2 by 2 (wrong expectation)'
#   at failing_test.pl line 11.
#          got: '4'
#     expected: '5'
# Looks like you failed 1 test of 3.

Plans, done_testing, and subtests

When the number of tests is not known in advance, leave the plan out and end the script with done_testing. Subtests group related assertions under one name, and each subtest can declare its own plan.

done_testing.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;
use Test::More;

my @cases = ([1, 'one'], [2, 'two'], [3, 'three']);

for my $case (@cases) {
    my ($num, $word) = @$case;
    my @words = qw(zero one two three);
    is($words[$num], $word, "word for $num");
}

subtest 'nested checks' => sub {
    plan tests => 2;
    ok(1, 'first nested');
    ok(1, 'second nested');
};

done_testing();

The loop runs one assertion per case, so the number of tests is determined at run time. done_testing records how many tests ran and prints the final plan. A subtest is reported as a single test line in the parent, and its own results are indented beneath it.

$ perl done_testing.pl
ok 1 - word for 1
ok 2 - word for 2
ok 3 - word for 3
# Subtest: nested checks
    1..2
    ok 1 - first nested
    ok 2 - second nested
ok 4 - nested checks
1..4

Running tests with prove

The prove program, installed with Perl, runs one or more test files and summarizes the results. Given a directory, it runs every file ending in .t. Using the .t extension is the convention for test files.

$ prove -v basic_test.pl
basic_test.pl ..
1..6
ok 1 - add is defined
...
ok 6 - doubling a list
ok
All tests successful.
Files=1, Tests=6

Best Practices

In this article we have covered Test::More, including ok, is, isnt, like, is_deeply, subtests, done_testing, and running tests with prove.

Source

Test::More ยท prove

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.