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