Debian Codegraph @

upstream/mageia-cauldron/man3pm/Test2::Tools::Spec.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (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 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Test2::Tools::Spec 3pm"
61 .TH Test2::Tools::Spec 3pm 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Test2::Tools::Spec \- RSPEC implementation on top of Test2::Workflow
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 This uses Test2::Workflow to implement an RSPEC variant. This variant
71 supports isolation and/or concurrency via forking or threads.
72 .SH SYNOPSIS
73 .IX Header "SYNOPSIS"
74 .Vb 2
75 \& use Test2::Bundle::Extended;
76 \& use Test2::Tools::Spec;
77 \&
78 \& describe foo => sub {
79 \& before_all once => sub { ... };
80 \& before_each many => sub { ... };
81 \&
82 \& after_all once => sub { ... };
83 \& after_each many => sub { ... };
84 \&
85 \& case condition_a => sub { ... };
86 \& case condition_b => sub { ... };
87 \&
88 \& tests foo => sub { ... };
89 \& tests bar => sub { ... };
90 \& };
91 \&
92 \& done_testing;
93 .Ve
94 .SH EXPORTS
95 .IX Header "EXPORTS"
96 All of these use the same argument pattern. The first argument must always be a
97 name for the block. The last argument must always be a code reference.
98 Optionally a configuration hash can be inserted between the name and the code
99 reference.
100 .PP
101 .Vb 1
102 \& FUNCTION "name" => sub { ... };
103 \&
104 \& FUNCTION "name" => {...}, sub { ... };
105 .Ve
106 .IP NAME 4
107 .IX Item "NAME"
108 The first argument to a Test2::Tools::Spec function MUST be a name. The name
109 does not need to be unique.
110 .IP PARAMS 4
111 .IX Item "PARAMS"
112 This argument is optional. If present this should be a hashref.
113 .Sp
114 Here are the valid keys for the hashref:
115 .RS 4
116 .ie n .IP "flat => $bool" 8
117 .el .IP "flat => \f(CW$bool\fR" 8
118 .IX Item "flat => $bool"
119 If this is set to true then the block will not render as a subtest, instead the
120 events will be inline with the parent subtest (or main test).
121 .ie n .IP "async => $bool" 8
122 .el .IP "async => \f(CW$bool\fR" 8
123 .IX Item "async => $bool"
124 Set this to true to mark a block as being capable of running concurrently with
125 other test blocks. This does not mean the block WILL be run concurrently, just
126 that it can be.
127 .ie n .IP "iso => $bool" 8
128 .el .IP "iso => \f(CW$bool\fR" 8
129 .IX Item "iso => $bool"
130 Set this to true if the block MUST be run in isolation. If this is true then
131 the block will run in its own forked process.
132 .Sp
133 These tests will be skipped on any platform that does not have true forking, or
134 working/enabled threads.
135 .Sp
136 Threads will ONLY be used if the T2_WORKFLOW_USE_THREADS env var is set. Thread
137 tests are only run if the T2_DO_THREAD_TESTS env var is set.
138 .ie n .IP "todo => $reason" 8
139 .el .IP "todo => \f(CW$reason\fR" 8
140 .IX Item "todo => $reason"
141 Use this to mark an entire block as TODO.
142 .ie n .IP "skip => $reason" 8
143 .el .IP "skip => \f(CW$reason\fR" 8
144 .IX Item "skip => $reason"
145 Use this to prevent a block from running at all.
146 .RE
147 .RS 4
148 .RE
149 .IP CODEREF 4
150 .IX Item "CODEREF"
151 This argument is required. This should be a code reference that will run some
152 assertions.
153 .SS ESSENTIALS
154 .IX Subsection "ESSENTIALS"
155 .IP "tests NAME => sub { ... }" 4
156 .IX Item "tests NAME => sub { ... }"
157 .PD 0
158 .IP "tests NAME => \e%params, sub { ... }" 4
159 .IX Item "tests NAME => %params, sub { ... }"
160 .IP "tests($NAME, \e%PARAMS, \e&CODE)" 4
161 .IX Item "tests($NAME, %PARAMS, &CODE)"
162 .IP "it NAME => sub { ... }" 4
163 .IX Item "it NAME => sub { ... }"
164 .IP "it NAME => \e%params, sub { ... }" 4
165 .IX Item "it NAME => %params, sub { ... }"
166 .IP "it($NAME, \e%PARAMS, \e&CODE)" 4
167 .IX Item "it($NAME, %PARAMS, &CODE)"
168 .PD
169 This defines a test block. Test blocks are essentially subtests. All test
170 blocks will be run, and are expected to produce events. Test blocks can run
171 multiple times if the \f(CWcase()\fR function is also used.
172 .Sp
173 \&\f(CWit()\fR is an alias to \f(CWtests()\fR.
174 .Sp
175 These ARE NOT inherited by nested describe blocks.
176 .IP "case NAME => sub { ... }" 4
177 .IX Item "case NAME => sub { ... }"
178 .PD 0
179 .IP "case NAME => \e%params, sub { ... }" 4
180 .IX Item "case NAME => %params, sub { ... }"
181 .IP "case($NAME, \e%PARAMS, \e&CODE)" 4
182 .IX Item "case($NAME, %PARAMS, &CODE)"
183 .PD
184 This lets you specify multiple conditions in which the test blocks should be
185 run. Every test block within the same group (\f(CW\*(C`describe\*(C'\fR) will be run once per
186 case.
187 .Sp
188 These ARE NOT inherited by nested describe blocks, but nested describe blocks
189 will be executed once per case.
190 .IP "before_each NAME => sub { ... }" 4
191 .IX Item "before_each NAME => sub { ... }"
192 .PD 0
193 .IP "before_each NAME => \e%params, sub { ... }" 4
194 .IX Item "before_each NAME => %params, sub { ... }"
195 .IP "before_each($NAME, \e%PARAMS, \e&CODE)" 4
196 .IX Item "before_each($NAME, %PARAMS, &CODE)"
197 .PD
198 Specify a codeblock that should be run multiple times, once before each
199 \&\f(CWtests()\fR block is run. These will run AFTER \f(CWcase()\fR blocks but before
200 \&\f(CWtests()\fR blocks.
201 .Sp
202 These ARE inherited by nested describe blocks.
203 .IP "before_case NAME => sub { ... }" 4
204 .IX Item "before_case NAME => sub { ... }"
205 .PD 0
206 .IP "before_case NAME => \e%params, sub { ... }" 4
207 .IX Item "before_case NAME => %params, sub { ... }"
208 .IP "before_case($NAME, \e%PARAMS, \e&CODE)" 4
209 .IX Item "before_case($NAME, %PARAMS, &CODE)"
210 .PD
211 Same as \f(CWbefore_each()\fR, except these blocks run BEFORE \f(CWcase()\fR blocks.
212 .Sp
213 These ARE NOT inherited by nested describe blocks.
214 .IP "before_all NAME => sub { ... }" 4
215 .IX Item "before_all NAME => sub { ... }"
216 .PD 0
217 .IP "before_all NAME => \e%params, sub { ... }" 4
218 .IX Item "before_all NAME => %params, sub { ... }"
219 .IP "before_all($NAME, \e%PARAMS, \e&CODE)" 4
220 .IX Item "before_all($NAME, %PARAMS, &CODE)"
221 .PD
222 Specify a codeblock that should be run once, before all the test blocks run.
223 .Sp
224 These ARE NOT inherited by nested describe blocks.
225 .IP "around_each NAME => sub { ... }" 4
226 .IX Item "around_each NAME => sub { ... }"
227 .PD 0
228 .IP "around_each NAME => \e%params, sub { ... }" 4
229 .IX Item "around_each NAME => %params, sub { ... }"
230 .IP "around_each($NAME, \e%PARAMS, \e&CODE)" 4
231 .IX Item "around_each($NAME, %PARAMS, &CODE)"
232 .PD
233 Specify a codeblock that should wrap around each test block. These blocks are
234 run AFTER case blocks, but before test blocks.
235 .Sp
236 .Vb 2
237 \& around_each wrapit => sub {
238 \& my $cont = shift;
239 \&
240 \& local %ENV = ( ... );
241 \&
242 \& $cont\->();
243 \&
244 \& ...
245 \& };
246 .Ve
247 .Sp
248 The first argument to the codeblock will be a callback that MUST be called
249 somewhere inside the sub in order for nested items to run.
250 .Sp
251 These ARE inherited by nested describe blocks.
252 .IP "around_case NAME => sub { ... }" 4
253 .IX Item "around_case NAME => sub { ... }"
254 .PD 0
255 .IP "around_case NAME => \e%params, sub { ... }" 4
256 .IX Item "around_case NAME => %params, sub { ... }"
257 .IP "around_case($NAME, \e%PARAMS, \e&CODE)" 4
258 .IX Item "around_case($NAME, %PARAMS, &CODE)"
259 .PD
260 Same as \f(CW\*(C`around_each\*(C'\fR except these run BEFORE case blocks.
261 .Sp
262 These ARE NOT inherited by nested describe blocks.
263 .IP "around_all NAME => sub { ... }" 4
264 .IX Item "around_all NAME => sub { ... }"
265 .PD 0
266 .IP "around_all NAME => \e%params, sub { ... }" 4
267 .IX Item "around_all NAME => %params, sub { ... }"
268 .IP "around_all($NAME, \e%PARAMS, \e&CODE)" 4
269 .IX Item "around_all($NAME, %PARAMS, &CODE)"
270 .PD
271 Same as \f(CW\*(C`around_each\*(C'\fR except that it only runs once to wrap ALL test blocks.
272 .Sp
273 These ARE NOT inherited by nested describe blocks.
274 .IP "after_each NAME => sub { ... }" 4
275 .IX Item "after_each NAME => sub { ... }"
276 .PD 0
277 .IP "after_each NAME => \e%params, sub { ... }" 4
278 .IX Item "after_each NAME => %params, sub { ... }"
279 .IP "after_each($NAME, \e%PARAMS, \e&CODE)" 4
280 .IX Item "after_each($NAME, %PARAMS, &CODE)"
281 .PD
282 Same as \f(CW\*(C`before_each\*(C'\fR except it runs right after each test block.
283 .Sp
284 These ARE inherited by nested describe blocks.
285 .IP "after_case NAME => sub { ... }" 4
286 .IX Item "after_case NAME => sub { ... }"
287 .PD 0
288 .IP "after_case NAME => \e%params, sub { ... }" 4
289 .IX Item "after_case NAME => %params, sub { ... }"
290 .IP "after_case($NAME, \e%PARAMS, \e&CODE)" 4
291 .IX Item "after_case($NAME, %PARAMS, &CODE)"
292 .PD
293 Same as \f(CW\*(C`after_each\*(C'\fR except it runs right after the case block, and before the
294 test block.
295 .Sp
296 These ARE NOT inherited by nested describe blocks.
297 .IP "after_all NAME => sub { ... }" 4
298 .IX Item "after_all NAME => sub { ... }"
299 .PD 0
300 .IP "after_all NAME => \e%params, sub { ... }" 4
301 .IX Item "after_all NAME => %params, sub { ... }"
302 .IP "after_all($NAME, \e%PARAMS, \e&CODE)" 4
303 .IX Item "after_all($NAME, %PARAMS, &CODE)"
304 .PD
305 Same as \f(CW\*(C`before_all\*(C'\fR except it runs after all test blocks have been run.
306 .Sp
307 These ARE NOT inherited by nested describe blocks.
308 .SS SHORTCUTS
309 .IX Subsection "SHORTCUTS"
310 These are shortcuts. Each of these is the same as \f(CWtests()\fR except some
311 parameters are added for you.
312 .PP
313 These are NOT exported by default/.
314 .IP "mini NAME => sub { ... }" 4
315 .IX Item "mini NAME => sub { ... }"
316 Same as:
317 .Sp
318 .Vb 1
319 \& tests NAME => { flat => 1 }, sub { ... }
320 .Ve
321 .IP "iso NAME => sub { ... }" 4
322 .IX Item "iso NAME => sub { ... }"
323 Same as:
324 .Sp
325 .Vb 1
326 \& tests NAME => { iso => 1 }, sub { ... }
327 .Ve
328 .IP "miso NAME => sub { ... }" 4
329 .IX Item "miso NAME => sub { ... }"
330 Same as:
331 .Sp
332 .Vb 1
333 \& tests NAME => { mini => 1, iso => 1 }, sub { ... }
334 .Ve
335 .IP "async NAME => sub { ... }" 4
336 .IX Item "async NAME => sub { ... }"
337 Same as:
338 .Sp
339 .Vb 1
340 \& tests NAME => { async => 1 }, sub { ... }
341 .Ve
342 .Sp
343 \&\fBNote:\fR This conflicts with the \f(CWasync()\fR exported from threads. Don\*(Aqt
344 import both.
345 .IP "masync NAME => sub { ... }" 4
346 .IX Item "masync NAME => sub { ... }"
347 Same as:
348 .Sp
349 .Vb 1
350 \& tests NAME => { minit => 1, async => 1 }, sub { ... }
351 .Ve
352 .SS "CUSTOM ATTRIBUTE DEFAULTS"
353 .IX Subsection "CUSTOM ATTRIBUTE DEFAULTS"
354 Sometimes you want to apply default attributes to all \f(CWtests()\fR or \f(CWcase()\fR
355 blocks. This can be done, and is lexical to your describe or package root!
356 .PP
357 .Vb 2
358 \& use Test2::Bundle::Extended;
359 \& use Test2::Tools::Spec \*(Aq:ALL\*(Aq;
360 \&
361 \& # All \*(Aqtests\*(Aq blocks after this declaration will have C<<iso => 1>> by default
362 \& spec_defaults tests => (iso => 1);
363 \&
364 \& tests foo => sub { ... }; # isolated
365 \&
366 \& tests foo, {iso => 0}, sub { ... }; # Not isolated
367 \&
368 \& spec_defaults tests => (iso => 0); # Turn it off again
369 .Ve
370 .PP
371 Defaults are inherited by nested describe blocks. You can also override the
372 defaults for the scope of the describe:
373 .PP
374 .Vb 1
375 \& spec_defaults tests => (iso => 1);
376 \&
377 \& describe foo => sub {
378 \& spec_defaults tests => (async => 1); # Scoped to this describe and any child describes
379 \&
380 \& tests bar => sub { ... }; # both iso and async
381 \& };
382 \&
383 \& tests baz => sub { ... }; # Just iso, no async.
384 .Ve
385 .PP
386 You can apply defaults to any type of blocks:
387 .PP
388 .Vb 1
389 \& spec_defaults case => (iso => 1); # All cases are \*(Aqiso\*(Aq;
390 .Ve
391 .PP
392 Defaults are not inherited when a builder\*(Aqs return is captured.
393 .PP
394 .Vb 1
395 \& spec_defaults tests => (iso => 1);
396 \&
397 \& # Note we are not calling this in void context, that is the key here.
398 \& my $d = describe foo => {
399 \& tests bar => sub { ... }; # Not iso
400 \& };
401 .Ve
402 .SH "EXECUTION ORDER"
403 .IX Header "EXECUTION ORDER"
404 As each function is encountered it executes, just like any other function. The
405 \&\f(CWdescribe()\fR function will immediately execute the codeblock it is given. All
406 other functions will stash their codeblocks to be run later. When
407 \&\f(CWdone_testing()\fR is run the workflow will be compiled, at which point all
408 other blocks will run.
409 .PP
410 Here is an overview of the order in which blocks get called once compiled (at
411 \&\f(CWdone_testing()\fR).
412 .PP
413 .Vb 5
414 \& before_all
415 \& for\-each\-case {
416 \& before_case
417 \& case
418 \& after_case
419 \&
420 \& # AND/OR nested describes
421 \& before_each
422 \& tests
423 \& after_each
424 \& }
425 \& after_all
426 .Ve
427 .SH SOURCE
428 .IX Header "SOURCE"
429 The source code repository for Test2\-Workflow can be found at
430 \&\fIhttps://github.com/Test\-More/test\-more/\fR.
431 .SH MAINTAINERS
432 .IX Header "MAINTAINERS"
433 .IP "Chad Granum <[email protected]>" 4
434 .IX Item "Chad Granum <[email protected]>"
435 .SH AUTHORS
436 .IX Header "AUTHORS"
437 .IP "Chad Granum <[email protected]>" 4
438 .IX Item "Chad Granum <[email protected]>"
439 .SH COPYRIGHT
440 .IX Header "COPYRIGHT"
441 Copyright Chad Granum <[email protected]>.
442 .PP
443 This program is free software; you can redistribute it and/or
444 modify it under the same terms as Perl itself.
445 .PP
446 See \fIhttp://dev.perl.org/licenses/\fR