Test for no warnings - the hard way


If we can test our code for specific warnings we should also test that in other places there are no warnings.


examples/test-warn/t/fibonacci_test_warn.t
use strict;
use warnings;

use Test::More;
use Test::Warn;

use MyTools qw(fibo);

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

subtest positive_4 => sub {
    my $result;
    warning_is {$result = fibo(4)} undef, 'no warning here';
    is($result, 3, 'fibonacci on 4 returns 3');
};

subtest positive_6 => sub {
    my $result;
    warning_is {$result = fibo(6)} undef, 'no warning here';
    is($result, 8, 'fibonacci on 6 returns 8');
};

done_testing;

prove -lv t/fibonacci_test_warn.t


t/fibonacci_test_warn.t .. 
# Subtest: negative
    ok 1 - warning when called with -1
    ok 2 - fibonacci on -1 returns undef
    1..2
ok 1 - negative
# Subtest: positive_4
    ok 1 - no warning here
    ok 2 - fibonacci on 4 returns 3
    1..2
ok 2 - positive_4
# Subtest: positive_6
    ok 1 - no warning here
    ok 2 - fibonacci on 6 returns 8
    1..2
ok 3 - positive_6
1..3
ok
All tests successful.
Files=1, Tests=3,  0 wallclock secs ( 0.01 usr  0.00 sys +  0.15 cusr  0.02 csys =  0.18 CPU)
Result: PASS