Testing for warnings

Test code that should give a warning, and check if that is the correct warning.

So once we have tested our nicely behaving code we can also test our warnings and errors. For this we are going to use several additional modules from CPAN. As they all use the Test::Builder backend we can use them along with our standard Test::More setup.

use strict;
use warnings;

use Test::More tests => 2;
use Test::Warn;

use lib 'lib';
use MyTools;

    my $result;
    warning_is {$result = fibonacci(-1)} "Given number must be > 0",
        'warning when called with -1';
    is($result, 0, 'fibonacci on -1 returns 0');

ok 1 - warning when called with -1
ok 2 - fibonacci on -1 returns 0