Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Forget about your plan, use no_plan

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../lib";
use MySimpleCalc qw(sum);

my @tests = (
    [ 1,  1,  2    ],
    [ 2,  2,  4    ],
    [ 2,  2,  2, 6 ],
    [ 3,  3,  6    ],
    [-1, -1, -2    ],
    [ 1, -1,  0    ],
);

use Test::Simple 'no_plan';

foreach my $t (@tests) {
    my $expected = pop @$t;
    my $real     = sum(@$t);
    my $name     = join " + ", @$t;

    ok( $real == $expected, $name );
}

Output:

ok 1 - 1 + 1
ok 2 - 2 + 2
not ok 3 - 2 + 2 + 2

#   Failed test '2 + 2 + 2'
#   at examples/perl/tests/t22.pl line 24.
ok 4 - 3 + 3
ok 5 - -1 + -1
ok 6 - 1 + -1
1..6
# Looks like you failed 1 test of 6.

The 1..6 is now at the end.

This is one of the solutions and in some cases it is hard to avoid it, but it is not a really good solution. Those who advocate never to put no_plan in your test say that checking if the exact number of unit tests were executed is an additional control over our test suite. Without a plan you can never be sure if - after a successful execution - the OKs you see were all the units there, or if the test script aborted in the middle and you have not executed all of the units.

There are also people who say it is not that important to have a plan but personally I am in the first camp. I think the plan is important.

There is a command called done_testing we’ll cover later on.