Debian Codegraph @

upstream/debian-unstable/man3/Test2::Manual::Tooling::Testing.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Manual::Tooling::Testing 3perl"
58 .TH Test2::Manual::Tooling::Testing 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Manual::Tooling::Testing \- Tutorial on how to test your testing tools.
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 Testing your test tools used to be a complex and difficult prospect. The old
68 tools such as Test::Tester and Test::Builder::Tester were limited, and
69 fragile. Test2 on the other hand was designed from the very start to be easily
70 tested! This tutorial shows you how.
71 .SH "THE HOLY GRAIL OF TESTING YOUR TOOLS"
72 .IX Header "THE HOLY GRAIL OF TESTING YOUR TOOLS"
73 The key to making Test2 easily testable (specially when compared to
74 Test::Builder) is the \f(CW\*(C`intercept\*(C'\fR function.
75 .PP
76 .Vb 1
77 \& use Test2::API qw/intercept/;
78 \&
79 \& my $events = intercept {
80 \& ok(1, "pass");
81 \& ok(0, "fail");
82 \&
83 \& diag("A diag");
84 \& };
85 .Ve
86 .PP
87 The intercept function lets you use any test tools you want inside a codeblock.
88 No events or contexts generated within the intercept codeblock will have any
89 effect on the outside testing state. The \f(CW\*(C`intercept\*(C'\fR function completely
90 isolates the tools called within.
91 .PP
92 \&\fBNote:\fR Plugins and things that effect global API state may not be fully
93 isolated. \f(CW\*(C`intercept\*(C'\fR is intended specifically for event isolation.
94 .PP
95 The \f(CW\*(C`intercept\*(C'\fR function will return an arrayref containing all the events
96 that were generated within the codeblock. You can now make any assertions you
97 want about the events you expected your tools to generate.
98 .PP
99 .Vb 6
100 \& [
101 \& bless({...}, \*(AqTest2::Event::Ok\*(Aq), # pass
102 \& bless({...}, \*(AqTest2::Event::Ok\*(Aq), # fail
103 \& bless({...}, \*(AqTest2::Event::Diag\*(Aq), # Failure diagnostics (not always a second event)
104 \& bless({...}, \*(AqTest2::Event::Diag\*(Aq), # custom \*(AqA diag\*(Aq message
105 \& ]
106 .Ve
107 .PP
108 Most test tools eventually produce one or more events. To effectively verify
109 the events you get from intercept you really should read up on how events work
110 Test2::Manual::Anatomy::Event. Once you know about events you can move on to
111 the next section which points you at some helpers.
112 .SH "ADDITIONAL HELPERS"
113 .IX Header "ADDITIONAL HELPERS"
114 .SS Test2::Tools::Tester
115 .IX Subsection "Test2::Tools::Tester"
116 This is the most recent set of tools to help you test your events. To really
117 understand these you should familiarize yourself with
118 Test2::Manual::Anatomy::Event. If you are going to be writing anything more
119 than the most simple of tools you should know how events work.
120 .PP
121 The Test2::Tools::Tester documentation is a good place for further reading.
122 .SS Test2::Tools::HarnessTester
123 .IX Subsection "Test2::Tools::HarnessTester"
124 The Test2::Tools::HarnessTester can export the \f(CWsummarize_events()\fR tool.
125 This tool lets you run your event arrayref through Test2::Harness so that you
126 can get a pass/fail summary.
127 .PP
128 .Vb 1
129 \& my $summary = summarize_events($events);
130 .Ve
131 .PP
132 The summary looks like this:
133 .PP
134 .Vb 8
135 \& {
136 \& plan => $plan_facet, # the plan event facet
137 \& pass => $bool, # true if the events result in a pass
138 \& fail => $bool, # true if the events result in a fail
139 \& errors => $error_count, # Number of error facets seen
140 \& failures => $failure_count, # Number of failing assertions seen
141 \& assertions => $assertion_count, # Total number of assertions seen
142 \& }
143 .Ve
144 .SS Test2::Tools::Compare
145 .IX Subsection "Test2::Tools::Compare"
146 \&\fBDEPRECATED\fR These tools were written before the switch to faceted events.
147 These will still work, but are no longer the recommended way to test your
148 tools.
149 .PP
150 The Test2::Tools::Compare library exports a handful of extras to help test
151 events.
152 .ie n .IP "event $TYPE => ..." 4
153 .el .IP "event \f(CW$TYPE\fR => ..." 4
154 .IX Item "event $TYPE => ..."
155 Use in an array check against \f(CW$events\fR to check for a specific type of event
156 with the properties you specify.
157 .ie n .IP "fail_events $TYPE => ..." 4
158 .el .IP "fail_events \f(CW$TYPE\fR => ..." 4
159 .IX Item "fail_events $TYPE => ..."
160 Use when you expect a failing assertion of \f(CW$TYPE\fR. This will automatically check
161 that the next event following it is a diagnostics message with the default
162 failure text.
163 .Sp
164 \&\fBNote:\fR This is outdated as a single event may now possess both the failing
165 assertion AND the failing text, such events will fail this test.
166 .SH "SEE ALSO"
167 .IX Header "SEE ALSO"
168 Test2::Manual \- Primary index of the manual.
169 .SH SOURCE
170 .IX Header "SOURCE"
171 The source code repository for Test2\-Manual can be found at
172 \&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR.
173 .SH MAINTAINERS
174 .IX Header "MAINTAINERS"
175 .IP "Chad Granum <[email protected]>" 4
176 .IX Item "Chad Granum <[email protected]>"
177 .SH AUTHORS
178 .IX Header "AUTHORS"
179 .PD 0
180 .IP "Chad Granum <[email protected]>" 4
181 .IX Item "Chad Granum <[email protected]>"
182 .PD
183 .SH COPYRIGHT
184 .IX Header "COPYRIGHT"
185 Copyright 2018 Chad Granum <[email protected]>.
186 .PP
187 This program is free software; you can redistribute it and/or
188 modify it under the same terms as Perl itself.
189 .PP
190 See \fIhttp://dev.perl.org/licenses/\fR