Is it possible to test the expected errors when the test goes out with an error using TAP in Perl?

Suppose you are doing some unit tests and you want to check if the method (or script or function or something else) that you are testing is working. How do you set up such a test? I hope for something like this:

ok( $obj->method($my, $bad, $params) == DEATH, 'method dies as expected');

although I don’t see how it will work with method diepassing bad parameters, and the test script stops.

Is there another way?

+4
source share
3 answers

Have you tried Test :: Exception ? dies_okshould do what you want. For instance:

# Check that something died - we do not care why
dies_ok { $foo->method } 'expecting to die';
+5
source

Test:: Fatal Test:: Exception.

Test:: Exception , , Test:: Fatal . Test:: Fatal 1 : exception. , , undef, . Test:: More , is, isnt, like isa_ok.

Test:: Exception , , throws_ok dies_ok, , .

, :

use Test::More;
use Test::Fatal;

my $obj = ...;

isnt(exception { $obj->method($my, $bad, $params) },
     undef, 'method dies as expected');

like isa_ok, , .

Test:: Fatal , Test:: Exception.

+5

. , eval, :

ok !eval {$obj->method($my, $bad, $params); 1}, 'method dies as expected';

eval, , 1. , eval undef. , ok, ! .

, :

like $@, qr/invalid argument/, 'method died with invalid argument';
+1

All Articles