Test::Exception - Test exception based code
use Test::More tests => 5; use Test::Exception;
# or if you don't need Test::More
use Test::Exception tests => 5;
# then...
# Check that something died dies_ok {$foo->method1} 'expecting to die';
# Check that something did not die lives_ok {$foo->method2} 'expecting to live';
# Check that the stringified exception matches given regex throws_ok {$foo->method3} qr/division by zero/, 'zero caught okay';
# Check an exception of the given class (or subclass) is thrown throws_ok {$foo->method4} 'Error::Simple', 'simple error thrown';
# Check that a test runs without an exception lives_and {is $foo->method, 42} 'method is 42';
# or if you don't like prototyped functions
dies_ok( sub { $foo->method1 }, 'expecting to die' ); lives_ok( sub {$foo->method2}, 'expecting to live' ); throws_ok( sub {$foo->method3}, qr/division by zero/, 'zero caught okay' ); throws_ok( sub {$foo->method4}, 'Error::Simple', 'simple error thrown' ); lives_and( sub {is $foo->method, 42}, 'method is 42' );
If you are not already familiar with Test::More now would be the time to go take a look.
You can specify the test plan when you "use Test::Exception" in the same way as "use Test::More". See Test::More for details.
sub div { my ($a, $b) = @_; return( $a / $b ); };
dies_ok { div(1, 0) } 'divide by zero detected'; # or if you don't like prototypes dies_ok( sub { div(1, 0) }, 'divide by zero detected' );
A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
The test description is optional, but recommended.
sub read_file { my $file = shift; local $/ = undef; open(FILE, $file) or die "open failed ($!)\n"; $file = <FILE>; close(FILE); return($file); };
my $file; lives_ok { $file = read_file('test.txt') } 'file read'; # or if you don't like prototypes lives_ok( sub { $file = read_file('test.txt') }, 'file read' );
Should a lives_ok() test fail it produces appropriate diagnostic messages. For example:
not ok 1 - file read # Failed test (test.t at line 15) # died: open failed (No such file or directory)
A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
The test description is optional, but recommended.
throws_ok BLOCK REGEX, TEST_DESCRIPTION throws_ok BLOCK CLASS, TEST_DESCRIPTION
In the first form the test passes if the stringified exception matches the give regular expression. For example:
throws_ok { read_file('unreadable') } qr/No such file/, 'no file';
If your perl does not support "qr//" you can also pass a regex-like string, for example:
throws_ok { read_file('unreadable') } '/Permission denied/', 'no permissions';
The second form of throws_ok() test passes if the exception is of the same class as the one supplied, or a subclass of that class. For example:
throws_ok {$foo->bar} "Error::Simple", 'simple error';
Will only pass if the "bar" method throws an Error::Simple exception, or a subclass of an Error::Simple exception.
You can get the same effect by passing an instance of the exception you want to look for. The following is equivalent to the previous example:
my $SIMPLE = Error::Simple->new(); throws_ok {$foo->bar} $SIMPLE, 'simple error';
Should a throws_ok() test fail it produces appropriate diagnostic messages. For example:
not ok 3 - simple error # Failed test (test.t at line 48) # expecting: Error::Simple exception # found: normal exit
Like all other Test::Exception functions you can avoid protypes by passing a subroutine explicitly:
throws_ok( sub {$foo->bar}, "Error::Simple", 'simple error' );
A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
A description of the exception being checked is used if no optional test description is passed.
my $file; lives_ok { $file = read_file('answer.txt') } 'read_file worked'; is $file, "42", 'answer was 42';
You can use lives_and() like this:
lives_and { is read_file('answer.txt'), "42" } 'answer is 42'; # or if you don't like prototypes lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42');
Which is the same as doing
is read_file('answer.txt'), "42\n", 'answer is 42';
unless "read_file('answer.txt')" dies, in which case you get the same kind of error as lives_ok()
not ok 1 - answer is 42 # Failed test (test.t at line 15) # died: open failed (No such file or directory)
A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any).
The test description is optional, but recommended.
If you find any please let me know by e-mail, or report the problem with <http://rt.cpan.org/>.
You can see my current to do list at <http://adrianh.tadalist.com/lists/public/15421>, with an RSS feed of changes at <http://adrianh.tadalist.com/lists/feed_public/15421>.
Thanks to Michael G Schwern, Mark Fowler, Janek Schleicher, chromatic, Peter Scott, Aristotle, Andy Lester, David Wheeler, Jos I. Boumans, Jim Keenan & Perrin for comments, suggestions, bug reports and patches.
If you can spare the time, please drop me a line if you find this module useful.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |