| 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 "Test::Tester 3perl" |
| 58 |
.TH Test::Tester 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 |
Test::Tester \- Ease testing test modules built with Test::Builder |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& use Test::Tester tests => 6; |
| 69 |
\& |
| 70 |
\& use Test::MyStyle; |
| 71 |
\& |
| 72 |
\& check_test( |
| 73 |
\& sub { |
| 74 |
\& is_mystyle_eq("this", "that", "not eq"); |
| 75 |
\& }, |
| 76 |
\& { |
| 77 |
\& ok => 0, # expect this to fail |
| 78 |
\& name => "not eq", |
| 79 |
\& diag => "Expected: \*(Aqthis\*(Aq\enGot: \*(Aqthat\*(Aq", |
| 80 |
\& } |
| 81 |
\& ); |
| 82 |
.Ve |
| 83 |
.PP |
| 84 |
or |
| 85 |
.PP |
| 86 |
.Vb 1 |
| 87 |
\& use Test::Tester tests => 6; |
| 88 |
\& |
| 89 |
\& use Test::MyStyle; |
| 90 |
\& |
| 91 |
\& check_test( |
| 92 |
\& sub { |
| 93 |
\& is_mystyle_qr("this", "that", "not matching"); |
| 94 |
\& }, |
| 95 |
\& { |
| 96 |
\& ok => 0, # expect this to fail |
| 97 |
\& name => "not matching", |
| 98 |
\& diag => qr/Expected: \*(Aqthis\*(Aq\es+Got: \*(Aqthat\*(Aq/, |
| 99 |
\& } |
| 100 |
\& ); |
| 101 |
.Ve |
| 102 |
.PP |
| 103 |
or |
| 104 |
.PP |
| 105 |
.Vb 1 |
| 106 |
\& use Test::Tester; |
| 107 |
\& |
| 108 |
\& use Test::More tests => 3; |
| 109 |
\& use Test::MyStyle; |
| 110 |
\& |
| 111 |
\& my ($premature, @results) = run_tests( |
| 112 |
\& sub { |
| 113 |
\& is_database_alive("dbname"); |
| 114 |
\& } |
| 115 |
\& ); |
| 116 |
\& |
| 117 |
\& # now use Test::More::like to check the diagnostic output |
| 118 |
\& |
| 119 |
\& like($results[0]\->{diag}, "/^Database ping took \e\ed+ seconds$"/, "diag"); |
| 120 |
.Ve |
| 121 |
.SH DESCRIPTION |
| 122 |
.IX Header "DESCRIPTION" |
| 123 |
If you have written a test module based on Test::Builder then Test::Tester |
| 124 |
allows you to test it with the minimum of effort. |
| 125 |
.SH "HOW TO USE (THE EASY WAY)" |
| 126 |
.IX Header "HOW TO USE (THE EASY WAY)" |
| 127 |
From version 0.08 Test::Tester no longer requires you to included anything |
| 128 |
special in your test modules. All you need to do is |
| 129 |
.PP |
| 130 |
.Vb 1 |
| 131 |
\& use Test::Tester; |
| 132 |
.Ve |
| 133 |
.PP |
| 134 |
in your test script \fBbefore\fR any other Test::Builder based modules and away |
| 135 |
you go. |
| 136 |
.PP |
| 137 |
Other modules based on Test::Builder can be used to help with the |
| 138 |
testing. In fact you can even use functions from your module to test |
| 139 |
other functions from the same module (while this is possible it is |
| 140 |
probably not a good idea, if your module has bugs, then |
| 141 |
using it to test itself may give the wrong answers). |
| 142 |
.PP |
| 143 |
The easiest way to test is to do something like |
| 144 |
.PP |
| 145 |
.Vb 8 |
| 146 |
\& check_test( |
| 147 |
\& sub { is_mystyle_eq("this", "that", "not eq") }, |
| 148 |
\& { |
| 149 |
\& ok => 0, # we expect the test to fail |
| 150 |
\& name => "not eq", |
| 151 |
\& diag => "Expected: \*(Aqthis\*(Aq\enGot: \*(Aqthat\*(Aq", |
| 152 |
\& } |
| 153 |
\& ); |
| 154 |
.Ve |
| 155 |
.PP |
| 156 |
this will execute the is_mystyle_eq test, capturing its results and |
| 157 |
checking that they are what was expected. |
| 158 |
.PP |
| 159 |
You may need to examine the test results in a more flexible way, for |
| 160 |
example, the diagnostic output may be quite long or complex or it may involve |
| 161 |
something that you cannot predict in advance like a timestamp. In this case |
| 162 |
you can get direct access to the test results: |
| 163 |
.PP |
| 164 |
.Vb 5 |
| 165 |
\& my ($premature, @results) = run_tests( |
| 166 |
\& sub { |
| 167 |
\& is_database_alive("dbname"); |
| 168 |
\& } |
| 169 |
\& ); |
| 170 |
\& |
| 171 |
\& like($result[0]\->{diag}, "/^Database ping took \e\ed+ seconds$"/, "diag"); |
| 172 |
.Ve |
| 173 |
.PP |
| 174 |
or |
| 175 |
.PP |
| 176 |
.Vb 8 |
| 177 |
\& check_test( |
| 178 |
\& sub { is_mystyle_qr("this", "that", "not matching") }, |
| 179 |
\& { |
| 180 |
\& ok => 0, # we expect the test to fail |
| 181 |
\& name => "not matching", |
| 182 |
\& diag => qr/Expected: \*(Aqthis\*(Aq\es+Got: \*(Aqthat\*(Aq/, |
| 183 |
\& } |
| 184 |
\& ); |
| 185 |
.Ve |
| 186 |
.PP |
| 187 |
We cannot predict how long the database ping will take so we use |
| 188 |
Test::More's \fBlike()\fR test to check that the diagnostic string is of the right |
| 189 |
form. |
| 190 |
.SH "HOW TO USE (THE HARD WAY)" |
| 191 |
.IX Header "HOW TO USE (THE HARD WAY)" |
| 192 |
\&\fIThis is here for backwards compatibility only\fR |
| 193 |
.PP |
| 194 |
Make your module use the Test::Tester::Capture object instead of the |
| 195 |
Test::Builder one. How to do this depends on your module but assuming that |
| 196 |
your module holds the Test::Builder object in \f(CW$Test\fR and that all your test |
| 197 |
routines access it through \f(CW$Test\fR then providing a function something like this |
| 198 |
.PP |
| 199 |
.Vb 4 |
| 200 |
\& sub set_builder |
| 201 |
\& { |
| 202 |
\& $Test = shift; |
| 203 |
\& } |
| 204 |
.Ve |
| 205 |
.PP |
| 206 |
should allow your test scripts to do |
| 207 |
.PP |
| 208 |
.Vb 1 |
| 209 |
\& Test::YourModule::set_builder(Test::Tester\->capture); |
| 210 |
.Ve |
| 211 |
.PP |
| 212 |
and after that any tests inside your module will captured. |
| 213 |
.SH "TEST RESULTS" |
| 214 |
.IX Header "TEST RESULTS" |
| 215 |
The result of each test is captured in a hash. These hashes are the same as |
| 216 |
the hashes returned by Test::Builder\->details but with a couple of extra |
| 217 |
fields. |
| 218 |
.PP |
| 219 |
These fields are documented in Test::Builder in the \fBdetails()\fR function |
| 220 |
.IP ok 2 |
| 221 |
.IX Item "ok" |
| 222 |
Did the test pass? |
| 223 |
.IP actual_ok 2 |
| 224 |
.IX Item "actual_ok" |
| 225 |
Did the test really pass? That is, did the pass come from |
| 226 |
Test::Builder\->\fBok()\fR or did it pass because it was a TODO test? |
| 227 |
.IP name 2 |
| 228 |
.IX Item "name" |
| 229 |
The name supplied for the test. |
| 230 |
.IP type 2 |
| 231 |
.IX Item "type" |
| 232 |
What kind of test? Possibilities include, skip, todo etc. See |
| 233 |
Test::Builder for more details. |
| 234 |
.IP reason 2 |
| 235 |
.IX Item "reason" |
| 236 |
The reason for the skip, todo etc. See Test::Builder for more details. |
| 237 |
.PP |
| 238 |
These fields are exclusive to Test::Tester. |
| 239 |
.IP diag 2 |
| 240 |
.IX Item "diag" |
| 241 |
Any diagnostics that were output for the test. This only includes |
| 242 |
diagnostics output \fBafter\fR the test result is declared. |
| 243 |
.Sp |
| 244 |
Note that Test::Builder ensures that any diagnostics end in a \en and |
| 245 |
it in earlier versions of Test::Tester it was essential that you have |
| 246 |
the final \en in your expected diagnostics. From version 0.10 onward, |
| 247 |
Test::Tester will add the \en if you forgot it. It will not add a \en if |
| 248 |
you are expecting no diagnostics. See below for help tracking down |
| 249 |
hard to find space and tab related problems. |
| 250 |
.IP depth 2 |
| 251 |
.IX Item "depth" |
| 252 |
This allows you to check that your test module is setting the correct value |
| 253 |
for \f(CW$Test::Builder::Level\fR and thus giving the correct file and line number |
| 254 |
when a test fails. It is calculated by looking at \fBcaller()\fR and |
| 255 |
\&\f(CW$Test::Builder::Level\fR. It should count how many subroutines there are before |
| 256 |
jumping into the function you are testing. So for example in |
| 257 |
.Sp |
| 258 |
.Vb 1 |
| 259 |
\& run_tests( sub { my_test_function("a", "b") } ); |
| 260 |
.Ve |
| 261 |
.Sp |
| 262 |
the depth should be 1 and in |
| 263 |
.Sp |
| 264 |
.Vb 1 |
| 265 |
\& sub deeper { my_test_function("a", "b") } |
| 266 |
\& |
| 267 |
\& run_tests(sub { deeper() }); |
| 268 |
.Ve |
| 269 |
.Sp |
| 270 |
depth should be 2, that is 1 for the sub {} and one for \fBdeeper()\fR. This |
| 271 |
might seem a little complex but if your tests look like the simple |
| 272 |
examples in this doc then you don't need to worry as the depth will |
| 273 |
always be 1 and that's what Test::Tester expects by default. |
| 274 |
.Sp |
| 275 |
\&\fBNote\fR: if you do not specify a value for depth in \fBcheck_test()\fR then it |
| 276 |
automatically compares it against 1, if you really want to skip the depth |
| 277 |
test then pass in undef. |
| 278 |
.Sp |
| 279 |
\&\fBNote\fR: depth will not be correctly calculated for tests that run from a |
| 280 |
signal handler or an END block or anywhere else that hides the call stack. |
| 281 |
.PP |
| 282 |
Some of Test::Tester's functions return arrays of these hashes, just |
| 283 |
like Test::Builder\->details. That is, the hash for the first test will |
| 284 |
be array element 1 (not 0). Element 0 will not be a hash it will be a |
| 285 |
string which contains any diagnostic output that came before the first |
| 286 |
test. This should usually be empty, if it's not, it means something |
| 287 |
output diagnostics before any test results showed up. |
| 288 |
.SH "SPACES AND TABS" |
| 289 |
.IX Header "SPACES AND TABS" |
| 290 |
Appearances can be deceptive, especially when it comes to emptiness. If you |
| 291 |
are scratching your head trying to work out why Test::Tester is saying that |
| 292 |
your diagnostics are wrong when they look perfectly right then the answer is |
| 293 |
probably whitespace. From version 0.10 on, Test::Tester surrounds the |
| 294 |
expected and got diag values with single quotes to make it easier to spot |
| 295 |
trailing whitespace. So in this example |
| 296 |
.PP |
| 297 |
.Vb 4 |
| 298 |
\& # Got diag (5 bytes): |
| 299 |
\& # \*(Aqabcd \*(Aq |
| 300 |
\& # Expected diag (4 bytes): |
| 301 |
\& # \*(Aqabcd\*(Aq |
| 302 |
.Ve |
| 303 |
.PP |
| 304 |
it is quite clear that there is a space at the end of the first string. |
| 305 |
Another way to solve this problem is to use colour and inverse video on an |
| 306 |
ANSI terminal, see below COLOUR below if you want this. |
| 307 |
.PP |
| 308 |
Unfortunately this is sometimes not enough, neither colour nor quotes will |
| 309 |
help you with problems involving tabs, other non-printing characters and |
| 310 |
certain kinds of problems inherent in Unicode. To deal with this, you can |
| 311 |
switch Test::Tester into a mode whereby all "tricky" characters are shown as |
| 312 |
\&\e{xx}. Tricky characters are those with ASCII code less than 33 or higher |
| 313 |
than 126. This makes the output more difficult to read but much easier to |
| 314 |
find subtle differences between strings. To turn on this mode either call |
| 315 |
\&\f(CWshow_space()\fR in your test script or set the \f(CW\*(C`TESTTESTERSPACE\*(C'\fR environment |
| 316 |
variable to be a true value. The example above would then look like |
| 317 |
.PP |
| 318 |
.Vb 4 |
| 319 |
\& # Got diag (5 bytes): |
| 320 |
\& # abcd\ex{20} |
| 321 |
\& # Expected diag (4 bytes): |
| 322 |
\& # abcd |
| 323 |
.Ve |
| 324 |
.SH COLOUR |
| 325 |
.IX Header "COLOUR" |
| 326 |
If you prefer to use colour as a means of finding tricky whitespace |
| 327 |
characters then you can set the \f(CW\*(C`TESTTESTCOLOUR\*(C'\fR environment variable to a |
| 328 |
comma separated pair of colours, the first for the foreground, the second |
| 329 |
for the background. For example "white,red" will print white text on a red |
| 330 |
background. This requires the Term::ANSIColor module. You can specify any |
| 331 |
colour that would be acceptable to the Term::ANSIColor::color function. |
| 332 |
.PP |
| 333 |
If you spell colour differently, that's no problem. The \f(CW\*(C`TESTTESTERCOLOR\*(C'\fR |
| 334 |
variable also works (if both are set then the British spelling wins out). |
| 335 |
.SH "EXPORTED FUNCTIONS" |
| 336 |
.IX Header "EXPORTED FUNCTIONS" |
| 337 |
\fI($premature, \fR\f(CI@results\fR\fI) = run_tests(\e&test_sub)\fR |
| 338 |
.IX Subsection "($premature, @results) = run_tests(&test_sub)" |
| 339 |
.PP |
| 340 |
\&\e&test_sub is a reference to a subroutine. |
| 341 |
.PP |
| 342 |
run_tests runs the subroutine in \f(CW$test_sub\fR and captures the results of any |
| 343 |
tests inside it. You can run more than 1 test inside this subroutine if you |
| 344 |
like. |
| 345 |
.PP |
| 346 |
\&\f(CW$premature\fR is a string containing any diagnostic output from before |
| 347 |
the first test. |
| 348 |
.PP |
| 349 |
\&\f(CW@results\fR is an array of test result hashes. |
| 350 |
.PP |
| 351 |
\fIcmp_result(\e%result, \e%expect, \fR\f(CI$name\fR\fI)\fR |
| 352 |
.IX Subsection "cmp_result(%result, %expect, $name)" |
| 353 |
.PP |
| 354 |
\&\e%result is a ref to a test result hash. |
| 355 |
.PP |
| 356 |
\&\e%expect is a ref to a hash of expected values for the test result. |
| 357 |
.PP |
| 358 |
cmp_result compares the result with the expected values. If any differences |
| 359 |
are found it outputs diagnostics. You may leave out any field from the |
| 360 |
expected result and cmp_result will not do the comparison of that field. |
| 361 |
.PP |
| 362 |
\fIcmp_results(\e@results, \e@expects, \fR\f(CI$name\fR\fI)\fR |
| 363 |
.IX Subsection "cmp_results(@results, @expects, $name)" |
| 364 |
.PP |
| 365 |
\&\e@results is a ref to an array of test results. |
| 366 |
.PP |
| 367 |
\&\e@expects is a ref to an array of hash refs. |
| 368 |
.PP |
| 369 |
cmp_results checks that the results match the expected results and if any |
| 370 |
differences are found it outputs diagnostics. It first checks that the |
| 371 |
number of elements in \e@results and \e@expects is the same. Then it goes |
| 372 |
through each result checking it against the expected result as in |
| 373 |
\&\fBcmp_result()\fR above. |
| 374 |
.PP |
| 375 |
\fI($premature, \fR\f(CI@results\fR\fI) = check_tests(\e&test_sub, \e@expects, \fR\f(CI$name\fR\fI)\fR |
| 376 |
.IX Subsection "($premature, @results) = check_tests(&test_sub, @expects, $name)" |
| 377 |
.PP |
| 378 |
\&\e&test_sub is a reference to a subroutine. |
| 379 |
.PP |
| 380 |
\&\e@expect is a ref to an array of hash refs which are expected test results. |
| 381 |
.PP |
| 382 |
check_tests combines run_tests and cmp_tests into a single call. It also |
| 383 |
checks if the tests died at any stage. |
| 384 |
.PP |
| 385 |
It returns the same values as run_tests, so you can further examine the test |
| 386 |
results if you need to. |
| 387 |
.PP |
| 388 |
\fI($premature, \fR\f(CI@results\fR\fI) = check_test(\e&test_sub, \e%expect, \fR\f(CI$name\fR\fI)\fR |
| 389 |
.IX Subsection "($premature, @results) = check_test(&test_sub, %expect, $name)" |
| 390 |
.PP |
| 391 |
\&\e&test_sub is a reference to a subroutine. |
| 392 |
.PP |
| 393 |
\&\e%expect is a ref to an hash of expected values for the test result. |
| 394 |
.PP |
| 395 |
check_test is a wrapper around check_tests. It combines run_tests and |
| 396 |
cmp_tests into a single call, checking if the test died. It assumes |
| 397 |
that only a single test is run inside \e&test_sub and include a test to |
| 398 |
make sure this is true. |
| 399 |
.PP |
| 400 |
It returns the same values as run_tests, so you can further examine the test |
| 401 |
results if you need to. |
| 402 |
.PP |
| 403 |
\fR\f(BIshow_space()\fR\fI\fR |
| 404 |
.IX Subsection "show_space()" |
| 405 |
.PP |
| 406 |
Turn on the escaping of characters as described in the SPACES AND TABS |
| 407 |
section. |
| 408 |
.SH "HOW IT WORKS" |
| 409 |
.IX Header "HOW IT WORKS" |
| 410 |
Normally, a test module (let's call it Test:MyStyle) calls |
| 411 |
Test::Builder\->new to get the Test::Builder object. Test::MyStyle calls |
| 412 |
methods on this object to record information about test results. When |
| 413 |
Test::Tester is loaded, it replaces Test::Builder's \fBnew()\fR method with one |
| 414 |
which returns a Test::Tester::Delegate object. Most of the time this object |
| 415 |
behaves as the real Test::Builder object. Any methods that are called are |
| 416 |
delegated to the real Test::Builder object so everything works perfectly. |
| 417 |
However once we go into test mode, the method calls are no longer passed to |
| 418 |
the real Test::Builder object, instead they go to the Test::Tester::Capture |
| 419 |
object. This object seems exactly like the real Test::Builder object, |
| 420 |
except, instead of outputting test results and diagnostics, it just records |
| 421 |
all the information for later analysis. |
| 422 |
.SH CAVEATS |
| 423 |
.IX Header "CAVEATS" |
| 424 |
Support for calling Test::Builder\->note is minimal. It's implemented |
| 425 |
as an empty stub, so modules that use it will not crash but the calls |
| 426 |
are not recorded for testing purposes like the others. Patches |
| 427 |
welcome. |
| 428 |
.SH "SEE ALSO" |
| 429 |
.IX Header "SEE ALSO" |
| 430 |
Test::Builder the source of testing goodness. Test::Builder::Tester |
| 431 |
for an alternative approach to the problem tackled by Test::Tester \- |
| 432 |
captures the strings output by Test::Builder. This means you cannot get |
| 433 |
separate access to the individual pieces of information and you must predict |
| 434 |
\&\fBexactly\fR what your test will output. |
| 435 |
.SH AUTHOR |
| 436 |
.IX Header "AUTHOR" |
| 437 |
This module is copyright 2005 Fergal Daly <[email protected]>, some parts |
| 438 |
are based on other people's work. |
| 439 |
.PP |
| 440 |
Plan handling lifted from Test::More. written by Michael G Schwern |
| 441 |
<[email protected]>. |
| 442 |
.PP |
| 443 |
Test::Tester::Capture is a cut down and hacked up version of Test::Builder. |
| 444 |
Test::Builder was written by chromatic <[email protected]> and Michael G |
| 445 |
Schwern <[email protected]>. |
| 446 |
.SH LICENSE |
| 447 |
.IX Header "LICENSE" |
| 448 |
Under the same license as Perl itself |
| 449 |
.PP |
| 450 |
See <https://dev.perl.org/licenses/> |