This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
cpan/Test2-Suite - Update to version 0.000159
[perl5.git] / cpan / Test2-Suite / lib / Test2 / Manual / Tooling / Testing.pm
CommitLineData
6281b77d
PE
1package Test2::Manual::Tooling::Testing;
2use strict;
3use warnings;
4
dde38cd6 5our $VERSION = '0.000159';
6281b77d
PE
6
71;
8
9__END__
10
11=head1 NAME
12
13Test2::Manual::Tooling::Testing - Tutorial on how to test your testing tools.
14
15=head1 DESCRIPTION
16
17Testing your test tools used to be a complex and difficult prospect. The old
18tools such as L<Test::Tester> and L<Test::Builder::Tester> were limited, and
19fragile. Test2 on the other hand was designed from the very start to be easily
20tested! This tutorial shows you how.
21
22=head1 THE HOLY GRAIL OF TESTING YOUR TOOLS
23
24The key to making Test2 easily testable (specially when compared to
25Test::Builder) is the C<intercept> function.
26
27 use Test2::API qw/intercept/;
28
29 my $events = intercept {
30 ok(1, "pass");
31 ok(0, "fail");
32
33 diag("A diag");
34 };
35
36The intercept function lets you use any test tools you want inside a codeblock.
37No events or contexts generated within the intercept codeblock will have any
38effect on the outside testing state. The C<intercept> function completely
39isolates the tools called within.
40
41B<Note:> Plugins and things that effect global API state may not be fully
42isolated. C<intercept> is intended specifically for event isolation.
43
44The C<intercept> function will return an arrayref containing all the events
45that were generated within the codeblock. You can now make any assertions you
46want about the events you expected your tools to generate.
47
48 [
49 bless({...}, 'Test2::Event::Ok'), # pass
50 bless({...}, 'Test2::Event::Ok'), # fail
51 bless({...}, 'Test2::Event::Diag'), # Failure diagnostics (not always a second event)
52 bless({...}, 'Test2::Event::Diag'), # custom 'A diag' message
53 ]
54
55Most test tools eventually produce one or more events. To effectively verify
56the events you get from intercept you really should read up on how events work
57L<Test2::Manual::Anatomy::Event>. Once you know about events you can move on to
58the next section which points you at some helpers.
59
60=head1 ADDITIONAL HELPERS
61
62=head2 Test2::Tools::Tester
63
64This is the most recent set of tools to help you test your events. To really
65understand these you should familiarize yourself with
66L<Test2::Manual::Anatomy::Event>. If you are going to be writing anything more
67than the most simple of tools you should know how events work.
68
69The L<Test2::Tools::Tester> documentation is a good place for further reading.
70
71=head2 Test2::Tools::HarnessTester
72
73The L<Test2::Tools::HarnessTester> can export the C<summarize_events()> tool.
74This tool lets you run your event arrayref through L<Test2::Harness> so that you
75can get a pass/fail summary.
76
77 my $summary = summarize_events($events);
78
79The summary looks like this:
80
81 {
82 plan => $plan_facet, # the plan event facet
83 pass => $bool, # true if the events result in a pass
84 fail => $bool, # true if the events result in a fail
85 errors => $error_count, # Number of error facets seen
86 failures => $failure_count, # Number of failing assertions seen
87 assertions => $assertion_count, # Total number of assertions seen
88 }
89
90=head2 Test2::Tools::Compare
91
92B<DEPRECATED> These tools were written before the switch to faceted events.
93These will still work, but are no longer the recommended way to test your
94tools.
95
96The L<Test2::Tools::Compare> library exports a handful of extras to help test
97events.
98
99=over 4
100
101=item event $TYPE => ...
102
103Use in an array check against $events to check for a specific type of event
104with the properties you specify.
105
106=item fail_events $TYPE => ...
107
108Use when you expect a failing assertion of $TYPE. This will automatically check
109that the next event following it is a diagnostics message with the default
110failure text.
111
112B<Note:> This is outdated as a single event may now possess both the failing
113assertion AND the failing text, such events will fail this test.
114
115=back
116
117=head1 SEE ALSO
118
119L<Test2::Manual> - Primary index of the manual.
120
121=head1 SOURCE
122
123The source code repository for Test2-Manual can be found at
124F<https://github.com/Test-More/Test2-Suite/>.
125
126=head1 MAINTAINERS
127
128=over 4
129
130=item Chad Granum E<lt>exodist@cpan.orgE<gt>
131
132=back
133
134=head1 AUTHORS
135
136=over 4
137
138=item Chad Granum E<lt>exodist@cpan.orgE<gt>
139
140=back
141
142=head1 COPYRIGHT
143
144Copyright 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
145
146This program is free software; you can redistribute it and/or
147modify it under the same terms as Perl itself.
148
149See F<http://dev.perl.org/licenses/>
150
151=cut