| 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::TestBuilder 3perl" |
| 58 |
.TH Test2::Manual::Tooling::TestBuilder 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::TestBuilder \- This section maps Test::Builder methods |
| 65 |
to Test2 concepts. |
| 66 |
.SH DESCRIPTION |
| 67 |
.IX Header "DESCRIPTION" |
| 68 |
With Test::Builder tools were encouraged to use methods on the Test::Builder |
| 69 |
singleton object. Test2 has a different approach, every tool should get a new |
| 70 |
Test2::API::Context object, and call methods on that. This document maps |
| 71 |
several concepts from Test::Builder to Test2. |
| 72 |
.SH CONTEXT |
| 73 |
.IX Header "CONTEXT" |
| 74 |
First thing to do, stop using the Test::Builder singleton, in fact stop using |
| 75 |
or even loading Test::Builder. Instead of Test::Builder each tool you write |
| 76 |
should follow this template: |
| 77 |
.PP |
| 78 |
.Vb 1 |
| 79 |
\& use Test2::API qw/context/; |
| 80 |
\& |
| 81 |
\& sub my_tool { |
| 82 |
\& my $ctx = context(); |
| 83 |
\& |
| 84 |
\& ... do work ... |
| 85 |
\& |
| 86 |
\& $ctx\->ok(1, "a passing assertion"); |
| 87 |
\& |
| 88 |
\& $ctx\->release; |
| 89 |
\& |
| 90 |
\& return $whatever; |
| 91 |
\& } |
| 92 |
.Ve |
| 93 |
.PP |
| 94 |
The original Test::Builder style was this: |
| 95 |
.PP |
| 96 |
.Vb 2 |
| 97 |
\& use Test::Builder; |
| 98 |
\& my $tb = Test::Builder\->new; # gets the singleton |
| 99 |
\& |
| 100 |
\& sub my_tool { |
| 101 |
\& ... do work ... |
| 102 |
\& |
| 103 |
\& $tb\->ok(1, "a passing assertion"); |
| 104 |
\& |
| 105 |
\& return $whatever; |
| 106 |
\& } |
| 107 |
.Ve |
| 108 |
.SH "TEST BUILDER METHODS" |
| 109 |
.IX Header "TEST BUILDER METHODS" |
| 110 |
.ie n .IP $tb\->BAIL_OUT($reason) 4 |
| 111 |
.el .IP \f(CW$tb\fR\->BAIL_OUT($reason) 4 |
| 112 |
.IX Item "$tb->BAIL_OUT($reason)" |
| 113 |
The context object has a 'bail' method: |
| 114 |
.Sp |
| 115 |
.Vb 1 |
| 116 |
\& $ctx\->bail($reason) |
| 117 |
.Ve |
| 118 |
.ie n .IP $tb\->diag($string) 4 |
| 119 |
.el .IP \f(CW$tb\fR\->diag($string) 4 |
| 120 |
.IX Item "$tb->diag($string)" |
| 121 |
.PD 0 |
| 122 |
.ie n .IP $tb\->note($string) 4 |
| 123 |
.el .IP \f(CW$tb\fR\->note($string) 4 |
| 124 |
.IX Item "$tb->note($string)" |
| 125 |
.PD |
| 126 |
The context object has diag and note methods: |
| 127 |
.Sp |
| 128 |
.Vb 2 |
| 129 |
\& $ctx\->diag($string); |
| 130 |
\& $ctx\->note($string); |
| 131 |
.Ve |
| 132 |
.ie n .IP $tb\->done_testing 4 |
| 133 |
.el .IP \f(CW$tb\fR\->done_testing 4 |
| 134 |
.IX Item "$tb->done_testing" |
| 135 |
The context object has a done_testing method: |
| 136 |
.Sp |
| 137 |
.Vb 1 |
| 138 |
\& $ctx\->done_testing; |
| 139 |
.Ve |
| 140 |
.Sp |
| 141 |
Unlike the Test::Builder version, no arguments are allowed. |
| 142 |
.ie n .IP $tb\->like 4 |
| 143 |
.el .IP \f(CW$tb\fR\->like 4 |
| 144 |
.IX Item "$tb->like" |
| 145 |
.PD 0 |
| 146 |
.ie n .IP $tb\->unlike 4 |
| 147 |
.el .IP \f(CW$tb\fR\->unlike 4 |
| 148 |
.IX Item "$tb->unlike" |
| 149 |
.PD |
| 150 |
These are not part of context, instead look at Test2::Compare and |
| 151 |
Test2::Tools::Compare. |
| 152 |
.ie n .IP "$tb\->ok($bool, $name)" 4 |
| 153 |
.el .IP "\f(CW$tb\fR\->ok($bool, \f(CW$name\fR)" 4 |
| 154 |
.IX Item "$tb->ok($bool, $name)" |
| 155 |
.Vb 3 |
| 156 |
\& # Preferred |
| 157 |
\& $ctx\->pass($name); |
| 158 |
\& $ctx\->fail($name, @diag); |
| 159 |
\& |
| 160 |
\& # Discouraged, but supported: |
| 161 |
\& $ctx\->ok($bool, $name, \e@failure_diags) |
| 162 |
.Ve |
| 163 |
.ie n .IP $tb\->subtest 4 |
| 164 |
.el .IP \f(CW$tb\fR\->subtest 4 |
| 165 |
.IX Item "$tb->subtest" |
| 166 |
use the \f(CWTest2::API::run_subtest()\fR function instead. See Test2::API for documentation. |
| 167 |
.ie n .IP $tb\->todo_start 4 |
| 168 |
.el .IP \f(CW$tb\fR\->todo_start 4 |
| 169 |
.IX Item "$tb->todo_start" |
| 170 |
.PD 0 |
| 171 |
.ie n .IP $tb\->todo_end 4 |
| 172 |
.el .IP \f(CW$tb\fR\->todo_end 4 |
| 173 |
.IX Item "$tb->todo_end" |
| 174 |
.PD |
| 175 |
See Test2::Tools::Todo instead. |
| 176 |
.ie n .IP "$tb\->output, $tb\->failure_output, and $tb\->todo_output" 4 |
| 177 |
.el .IP "\f(CW$tb\fR\->output, \f(CW$tb\fR\->failure_output, and \f(CW$tb\fR\->todo_output" 4 |
| 178 |
.IX Item "$tb->output, $tb->failure_output, and $tb->todo_output" |
| 179 |
These are handled via formatters now. See Test2::Formatter and |
| 180 |
Test2::Formatter::TAP. |
| 181 |
.SH LEVEL |
| 182 |
.IX Header "LEVEL" |
| 183 |
Test::Builder had the \f(CW$Test::Builder::Level\fR variable that you could |
| 184 |
modify in order to set the stack depth. This was useful if you needed to nest |
| 185 |
tools and wanted to make sure your file and line number were correct. It was |
| 186 |
also frustrating and prone to errors. Some people never even discovered the |
| 187 |
level variable and always had incorrect line numbers when their tools would |
| 188 |
fail. |
| 189 |
.PP |
| 190 |
Test2 uses the context system, which solves the problem a better way. The |
| 191 |
top-most tool get a context, and holds on to it until it is done. Any tool |
| 192 |
nested under the first will find and use the original context instead of |
| 193 |
generating a new one. This means the level problem is solved for free, no |
| 194 |
variables to mess with. |
| 195 |
.PP |
| 196 |
Test2 is also smart enough to honor \f(CW$Test::Builder::Level\fR if it is set. |
| 197 |
.SH TODO |
| 198 |
.IX Header "TODO" |
| 199 |
Test::Builder used the \f(CW$TODO\fR package variable to set the TODO state. This |
| 200 |
was confusing, and easy to get wrong. See Test2::Tools::Todo for the modern |
| 201 |
way to accomplish a TODO state. |
| 202 |
.SH "SEE ALSO" |
| 203 |
.IX Header "SEE ALSO" |
| 204 |
Test2::Manual \- Primary index of the manual. |
| 205 |
.SH SOURCE |
| 206 |
.IX Header "SOURCE" |
| 207 |
The source code repository for Test2\-Manual can be found at |
| 208 |
\&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR. |
| 209 |
.SH MAINTAINERS |
| 210 |
.IX Header "MAINTAINERS" |
| 211 |
.IP "Chad Granum <[email protected]>" 4 |
| 212 |
.IX Item "Chad Granum <[email protected]>" |
| 213 |
.SH AUTHORS |
| 214 |
.IX Header "AUTHORS" |
| 215 |
.PD 0 |
| 216 |
.IP "Chad Granum <[email protected]>" 4 |
| 217 |
.IX Item "Chad Granum <[email protected]>" |
| 218 |
.PD |
| 219 |
.SH COPYRIGHT |
| 220 |
.IX Header "COPYRIGHT" |
| 221 |
Copyright 2018 Chad Granum <[email protected]>. |
| 222 |
.PP |
| 223 |
This program is free software; you can redistribute it and/or |
| 224 |
modify it under the same terms as Perl itself. |
| 225 |
.PP |
| 226 |
See \fIhttp://dev.perl.org/licenses/\fR |