1Test2::Tools::ExceptionU(s3e)r Contributed Perl DocumentaTteisotn2::Tools::Exception(3)
2
3
4
6 Test2::Tools::Exception - Test2 based tools for checking exceptions
7
9 This is the Test2 implementation of code used to test exceptions. This
10 is similar to Test::Fatal, but it intentionally does much less.
11
13 use Test2::Tools::Exception qw/dies lives/;
14
15 like(
16 dies { die 'xxx' },
17 qr/xxx/,
18 "Got exception"
19 );
20
21 ok(lives { ... }, "did not die") or note($@);
22
24 All subs are exported by default.
25
26 $e = dies { ... }
27 This will trap any exception the codeblock throws. If no exception
28 is thrown the sub will return undef. If an exception is thrown it
29 will be returned. This function preserves $@, it will not be
30 altered from its value before the sub is called.
31
32 $bool = lives { ... }
33 This will trap any exception thrown in the codeblock. It will
34 return true when there is no exception, and false when there is. $@
35 is preserved from before the sub is called when there is no
36 exception. When an exception is trapped $@ will have the exception
37 so that you can look at it.
38
39 $bool = try_ok { ... }
40 $bool = try_ok { ... } "Test Description"
41 This will run the code block trapping any exception. If there is no
42 exception a passing event will be issued. If the test fails a
43 failing event will be issued, and the exception will be reported as
44 diagnostics.
45
46 Note: This function does not preserve $@ on failure, it will be set
47 to the exception the codeblock throws, this is by design so that
48 you can obtain the exception if desired.
49
51 Test::Fatal sets $Test::Builder::Level such that failing tests inside
52 the exception block will report to the line where "exception()" is
53 called. I disagree with this, and think the actual line of the failing
54 test is more important. Ultimately, though Test::Fatal cannot be
55 changed, people probably already depend on that behavior.
56
58 The source code repository for Test2-Suite can be found at
59 https://github.com/Test-More/Test2-Suite/.
60
62 Chad Granum <exodist@cpan.org>
63
65 Chad Granum <exodist@cpan.org>
66
68 Copyright 2018 Chad Granum <exodist@cpan.org>.
69
70 This program is free software; you can redistribute it and/or modify it
71 under the same terms as Perl itself.
72
73 See http://dev.perl.org/licenses/
74
75
76
77perl v5.28.2 2019-05-18 Test2::Tools::Exception(3)