| 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 3perl" |
| 58 |
.TH Test 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 \- provides a simple framework for writing test scripts |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 2 |
| 68 |
\& use strict; |
| 69 |
\& use Test; |
| 70 |
\& |
| 71 |
\& # use a BEGIN block so we print our plan before MyModule is loaded |
| 72 |
\& BEGIN { plan tests => 14, todo => [3,4] } |
| 73 |
\& |
| 74 |
\& # load your module... |
| 75 |
\& use MyModule; |
| 76 |
\& |
| 77 |
\& # Helpful notes. All note\-lines must start with a "#". |
| 78 |
\& print "# I\*(Aqm testing MyModule version $MyModule::VERSION\en"; |
| 79 |
\& |
| 80 |
\& ok(0); # failure |
| 81 |
\& ok(1); # success |
| 82 |
\& |
| 83 |
\& ok(0); # ok, expected failure (see todo list, above) |
| 84 |
\& ok(1); # surprise success! |
| 85 |
\& |
| 86 |
\& ok(0,1); # failure: \*(Aq0\*(Aq ne \*(Aq1\*(Aq |
| 87 |
\& ok(\*(Aqbroke\*(Aq,\*(Aqfixed\*(Aq); # failure: \*(Aqbroke\*(Aq ne \*(Aqfixed\*(Aq |
| 88 |
\& ok(\*(Aqfixed\*(Aq,\*(Aqfixed\*(Aq); # success: \*(Aqfixed\*(Aq eq \*(Aqfixed\*(Aq |
| 89 |
\& ok(\*(Aqfixed\*(Aq,qr/x/); # success: \*(Aqfixed\*(Aq =~ qr/x/ |
| 90 |
\& |
| 91 |
\& ok(sub { 1+1 }, 2); # success: \*(Aq2\*(Aq eq \*(Aq2\*(Aq |
| 92 |
\& ok(sub { 1+1 }, 3); # failure: \*(Aq2\*(Aq ne \*(Aq3\*(Aq |
| 93 |
\& |
| 94 |
\& my @list = (0,0); |
| 95 |
\& ok @list, 3, "\e@list=".join(\*(Aq,\*(Aq,@list); #extra notes |
| 96 |
\& ok \*(Aqsegmentation fault\*(Aq, \*(Aq/(?i)success/\*(Aq; #regex match |
| 97 |
\& |
| 98 |
\& skip( |
| 99 |
\& $^O =~ m/MSWin/ ? "Skip if MSWin" : 0, # whether to skip |
| 100 |
\& $foo, $bar # arguments just like for ok(...) |
| 101 |
\& ); |
| 102 |
\& skip( |
| 103 |
\& $^O =~ m/MSWin/ ? 0 : "Skip unless MSWin", # whether to skip |
| 104 |
\& $foo, $bar # arguments just like for ok(...) |
| 105 |
\& ); |
| 106 |
.Ve |
| 107 |
.SH DESCRIPTION |
| 108 |
.IX Header "DESCRIPTION" |
| 109 |
This module simplifies the task of writing test files for Perl modules, |
| 110 |
such that their output is in the format that |
| 111 |
Test::Harness expects to see. |
| 112 |
.SH "QUICK START GUIDE" |
| 113 |
.IX Header "QUICK START GUIDE" |
| 114 |
To write a test for your new (and probably not even done) module, create |
| 115 |
a new file called \fIt/test.t\fR (in a new \fIt\fR directory). If you have |
| 116 |
multiple test files, to test the "foo", "bar", and "baz" feature sets, |
| 117 |
then feel free to call your files \fIt/foo.t\fR, \fIt/bar.t\fR, and |
| 118 |
\&\fIt/baz.t\fR |
| 119 |
.SS Functions |
| 120 |
.IX Subsection "Functions" |
| 121 |
This module defines three public functions, \f(CWplan(...)\fR, \f(CWok(...)\fR, |
| 122 |
and \f(CWskip(...)\fR. By default, all three are exported by |
| 123 |
the \f(CW\*(C`use Test;\*(C'\fR statement. |
| 124 |
.ie n .IP plan(...) 4 |
| 125 |
.el .IP \f(CWplan(...)\fR 4 |
| 126 |
.IX Item "plan(...)" |
| 127 |
.Vb 1 |
| 128 |
\& BEGIN { plan %theplan; } |
| 129 |
.Ve |
| 130 |
.Sp |
| 131 |
This should be the first thing you call in your test script. It |
| 132 |
declares your testing plan, how many there will be, if any of them |
| 133 |
should be allowed to fail, and so on. |
| 134 |
.Sp |
| 135 |
Typical usage is just: |
| 136 |
.Sp |
| 137 |
.Vb 2 |
| 138 |
\& use Test; |
| 139 |
\& BEGIN { plan tests => 23 } |
| 140 |
.Ve |
| 141 |
.Sp |
| 142 |
These are the things that you can put in the parameters to plan: |
| 143 |
.RS 4 |
| 144 |
.ie n .IP """tests => \fInumber\fR""" 4 |
| 145 |
.el .IP "\f(CWtests => \fR\f(CInumber\fR\f(CW\fR" 4 |
| 146 |
.IX Item "tests => number" |
| 147 |
The number of tests in your script. |
| 148 |
This means all \fBok()\fR and \fBskip()\fR calls. |
| 149 |
.ie n .IP """todo => [\fI1,5,14\fR]""" 4 |
| 150 |
.el .IP "\f(CWtodo => [\fR\f(CI1,5,14\fR\f(CW]\fR" 4 |
| 151 |
.IX Item "todo => [1,5,14]" |
| 152 |
A reference to a list of tests which are allowed to fail. |
| 153 |
See "TODO TESTS". |
| 154 |
.ie n .IP """onfail => sub { ... }""" 4 |
| 155 |
.el .IP "\f(CWonfail => sub { ... }\fR" 4 |
| 156 |
.IX Item "onfail => sub { ... }" |
| 157 |
.PD 0 |
| 158 |
.ie n .IP """onfail => \e&some_sub""" 4 |
| 159 |
.el .IP "\f(CWonfail => \e&some_sub\fR" 4 |
| 160 |
.IX Item "onfail => &some_sub" |
| 161 |
.PD |
| 162 |
A subroutine reference to be run at the end of the test script, if |
| 163 |
any of the tests fail. See "ONFAIL". |
| 164 |
.RE |
| 165 |
.RS 4 |
| 166 |
.Sp |
| 167 |
You must call \f(CWplan(...)\fR once and only once. You should call it |
| 168 |
in a \f(CW\*(C`BEGIN {...}\*(C'\fR block, like so: |
| 169 |
.Sp |
| 170 |
.Vb 1 |
| 171 |
\& BEGIN { plan tests => 23 } |
| 172 |
.Ve |
| 173 |
.RE |
| 174 |
.ie n .IP ok(...) 4 |
| 175 |
.el .IP \f(CWok(...)\fR 4 |
| 176 |
.IX Item "ok(...)" |
| 177 |
.Vb 3 |
| 178 |
\& ok(1 + 1 == 2); |
| 179 |
\& ok($have, $expect); |
| 180 |
\& ok($have, $expect, $diagnostics); |
| 181 |
.Ve |
| 182 |
.Sp |
| 183 |
This function is the reason for \f(CW\*(C`Test\*(C'\fR's existence. It's |
| 184 |
the basic function that |
| 185 |
handles printing "\f(CW\*(C`ok\*(C'\fR" or "\f(CW\*(C`not ok\*(C'\fR", along with the |
| 186 |
current test number. (That's what \f(CW\*(C`Test::Harness\*(C'\fR wants to see.) |
| 187 |
.Sp |
| 188 |
In its most basic usage, \f(CWok(...)\fR simply takes a single scalar |
| 189 |
expression. If its value is true, the test passes; if false, |
| 190 |
the test fails. Examples: |
| 191 |
.Sp |
| 192 |
.Vb 1 |
| 193 |
\& # Examples of ok(scalar) |
| 194 |
\& |
| 195 |
\& ok( 1 + 1 == 2 ); # ok if 1 + 1 == 2 |
| 196 |
\& ok( $foo =~ /bar/ ); # ok if $foo contains \*(Aqbar\*(Aq |
| 197 |
\& ok( baz($x + $y) eq \*(AqArmondo\*(Aq ); # ok if baz($x + $y) returns |
| 198 |
\& # \*(AqArmondo\*(Aq |
| 199 |
\& ok( @a == @b ); # ok if @a and @b are the same |
| 200 |
\& # length |
| 201 |
.Ve |
| 202 |
.Sp |
| 203 |
The expression is evaluated in scalar context. So the following will |
| 204 |
work: |
| 205 |
.Sp |
| 206 |
.Vb 4 |
| 207 |
\& ok( @stuff ); # ok if @stuff has any |
| 208 |
\& # elements |
| 209 |
\& ok( !grep !defined $_, @stuff ); # ok if everything in @stuff |
| 210 |
\& # is defined. |
| 211 |
.Ve |
| 212 |
.Sp |
| 213 |
A special case is if the expression is a subroutine reference (in either |
| 214 |
\&\f(CW\*(C`sub {...}\*(C'\fR syntax or \f(CW\*(C`\e&foo\*(C'\fR syntax). In |
| 215 |
that case, it is executed and its value (true or false) determines if |
| 216 |
the test passes or fails. For example, |
| 217 |
.Sp |
| 218 |
.Vb 5 |
| 219 |
\& ok( sub { # See whether sleep works at least passably |
| 220 |
\& my $start_time = time; |
| 221 |
\& sleep 5; |
| 222 |
\& time() \- $start_time >= 4 |
| 223 |
\& }); |
| 224 |
.Ve |
| 225 |
.Sp |
| 226 |
In its two-argument form, \f(CW\*(C`ok(\fR\f(CIarg1\fR\f(CW, \fR\f(CIarg2\fR\f(CW)\*(C'\fR compares the two |
| 227 |
scalar values to see if they match. They match if both are undefined, |
| 228 |
or if \fIarg2\fR is a regex that matches \fIarg1\fR, or if they compare equal |
| 229 |
with \f(CW\*(C`eq\*(C'\fR. |
| 230 |
.Sp |
| 231 |
.Vb 1 |
| 232 |
\& # Example of ok(scalar, scalar) |
| 233 |
\& |
| 234 |
\& ok( "this", "that" ); # not ok, \*(Aqthis\*(Aq ne \*(Aqthat\*(Aq |
| 235 |
\& ok( "", undef ); # not ok, "" is defined |
| 236 |
.Ve |
| 237 |
.Sp |
| 238 |
The second argument is considered a regex if it is either a regex |
| 239 |
object or a string that looks like a regex. Regex objects are |
| 240 |
constructed with the qr// operator in recent versions of perl. A |
| 241 |
string is considered to look like a regex if its first and last |
| 242 |
characters are "/", or if the first character is "m" |
| 243 |
and its second and last characters are both the |
| 244 |
same non-alphanumeric non-whitespace character. These regexp |
| 245 |
.Sp |
| 246 |
Regex examples: |
| 247 |
.Sp |
| 248 |
.Vb 4 |
| 249 |
\& ok( \*(AqJaffO\*(Aq, \*(Aq/Jaff/\*(Aq ); # ok, \*(AqJaffO\*(Aq =~ /Jaff/ |
| 250 |
\& ok( \*(AqJaffO\*(Aq, \*(Aqm|Jaff|\*(Aq ); # ok, \*(AqJaffO\*(Aq =~ m|Jaff| |
| 251 |
\& ok( \*(AqJaffO\*(Aq, qr/Jaff/ ); # ok, \*(AqJaffO\*(Aq =~ qr/Jaff/; |
| 252 |
\& ok( \*(AqJaffO\*(Aq, \*(Aq/(?i)jaff/ ); # ok, \*(AqJaffO\*(Aq =~ /jaff/i; |
| 253 |
.Ve |
| 254 |
.Sp |
| 255 |
If either (or both!) is a subroutine reference, it is run and used |
| 256 |
as the value for comparing. For example: |
| 257 |
.Sp |
| 258 |
.Vb 10 |
| 259 |
\& ok sub { |
| 260 |
\& open(OUT, \*(Aq>\*(Aq, \*(Aqx.dat\*(Aq) || die $!; |
| 261 |
\& print OUT "\ex{e000}"; |
| 262 |
\& close OUT; |
| 263 |
\& my $bytecount = \-s \*(Aqx.dat\*(Aq; |
| 264 |
\& unlink \*(Aqx.dat\*(Aq or warn "Can\*(Aqt unlink : $!"; |
| 265 |
\& return $bytecount; |
| 266 |
\& }, |
| 267 |
\& 4 |
| 268 |
\& ; |
| 269 |
.Ve |
| 270 |
.Sp |
| 271 |
The above test passes two values to \f(CW\*(C`ok(arg1, arg2)\*(C'\fR \-\- the first |
| 272 |
a coderef, and the second is the number 4. Before \f(CW\*(C`ok\*(C'\fR compares them, |
| 273 |
it calls the coderef, and uses its return value as the real value of |
| 274 |
this parameter. Assuming that \f(CW$bytecount\fR returns 4, \f(CW\*(C`ok\*(C'\fR ends up |
| 275 |
testing \f(CW\*(C`4 eq 4\*(C'\fR. Since that's true, this test passes. |
| 276 |
.Sp |
| 277 |
Finally, you can append an optional third argument, in |
| 278 |
\&\f(CW\*(C`ok(\fR\f(CIarg1\fR\f(CW,\fR\f(CIarg2\fR\f(CW, \fR\f(CInote\fR\f(CW)\*(C'\fR, where \fInote\fR is a string value that |
| 279 |
will be printed if the test fails. This should be some useful |
| 280 |
information about the test, pertaining to why it failed, and/or |
| 281 |
a description of the test. For example: |
| 282 |
.Sp |
| 283 |
.Vb 4 |
| 284 |
\& ok( grep($_ eq \*(Aqsomething unique\*(Aq, @stuff), 1, |
| 285 |
\& "Something that should be unique isn\*(Aqt!\en". |
| 286 |
\& \*(Aq@stuff = \*(Aq.join \*(Aq, \*(Aq, @stuff |
| 287 |
\& ); |
| 288 |
.Ve |
| 289 |
.Sp |
| 290 |
Unfortunately, a note cannot be used with the single argument |
| 291 |
style of \f(CWok()\fR. That is, if you try \f(CW\*(C`ok(\fR\f(CIarg1\fR\f(CW, \fR\f(CInote\fR\f(CW)\*(C'\fR, then |
| 292 |
\&\f(CW\*(C`Test\*(C'\fR will interpret this as \f(CW\*(C`ok(\fR\f(CIarg1\fR\f(CW, \fR\f(CIarg2\fR\f(CW)\*(C'\fR, and probably |
| 293 |
end up testing \f(CW\*(C`\fR\f(CIarg1\fR\f(CW eq \fR\f(CIarg2\fR\f(CW\*(C'\fR \-\- and that's not what you want! |
| 294 |
.Sp |
| 295 |
All of the above special cases can occasionally cause some |
| 296 |
problems. See "BUGS and CAVEATS". |
| 297 |
.ie n .IP """skip(\fIskip_if_true\fR, \fIargs...\fR)""" 4 |
| 298 |
.el .IP "\f(CWskip(\fR\f(CIskip_if_true\fR\f(CW, \fR\f(CIargs...\fR\f(CW)\fR" 4 |
| 299 |
.IX Item "skip(skip_if_true, args...)" |
| 300 |
This is used for tests that under some conditions can be skipped. It's |
| 301 |
basically equivalent to: |
| 302 |
.Sp |
| 303 |
.Vb 5 |
| 304 |
\& if( $skip_if_true ) { |
| 305 |
\& ok(1); |
| 306 |
\& } else { |
| 307 |
\& ok( args... ); |
| 308 |
\& } |
| 309 |
.Ve |
| 310 |
.Sp |
| 311 |
\&...except that the \f(CWok(1)\fR emits not just "\f(CW\*(C`ok \fR\f(CItestnum\fR\f(CW\*(C'\fR" but |
| 312 |
actually "\f(CW\*(C`ok \fR\f(CItestnum\fR\f(CW # \fR\f(CIskip_if_true_value\fR\f(CW\*(C'\fR". |
| 313 |
.Sp |
| 314 |
The arguments after the \fIskip_if_true\fR are what is fed to \f(CWok(...)\fR if |
| 315 |
this test isn't skipped. |
| 316 |
.Sp |
| 317 |
Example usage: |
| 318 |
.Sp |
| 319 |
.Vb 2 |
| 320 |
\& my $if_MSWin = |
| 321 |
\& $^O =~ m/MSWin/ ? \*(AqSkip if under MSWin\*(Aq : \*(Aq\*(Aq; |
| 322 |
\& |
| 323 |
\& # A test to be skipped if under MSWin (i.e., run except under |
| 324 |
\& # MSWin) |
| 325 |
\& skip($if_MSWin, thing($foo), thing($bar) ); |
| 326 |
.Ve |
| 327 |
.Sp |
| 328 |
Or, going the other way: |
| 329 |
.Sp |
| 330 |
.Vb 2 |
| 331 |
\& my $unless_MSWin = |
| 332 |
\& $^O =~ m/MSWin/ ? \*(Aq\*(Aq : \*(AqSkip unless under MSWin\*(Aq; |
| 333 |
\& |
| 334 |
\& # A test to be skipped unless under MSWin (i.e., run only under |
| 335 |
\& # MSWin) |
| 336 |
\& skip($unless_MSWin, thing($foo), thing($bar) ); |
| 337 |
.Ve |
| 338 |
.Sp |
| 339 |
The tricky thing to remember is that the first parameter is true if |
| 340 |
you want to \fIskip\fR the test, not \fIrun\fR it; and it also doubles as a |
| 341 |
note about why it's being skipped. So in the first codeblock above, read |
| 342 |
the code as "skip if MSWin \-\- (otherwise) test whether \f(CWthing($foo)\fR is |
| 343 |
\&\f(CWthing($bar)\fR" or for the second case, "skip unless MSWin...". |
| 344 |
.Sp |
| 345 |
Also, when your \fIskip_if_reason\fR string is true, it really should (for |
| 346 |
backwards compatibility with older Test.pm versions) start with the |
| 347 |
string "Skip", as shown in the above examples. |
| 348 |
.Sp |
| 349 |
Note that in the above cases, \f(CWthing($foo)\fR and \f(CWthing($bar)\fR |
| 350 |
\&\fIare\fR evaluated \-\- but as long as the \f(CW\*(C`skip_if_true\*(C'\fR is true, |
| 351 |
then we \f(CWskip(...)\fR just tosses out their value (i.e., not |
| 352 |
bothering to treat them like values to \f(CWok(...)\fR. But if |
| 353 |
you need to \fInot\fR eval the arguments when skipping the |
| 354 |
test, use |
| 355 |
this format: |
| 356 |
.Sp |
| 357 |
.Vb 7 |
| 358 |
\& skip( $unless_MSWin, |
| 359 |
\& sub { |
| 360 |
\& # This code returns true if the test passes. |
| 361 |
\& # (But it doesn\*(Aqt even get called if the test is skipped.) |
| 362 |
\& thing($foo) eq thing($bar) |
| 363 |
\& } |
| 364 |
\& ); |
| 365 |
.Ve |
| 366 |
.Sp |
| 367 |
or even this, which is basically equivalent: |
| 368 |
.Sp |
| 369 |
.Vb 3 |
| 370 |
\& skip( $unless_MSWin, |
| 371 |
\& sub { thing($foo) }, sub { thing($bar) } |
| 372 |
\& ); |
| 373 |
.Ve |
| 374 |
.Sp |
| 375 |
That is, both are like this: |
| 376 |
.Sp |
| 377 |
.Vb 7 |
| 378 |
\& if( $unless_MSWin ) { |
| 379 |
\& ok(1); # but it actually appends "# $unless_MSWin" |
| 380 |
\& # so that Test::Harness can tell it\*(Aqs a skip |
| 381 |
\& } else { |
| 382 |
\& # Not skipping, so actually call and evaluate... |
| 383 |
\& ok( sub { thing($foo) }, sub { thing($bar) } ); |
| 384 |
\& } |
| 385 |
.Ve |
| 386 |
.SH "TEST TYPES" |
| 387 |
.IX Header "TEST TYPES" |
| 388 |
.IP \(bu 4 |
| 389 |
NORMAL TESTS |
| 390 |
.Sp |
| 391 |
These tests are expected to succeed. Usually, most or all of your tests |
| 392 |
are in this category. If a normal test doesn't succeed, then that |
| 393 |
means that something is \fIwrong\fR. |
| 394 |
.IP \(bu 4 |
| 395 |
SKIPPED TESTS |
| 396 |
.Sp |
| 397 |
The \f(CWskip(...)\fR function is for tests that might or might not be |
| 398 |
possible to run, depending |
| 399 |
on the availability of platform-specific features. The first argument |
| 400 |
should evaluate to true (think "yes, please skip") if the required |
| 401 |
feature is \fInot\fR available. After the first argument, \f(CWskip(...)\fR works |
| 402 |
exactly the same way as \f(CWok(...)\fR does. |
| 403 |
.IP \(bu 4 |
| 404 |
TODO TESTS |
| 405 |
.Sp |
| 406 |
TODO tests are designed for maintaining an \fBexecutable TODO list\fR. |
| 407 |
These tests are \fIexpected to fail.\fR If a TODO test does succeed, |
| 408 |
then the feature in question shouldn't be on the TODO list, now |
| 409 |
should it? |
| 410 |
.Sp |
| 411 |
Packages should NOT be released with succeeding TODO tests. As soon |
| 412 |
as a TODO test starts working, it should be promoted to a normal test, |
| 413 |
and the newly working feature should be documented in the release |
| 414 |
notes or in the change log. |
| 415 |
.SH ONFAIL |
| 416 |
.IX Header "ONFAIL" |
| 417 |
.Vb 1 |
| 418 |
\& BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } } |
| 419 |
.Ve |
| 420 |
.PP |
| 421 |
Although test failures should be enough, extra diagnostics can be |
| 422 |
triggered at the end of a test run. \f(CW\*(C`onfail\*(C'\fR is passed an array ref |
| 423 |
of hash refs that describe each test failure. Each hash will contain |
| 424 |
at least the following fields: \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`repetition\*(C'\fR, and |
| 425 |
\&\f(CW\*(C`result\*(C'\fR. (You shouldn't rely on any other fields being present.) If the test |
| 426 |
had an expected value or a diagnostic (or "note") string, these will also be |
| 427 |
included. |
| 428 |
.PP |
| 429 |
The \fIoptional\fR \f(CW\*(C`onfail\*(C'\fR hook might be used simply to print out the |
| 430 |
version of your package and/or how to report problems. It might also |
| 431 |
be used to generate extremely sophisticated diagnostics for a |
| 432 |
particularly bizarre test failure. However it's not a panacea. Core |
| 433 |
dumps or other unrecoverable errors prevent the \f(CW\*(C`onfail\*(C'\fR hook from |
| 434 |
running. (It is run inside an \f(CW\*(C`END\*(C'\fR block.) Besides, \f(CW\*(C`onfail\*(C'\fR is |
| 435 |
probably over-kill in most cases. (Your test code should be simpler |
| 436 |
than the code it is testing, yes?) |
| 437 |
.SH "BUGS and CAVEATS" |
| 438 |
.IX Header "BUGS and CAVEATS" |
| 439 |
.IP \(bu 4 |
| 440 |
\&\f(CWok(...)\fR's special handing of strings which look like they might be |
| 441 |
regexes can also cause unexpected behavior. An innocent: |
| 442 |
.Sp |
| 443 |
.Vb 1 |
| 444 |
\& ok( $fileglob, \*(Aq/path/to/some/*stuff/\*(Aq ); |
| 445 |
.Ve |
| 446 |
.Sp |
| 447 |
will fail, since Test.pm considers the second argument to be a regex! |
| 448 |
The best bet is to use the one-argument form: |
| 449 |
.Sp |
| 450 |
.Vb 1 |
| 451 |
\& ok( $fileglob eq \*(Aq/path/to/some/*stuff/\*(Aq ); |
| 452 |
.Ve |
| 453 |
.IP \(bu 4 |
| 454 |
\&\f(CWok(...)\fR's use of string \f(CW\*(C`eq\*(C'\fR can sometimes cause odd problems |
| 455 |
when comparing |
| 456 |
numbers, especially if you're casting a string to a number: |
| 457 |
.Sp |
| 458 |
.Vb 2 |
| 459 |
\& $foo = "1.0"; |
| 460 |
\& ok( $foo, 1 ); # not ok, "1.0" ne 1 |
| 461 |
.Ve |
| 462 |
.Sp |
| 463 |
Your best bet is to use the single argument form: |
| 464 |
.Sp |
| 465 |
.Vb 1 |
| 466 |
\& ok( $foo == 1 ); # ok "1.0" == 1 |
| 467 |
.Ve |
| 468 |
.IP \(bu 4 |
| 469 |
As you may have inferred from the above documentation and examples, |
| 470 |
\&\f(CW\*(C`ok\*(C'\fR's prototype is \f(CW\*(C`($;$$)\*(C'\fR (and, incidentally, \f(CW\*(C`skip\*(C'\fR's is |
| 471 |
\&\f(CW\*(C`($;$$$)\*(C'\fR). This means, for example, that you can do \f(CW\*(C`ok @foo, @bar\*(C'\fR |
| 472 |
to compare the \fIsize\fR of the two arrays. But don't be fooled into |
| 473 |
thinking that \f(CW\*(C`ok @foo, @bar\*(C'\fR means a comparison of the contents of two |
| 474 |
arrays \-\- you're comparing \fIjust\fR the number of elements of each. It's |
| 475 |
so easy to make that mistake in reading \f(CW\*(C`ok @foo, @bar\*(C'\fR that you might |
| 476 |
want to be very explicit about it, and instead write \f(CWok scalar(@foo), |
| 477 |
scalar(@bar)\fR. |
| 478 |
.IP \(bu 4 |
| 479 |
This almost definitely doesn't do what you expect: |
| 480 |
.Sp |
| 481 |
.Vb 1 |
| 482 |
\& ok $thingy\->can(\*(Aqsome_method\*(Aq); |
| 483 |
.Ve |
| 484 |
.Sp |
| 485 |
Why? Because \f(CW\*(C`can\*(C'\fR returns a coderef to mean "yes it can (and the |
| 486 |
method is this...)", and then \f(CW\*(C`ok\*(C'\fR sees a coderef and thinks you're |
| 487 |
passing a function that you want it to call and consider the truth of |
| 488 |
the result of! I.e., just like: |
| 489 |
.Sp |
| 490 |
.Vb 1 |
| 491 |
\& ok $thingy\->can(\*(Aqsome_method\*(Aq)\->(); |
| 492 |
.Ve |
| 493 |
.Sp |
| 494 |
What you probably want instead is this: |
| 495 |
.Sp |
| 496 |
.Vb 1 |
| 497 |
\& ok $thingy\->can(\*(Aqsome_method\*(Aq) && 1; |
| 498 |
.Ve |
| 499 |
.Sp |
| 500 |
If the \f(CW\*(C`can\*(C'\fR returns false, then that is passed to \f(CW\*(C`ok\*(C'\fR. If it |
| 501 |
returns true, then the larger expression \f(CW\*(C`$thingy\->can(\*(Aqsome_method\*(Aq)\ &&\ 1\*(C'\fR returns 1, which \f(CW\*(C`ok\*(C'\fR sees as |
| 502 |
a simple signal of success, as you would expect. |
| 503 |
.IP \(bu 4 |
| 504 |
The syntax for \f(CW\*(C`skip\*(C'\fR is about the only way it can be, but it's still |
| 505 |
quite confusing. Just start with the above examples and you'll |
| 506 |
be okay. |
| 507 |
.Sp |
| 508 |
Moreover, users may expect this: |
| 509 |
.Sp |
| 510 |
.Vb 1 |
| 511 |
\& skip $unless_mswin, foo($bar), baz($quux); |
| 512 |
.Ve |
| 513 |
.Sp |
| 514 |
to not evaluate \f(CWfoo($bar)\fR and \f(CWbaz($quux)\fR when the test is being |
| 515 |
skipped. But in reality, they \fIare\fR evaluated, but \f(CW\*(C`skip\*(C'\fR just won't |
| 516 |
bother comparing them if \f(CW$unless_mswin\fR is true. |
| 517 |
.Sp |
| 518 |
You could do this: |
| 519 |
.Sp |
| 520 |
.Vb 1 |
| 521 |
\& skip $unless_mswin, sub{foo($bar)}, sub{baz($quux)}; |
| 522 |
.Ve |
| 523 |
.Sp |
| 524 |
But that's not terribly pretty. You may find it simpler or clearer in |
| 525 |
the long run to just do things like this: |
| 526 |
.Sp |
| 527 |
.Vb 10 |
| 528 |
\& if( $^O =~ m/MSWin/ ) { |
| 529 |
\& print "# Yay, we\*(Aqre under $^O\en"; |
| 530 |
\& ok foo($bar), baz($quux); |
| 531 |
\& ok thing($whatever), baz($stuff); |
| 532 |
\& ok blorp($quux, $whatever); |
| 533 |
\& ok foo($barzbarz), thang($quux); |
| 534 |
\& } else { |
| 535 |
\& print "# Feh, we\*(Aqre under $^O. Watch me skip some tests...\en"; |
| 536 |
\& for(1 .. 4) { skip "Skip unless under MSWin" } |
| 537 |
\& } |
| 538 |
.Ve |
| 539 |
.Sp |
| 540 |
But be quite sure that \f(CW\*(C`ok\*(C'\fR is called exactly as many times in the |
| 541 |
first block as \f(CW\*(C`skip\*(C'\fR is called in the second block. |
| 542 |
.SH ENVIRONMENT |
| 543 |
.IX Header "ENVIRONMENT" |
| 544 |
If \f(CW\*(C`PERL_TEST_DIFF\*(C'\fR environment variable is set, it will be used as a |
| 545 |
command for comparing unexpected multiline results. If you have GNU |
| 546 |
diff installed, you might want to set \f(CW\*(C`PERL_TEST_DIFF\*(C'\fR to \f(CW\*(C`diff \-u\*(C'\fR. |
| 547 |
If you don't have a suitable program, you might install the |
| 548 |
\&\f(CW\*(C`Text::Diff\*(C'\fR module and then set \f(CW\*(C`PERL_TEST_DIFF\*(C'\fR to be \f(CW\*(C`perl |
| 549 |
\&\-MText::Diff \-e \*(Aqprint diff(@ARGV)\*(Aq\*(C'\fR. If \f(CW\*(C`PERL_TEST_DIFF\*(C'\fR isn't set |
| 550 |
but the \f(CW\*(C`Algorithm::Diff\*(C'\fR module is available, then it will be used |
| 551 |
to show the differences in multiline results. |
| 552 |
.SH NOTE |
| 553 |
.IX Header "NOTE" |
| 554 |
A past developer of this module once said that it was no longer being |
| 555 |
actively developed. However, rumors of its demise were greatly |
| 556 |
exaggerated. Feedback and suggestions are quite welcome. |
| 557 |
.PP |
| 558 |
Be aware that the main value of this module is its simplicity. Note |
| 559 |
that there are already more ambitious modules out there, such as |
| 560 |
Test::More and Test::Unit. |
| 561 |
.PP |
| 562 |
Some earlier versions of this module had docs with some confusing |
| 563 |
typos in the description of \f(CWskip(...)\fR. |
| 564 |
.SH "SEE ALSO" |
| 565 |
.IX Header "SEE ALSO" |
| 566 |
Test::Harness |
| 567 |
.PP |
| 568 |
Test::Simple, Test::More, Devel::Cover |
| 569 |
.PP |
| 570 |
Test::Builder for building your own testing library. |
| 571 |
.PP |
| 572 |
Test::Unit is an interesting XUnit-style testing library. |
| 573 |
.PP |
| 574 |
Test::Inline lets you embed tests in code. |
| 575 |
.SH AUTHOR |
| 576 |
.IX Header "AUTHOR" |
| 577 |
Copyright (c) 1998\-2000 Joshua Nathaniel Pritikin. |
| 578 |
.PP |
| 579 |
Copyright (c) 2001\-2002 Michael G. Schwern. |
| 580 |
.PP |
| 581 |
Copyright (c) 2002\-2004 Sean M. Burke. |
| 582 |
.PP |
| 583 |
Current maintainer: Jesse Vincent. <[email protected]> |
| 584 |
.PP |
| 585 |
This package is free software and is provided "as is" without express |
| 586 |
or implied warranty. It may be used, redistributed and/or modified |
| 587 |
under the same terms as Perl itself. |