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

done_testing

I am not a fan of it, but in rare cases it is useful to know that done_testing can be used to signal all tests have been done. This way we don’t need to have a “plan”.

use strict;
use warnings;

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

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

use Test::More;
plan tests => scalar @tests;
#use Test::More 'no_plan';
#use Test::More;

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

    ok( $real == $expected, $name );
    #exit if $expected == 6;
    #last if $expected == 6;
}

done_testing();

What happens if we have lots of tests and we would like to avoid running the later ones during development? What if we add (enable) the line with the exit or the line with the last call?

  • If we had a plan declared then the harness will notice you did not run the expected number of tests.

  • If we had no_plan then Test::More will be happy that there were 3 successful calls to ok and won’t notice that we did not run 1000 other test cases.

  • If we only have use Test::More; at the top and then call done_testing to indicate we have reached the end of all the tests, this will report failure if we enable the line with the exit, but it will still be happy if we enabled the line with `last.

In other words, if you want to make sure all the tests run, then you need to set the plan.

OTOH Pytest does not have it.