.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::Exception 3pm" .TH Test::Exception 3pm "2016-01-07" "perl v5.22.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::Exception \- Test exception\-based code .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Test::More tests => 5; \& use Test::Exception; \& \& # or if you don\*(Aqt need Test::More \& \& use Test::Exception tests => 5; \& \& # then... \& \& # Check that the stringified exception matches given regex \& throws_ok { $foo\->method } qr/division by zero/, \*(Aqzero caught okay\*(Aq; \& \& # Check an exception of the given class (or subclass) is thrown \& throws_ok { $foo\->method } \*(AqError::Simple\*(Aq, \*(Aqsimple error thrown\*(Aq; \& \& # all Test::Exceptions subroutines are guaranteed to preserve the state \& # of $@ so you can do things like this after throws_ok and dies_ok \& like $@, \*(Aqwhat the stringified exception should look like\*(Aq; \& \& # Check that something died \- we do not care why \& dies_ok { $foo\->method } \*(Aqexpecting to die\*(Aq; \& \& # Check that something did not die \& lives_ok { $foo\->method } \*(Aqexpecting to live\*(Aq; \& \& # Check that a test runs without an exception \& lives_and { is $foo\->method, 42 } \*(Aqmethod is 42\*(Aq; \& \& # or if you don\*(Aqt like prototyped functions \& \& throws_ok( sub { $foo\->method }, qr/division by zero/, \& \*(Aqzero caught okay\*(Aq ); \& throws_ok( sub { $foo\->method }, \*(AqError::Simple\*(Aq, \& \*(Aqsimple error thrown\*(Aq ); \& dies_ok( sub { $foo\->method }, \*(Aqexpecting to die\*(Aq ); \& lives_ok( sub { $foo\->method }, \*(Aqexpecting to live\*(Aq ); \& lives_and( sub { is $foo\->method, 42 }, \*(Aqmethod is 42\*(Aq ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides a few convenience methods for testing exception based code. It is built with Test::Builder and plays happily with Test::More and friends. .PP If you are not already familiar with Test::More now would be the time to go take a look. .PP You can specify the test plan when you \f(CW\*(C`use Test::Exception\*(C'\fR in the same way as \f(CW\*(C`use Test::More\*(C'\fR. See Test::More for details. .PP \&\s-1NOTE:\s0 Test::Exception only checks for exceptions. It will ignore other methods of stopping program execution \- including \fIexit()\fR. If you have an \fIexit()\fR in evalled code Test::Exception will not catch this with any of its testing functions. .PP \&\s-1NOTE:\s0 This module uses Sub::Uplevel and relies on overriding \&\f(CW\*(C`CORE::GLOBAL::caller\*(C'\fR to hide your test blocks from the call stack. If this use of global overrides concerns you, the Test::Fatal module offers a more minimalist alternative. .IP "\fBthrows_ok\fR" 4 .IX Item "throws_ok" Tests to see that a specific exception is thrown. \fIthrows_ok()\fR has two forms: .Sp .Vb 2 \& throws_ok BLOCK REGEX, TEST_DESCRIPTION \& throws_ok BLOCK CLASS, TEST_DESCRIPTION .Ve .Sp In the first form the test passes if the stringified exception matches the give regular expression. For example: .Sp .Vb 1 \& throws_ok { read_file( \*(Aqunreadable\*(Aq ) } qr/No file/, \*(Aqno file\*(Aq; .Ve .Sp If your perl does not support \f(CW\*(C`qr//\*(C'\fR you can also pass a regex-like string, for example: .Sp .Vb 1 \& throws_ok { read_file( \*(Aqunreadable\*(Aq ) } \*(Aq/No file/\*(Aq, \*(Aqno file\*(Aq; .Ve .Sp The second form of \fIthrows_ok()\fR test passes if the exception is of the same class as the one supplied, or a subclass of that class. For example: .Sp .Vb 1 \& throws_ok { $foo\->bar } "Error::Simple", \*(Aqsimple error\*(Aq; .Ve .Sp Will only pass if the \f(CW\*(C`bar\*(C'\fR method throws an Error::Simple exception, or a subclass of an Error::Simple exception. .Sp 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: .Sp .Vb 2 \& my $SIMPLE = Error::Simple\->new; \& throws_ok { $foo\->bar } $SIMPLE, \*(Aqsimple error\*(Aq; .Ve .Sp Should a \fIthrows_ok()\fR test fail it produces appropriate diagnostic messages. For example: .Sp .Vb 4 \& not ok 3 \- simple error \& # Failed test (test.t at line 48) \& # expecting: Error::Simple exception \& # found: normal exit .Ve .Sp Like all other Test::Exception functions you can avoid prototypes by passing a subroutine explicitly: .Sp .Vb 1 \& throws_ok( sub {$foo\->bar}, "Error::Simple", \*(Aqsimple error\*(Aq ); .Ve .Sp A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). .Sp A description of the exception being checked is used if no optional test description is passed. .Sp \&\s-1NOTE:\s0 Remember when you \f(CW\*(C`die $string_without_a_trailing_newline\*(C'\fR perl will automatically add the current script line number, input line number and a newline. This will form part of the string that throws_ok regular expressions match against. .IP "\fBdies_ok\fR" 4 .IX Item "dies_ok" Checks that a piece of code dies, rather than returning normally. For example: .Sp .Vb 4 \& sub div { \& my ( $a, $b ) = @_; \& return $a / $b; \& }; \& \& dies_ok { div( 1, 0 ) } \*(Aqdivide by zero detected\*(Aq; \& \& # or if you don\*(Aqt like prototypes \& dies_ok( sub { div( 1, 0 ) }, \*(Aqdivide by zero detected\*(Aq ); .Ve .Sp A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). .Sp Remember: This test will pass if the code dies for any reason. If you care about the reason it might be more sensible to write a more specific test using \fIthrows_ok()\fR. .Sp The test description is optional, but recommended. .IP "\fBlives_ok\fR" 4 .IX Item "lives_ok" Checks that a piece of code doesn't die. This allows your test script to continue, rather than aborting if you get an unexpected exception. For example: .Sp .Vb 7 \& sub read_file { \& my $file = shift; \& local $/; \& open my $fh, \*(Aq<\*(Aq, $file or die "open failed ($!)\en"; \& $file = ; \& return $file; \& }; \& \& my $file; \& lives_ok { $file = read_file(\*(Aqtest.txt\*(Aq) } \*(Aqfile read\*(Aq; \& \& # or if you don\*(Aqt like prototypes \& lives_ok( sub { $file = read_file(\*(Aqtest.txt\*(Aq) }, \*(Aqfile read\*(Aq ); .Ve .Sp Should a \fIlives_ok()\fR test fail it produces appropriate diagnostic messages. For example: .Sp .Vb 3 \& not ok 1 \- file read \& # Failed test (test.t at line 15) \& # died: open failed (No such file or directory) .Ve .Sp A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). .Sp The test description is optional, but recommended. .IP "\fBlives_and\fR" 4 .IX Item "lives_and" Run a test that may throw an exception. For example, instead of doing: .Sp .Vb 3 \& my $file; \& lives_ok { $file = read_file(\*(Aqanswer.txt\*(Aq) } \*(Aqread_file worked\*(Aq; \& is $file, "42", \*(Aqanswer was 42\*(Aq; .Ve .Sp You can use \fIlives_and()\fR like this: .Sp .Vb 3 \& lives_and { is read_file(\*(Aqanswer.txt\*(Aq), "42" } \*(Aqanswer is 42\*(Aq; \& # or if you don\*(Aqt like prototypes \& lives_and(sub {is read_file(\*(Aqanswer.txt\*(Aq), "42"}, \*(Aqanswer is 42\*(Aq); .Ve .Sp Which is the same as doing .Sp .Vb 1 \& is read_file(\*(Aqanswer.txt\*(Aq), "42\en", \*(Aqanswer is 42\*(Aq; .Ve .Sp unless \f(CW\*(C`read_file(\*(Aqanswer.txt\*(Aq)\*(C'\fR dies, in which case you get the same kind of error as \fIlives_ok()\fR .Sp .Vb 3 \& not ok 1 \- answer is 42 \& # Failed test (test.t at line 15) \& # died: open failed (No such file or directory) .Ve .Sp A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). .Sp The test description is optional, but recommended. .SH "SKIPPING TEST::EXCEPTION TESTS" .IX Header "SKIPPING TEST::EXCEPTION TESTS" Sometimes we want to use Test::Exception tests in a test suite, but don't want to force the user to have Test::Exception installed. One way to do this is to skip the tests if Test::Exception is absent. You can do this with code something like this: .PP .Vb 3 \& use strict; \& use warnings; \& use Test::More; \& \& BEGIN { \& eval "use Test::Exception"; \& plan skip_all => "Test::Exception needed" if $@; \& } \& \& plan tests => 2; \& # ... tests that need Test::Exception ... .Ve .PP Note that we load Test::Exception in a \f(CW\*(C`BEGIN\*(C'\fR block ensuring that the subroutine prototypes are in place before the rest of the test script is compiled. .SH "BUGS" .IX Header "BUGS" There are some edge cases in Perl's exception handling where Test::Exception will miss exceptions thrown in \s-1DESTROY\s0 blocks. See the \s-1RT\s0 bug for details, along with the t/edge\-cases.t in the distribution test suite. These will be addressed in a future Test::Exception release. .PP If you find any more bugs please let me know by e\-mail, or report the problem with . .SH "COMMUNITY" .IX Header "COMMUNITY" .IP "perl-qa" 4 .IX Item "perl-qa" If you are interested in testing using Perl I recommend you visit and join the excellent perl-qa mailing list. See for details on how to subscribe. .IP "perlmonks" 4 .IX Item "perlmonks" You can find users of Test::Exception, including the module author, on . Feel free to ask questions on Test::Exception there. .IP "CPAN::Forum" 4 .IX Item "CPAN::Forum" The \s-1CPAN\s0 Forum is a web forum for discussing Perl's \s-1CPAN\s0 modules. The Test::Exception forum can be found at . .IP "AnnoCPAN" 4 .IX Item "AnnoCPAN" AnnoCPAN is a web site that allows community annotations of Perl module documentation. The Test::Exception annotations can be found at . .SH "TO DO" .IX Header "TO DO" If you think this module should do something that it doesn't (or does something that it shouldn't) please let me know. .PP You can see my current to do list at , with an \s-1RSS\s0 feed of changes at . .SH "ACKNOWLEDGMENTS" .IX Header "ACKNOWLEDGMENTS" Thanks to chromatic and Michael G Schwern for the excellent Test::Builder, without which this module wouldn't be possible. .PP Thanks to Adam Kennedy, Andy Lester, Aristotle Pagaltzis, Ben Prew, Cees Hek, Chris Dolan, chromatic, Curt Sampson, David Cantrell, David Golden, David Tulloh, David Wheeler, J. K. O'Brien, Janek Schleicher, Jim Keenan, Jos I. Boumans, Joshua ben Jore, Jost Krieger, Mark Fowler, Michael G Schwern, Nadim Khemir, Paul McCann, Perrin Harkins, Peter Rabbitson, Peter Scott, Ricardo Signes, Rob Muhlestein, Scott R. Godin, Steve Purkis, Steve, Tim Bunce, and various anonymous folk for comments, suggestions, bug reports and patches. .SH "AUTHOR" .IX Header "AUTHOR" Adrian Howard .PP If you can spare the time, please drop me a line if you find this module useful. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "" 4 .IX Item "" Delicious links on Test::Exception. .IP "Test::Fatal" 4 .IX Item "Test::Fatal" A slightly different interface to testing exceptions, without overriding \f(CW\*(C`CORE::caller\*(C'\fR. .IP "Test::Warnings & Test::Warn & Test::NoWarnings" 4 .IX Item "Test::Warnings & Test::Warn & Test::NoWarnings" Modules to help test warnings. .IP "Test::Builder" 4 .IX Item "Test::Builder" Support module for building test libraries. .IP "Test::Simple & Test::More" 4 .IX Item "Test::Simple & Test::More" Basic utilities for writing tests. .IP "" 4 .IX Item "" Overview of some of the many testing modules available on \s-1CPAN.\s0 .IP "" 4 .IX Item "" Delicious links on perl testing. .SH "LICENCE" .IX Header "LICENCE" Copyright 2002\-2007 Adrian Howard, All Rights Reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.