开发者

Is it possbile to test for expected errors when the testee exits with failure using TAP within Perl?

开发者 https://www.devze.com 2023-01-31 19:59 出处:网络
Suppose you\'re running some unit tests and you want to see if the method ( or script or function or whatever) you\'re testing fails as it should. How do you setup such a test?I\'m hoping for somethin

Suppose you're running some unit tests and you want to see if the method ( or script or function or whatever) you're testing fails as it should. How do you setup such a test? I'm hoping for somethin开发者_开发百科g like this:

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

although I don't see how it would work since method dies when passed bad parameters and the test script stops.

Is there another way?


Have you tried Test::Exception? dies_ok should do what you want. Eg:

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


I recommend Test::Fatal rather than Test::Exception.

Test::Exception has been around for a long time, so a lot of existing test suites use it, but Test::Fatal is easier to learn. Test::Fatal exports only 1 function: exception. This runs the associated code and returns the exception that it threw, or undef if it ran without error. You then test that return value using any of the normal Test::More functions like is, isnt, like, or isa_ok.

Test::Exception requires you to learn its own testing functions like throws_ok and dies_ok, and remember that you don't put a comma between the code and the test name.

So, your example would be:

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

my $obj = ...;

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

Or you could use like to match the expected error message, or isa_ok to test if it threw the correct class of exception object.

Test::Fatal just gives you more flexibility with less learning curve than Test::Exception.


There's really no need to use a module to do this. You can just wrap the call that you expect to fail in an eval block like so:

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

If all goes well in the eval, it will return the last statement executed, which is 1 in this case. if it fails, eval will return undef. This is the opposite of what ok wants, so ! to flip the values.

You can then follow that line with a check of the actual exception message if you want:

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

精彩评论

暂无评论...
验证码 换一张
取 消