jeudi 5 février 2015

how to test a subroutine which has 'exit' in it (perl)

For example:


dummy_mod.pl



package Dummy::Mod;

use warnings;
use strict;

sub print_and_exit {
print "Hello World\n";
exit;
}
1;


dummy_mod.t



use warnings;
use strict;

use Test::More;
use Test::Output;

plan tests => 2;
ok(require('dummy_mod.pl'), 'load test') or exit;
stdout_is(sub { Dummy::Mod->print_and_exit() }, "Hello World\n", "stdout test");


Then I got:



$ perl dummy_mod.t
1..2
ok 1 - load test
# Looks like you planned 2 tests but ran 1.


So seems the test script exit before test #2 finish. If I remove 'exit' from print_and_exit subroutine, it can work, but I don't want to do that in my real case. I've tried with:



stdout_is(sub { local $SIG{INT} = sub {}; Dummy::Mod->print_and_exit() }, "Hello World\n", "stdout test");


it doesn't work either.


Aucun commentaire:

Enregistrer un commentaire