Writing tests - Test-More/test-more GitHub Wiki

Writing tests for a test library is a bit special.

We test ourselves with a separate test library called t/test.pl. It comes from the Perl core. t/test.pl implements most of the Test::More interface, and then some, without relying on it. This lets us test ourselves even when we're broken. For all practical purposes, use it like Test::More.

Here is a test boilerplate:

#!/usr/bin/env perl -w

# A short explanation of what this test is doing

use strict;
use warnings;

BEGIN { require "t/test.pl" }

note "What this block of tests is doing"; {
    ...test code here...
}

note "What this other block of tests is doing"; {
    ...test code here...
}

...and so on...

done_testing;

Test blocks serve to split the test up into manageable chunks, both in terms of variable scope and how much you have to read. They also forgive not putting a name on each and every test.

Each test block should strive to handle one method or one situation. For example...

note "Passing result"; {
    my $result = Test::Builder2::Result->new_result( pass => 1 );

    is $result->type, 'pass';
    ok $result->is_pass;
    ok !$result->is_fail;
    ok !$result->is_todo;
    ok !$result->is_skip;
    ok $result;
}

note "Failing result"; {
    my $result = Test::Builder2::Result->new_result( pass => 0 );

    is $result->type, 'fail';
    ok !$result->is_pass;
    ok $result->is_fail;
    ok !$result->is_todo;
    ok !$result->is_skip;
    ok !$result;
}