Know-How für Ihr Projekt

Perl Documentation


Test::More - yet another framework for writing test scripts


use Test::More tests => 23;
# or
use Test::More skip_all => $reason;
# or
use Test::More;   # see done_testing()
require_ok( 'Some::Module' );
# Various ways to say "ok"
ok($got eq $expected, $test_name);
is  ($got, $expected, $test_name);
isnt($got, $expected, $test_name);
# Rather than print STDERR "# here's what went wrong\n"
diag("here's what went wrong");
like  ($got, qr/expected/, $test_name);
unlike($got, qr/expected/, $test_name);
cmp_ok($got, '==', $expected, $test_name);
is_deeply($got_complex_structure, $expected_complex_structure, $test_name);
    skip $why, $how_many unless $have_some_feature;
    ok( foo(),       $test_name );
    is( foo(42), 23, $test_name );
    local $TODO = $why;
    ok( foo(),       $test_name );
    is( foo(42), 23, $test_name );
can_ok($module, @methods);
isa_ok($object, $class);
my @status = Test::More::status;


STOP! If you're just getting started writing tests, have a look at Test::Simple first. This is a drop in replacement for Test::Simple which you can switch to once you get the hang of basic testing.

The purpose of this module is to provide a wide range of testing utilities. Various ways to say "ok" with better diagnostics, facilities to skip tests, test future features and compare complicated data structures. While you can do almost anything with a simple ok() function, it doesn't provide good diagnostic output.

I love it when a plan comes together

Before anything else, you need a testing plan. This basically declares how many tests your script is going to run to protect against premature failure.

The preferred way to do this is to declare a plan when you use Test::More.

use Test::More tests => 23;

There are cases when you will not know beforehand how many tests your script is going to run. In this case, you can declare your tests at the end.

use Test::More;
... run your tests ...
done_testing( $number_of_tests_run );

Sometimes you really don't know how many tests were run, or it's too difficult to calculate. In which case you can leave off $number_of_tests_run.

In some cases, you'll want to completely skip an entire testing script.

use Test::More skip_all => $skip_reason;

Your script will declare a skip with the reason why you skipped and exit immediately with a zero (success). See Test::Harness for details.

If you want to control what functions Test::More will export, you have to use the 'import' option. For example, to import everything but 'fail', you'd do:

use Test::More tests => 23, import => ['!fail'];

Alternatively, you can use the plan() function. Useful for when you have to calculate the number of tests.

use Test::More;
plan tests => keys %Stuff * 3;

or for deciding between running the tests at all:

use Test::More;
if( $^O eq 'MacOS' ) {
    plan skip_all => 'Test irrelevant on MacOS';
else {
    plan tests => 42;

Test names

By convention, each test is assigned a number in order. This is largely done automatically for you. However, it's often very useful to assign a name to each test. Which would you rather see:

ok 4
not ok 5
ok 6


ok 4 - basic multi-variable
not ok 5 - simple exponential
ok 6 - force == mass * acceleration

The later gives you some idea of what failed. It also makes it easier to find the test in your script, simply search for "simple exponential".

All test functions take a name argument. It's optional, but highly suggested that you use it.

I'm ok, you're not ok.

The basic purpose of this module is to print out either "ok #" or "not ok #" depending on if a given test succeeded or failed. Everything else is just gravy.

All of the following print "ok" or "not ok" depending on if the test succeeded or failed. They all also return true or false, respectively.

Module tests

Sometimes you want to test if a module, or a list of modules, can successfully load. For example, you'll often want a first test which simply loads all the modules in the distribution to make sure they work before going on to do more complicated testing.

For such purposes we have use_ok and require_ok.

Complex data structures

Not everything is a simple eq check or regex. There are times you need to see if two data structures are equivalent. For these instances Test::More provides a handful of useful functions.

NOTE I'm not quite sure what will happen with filehandles.


If you pick the right test function, you'll usually get a good idea of what went wrong when it failed. But sometimes it doesn't work out that way. So here we have ways for you to write your own diagnostic messages which are safer than just print STDERR.

Conditional tests

Sometimes running a test under certain conditions will cause the test script to die. A certain function or method isn't implemented (such as fork() on MacOS), some resource isn't available (like a net connection) or a module isn't available. In these cases it's necessary to skip tests, or declare that they are supposed to fail but will work in the future (a todo test).

For more details on the mechanics of skip and todo tests see Test::Harness.

The way Test::More handles this is with a named block. Basically, a block of tests which can be skipped over or made todo. It's best if I just show you...

Test control

Discouraged comparison functions

The use of the following functions is discouraged as they are not actually testing functions and produce no diagnostics to help figure out what went wrong. They were written before is_deeply() existed because I couldn't figure out how to display a useful diff of two arbitrary data structures.

These functions are usually used inside an ok().

ok( eq_array(\@got, \@expected) );

is_deeply() can do that better and with diagnostics.

is_deeply( \@got, \@expected );

They may be deprecated in future versions.

Extending and Embedding Test::More

Sometimes the Test::More interface isn't quite enough. Fortunately, Test::More is built on top of Test::Builder which provides a single, unified backend for any test library to use. This means two test libraries which both use <Test::Builder> can be used together in the same program>.

If you simply want to do a little tweaking of how the tests behave, you can access the underlying Test::Builder object like so:


If all your tests passed, Test::Builder will exit with zero (which is normal). If anything failed it will exit with how many failed. If you run less (or more) tests than you planned, the missing (or extras) will be considered failures. If no tests were ever run Test::Builder will throw a warning and exit with 255. If the test died, even after having successfully completed all its tests, it will still be considered a failure and will exit with 255.

So the exit codes are...

0                   all tests successful
255                 test died or all passed but wrong # of tests run
any other number    how many failed (including missing or extras)

If you fail more than 254 tests, it will be reported as 254.

NOTE This behavior may go away in future versions.


Test::More works with Perls as old as 5.8.1.

Thread support is not very reliable before 5.10.1, but that's because threads are not very reliable before 5.10.1.

Although Test::More has been a core module in versions of Perl since 5.6.2, Test::More has evolved since then, and not all of the features you're used to will be present in the shipped version of Test::More. If you are writing a module, don't forget to indicate in your package metadata the minimum version of Test::More that you require. For instance, if you want to use done_testing() but want your test script to run on Perl 5.10.0, you will need to explicitly require Test::More > 0.88.

Key feature milestones include:

There is a full version history in the Changes file, and the Test::More versions included as core can be found using Module::CoreList:

$ corelist -a Test::More



This is a case of convergent evolution with Joshua Pritikin's Test module. I was largely unaware of its existence when I'd first written my own ok() routines. This module exists because I can't figure out how to easily wedge test names into Test's interface (along with a few other problems).

The goal here is to have a testing utility that's simple to learn, quick to use and difficult to trip yourself up with while still providing more flexibility than the existing As such, the names of the most common routines are kept tiny, special cases and magic side-effects are kept to a minimum. WYSIWYG.



Test::Simple if all this confuses you and you just want to write some tests. You can upgrade to Test::More later (it's forward compatible).

Test::Legacy tests written with, the original testing module, do not play well with other testing libraries. Test::Legacy emulates the interface and does play well with others.


Fennec The Fennec framework is a testers toolbox. It uses Test::Builder under the hood. It brings enhancements for forking, defining state, and mocking. Fennec enhances several modules to work better together than they would if you loaded them individually on your own.

Fennec::Declare Provides enhanced (Devel::Declare) syntax for Fennec.


Test::Differences for more ways to test complex data structures. And it plays well with Test::More.

Test::Class is like xUnit but more perlish.

Test::Deep gives you more powerful complex data structure testing.

Test::Inline shows the idea of embedded testing.

Mock::Quick The ultimate mocking library. Easily spawn objects defined on the fly. Can also override, block, or reimplement packages as needed.

Test::FixtureBuilder Quickly define fixture data for unit tests.


Test::Harness is the test runner and output interpreter for Perl. It's the thing that powers make test and where the prove utility comes from.


Bundle::Test installs a whole bunch of useful test modules.

Test::Most Most commonly needed test functions and features.


Michael G Schwern <> with much inspiration from Joshua Pritikin's Test module and lots of help from Barrie Slaymaker, Tony Bowden,, chromatic, Fergal Daly and the perl-qa gang.



See to report and view bugs.


The source code repository for Test::More can be found at


Copyright 2001-2008 by Michael G Schwern <>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.