| 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::More 3perl" |
| 58 |
.TH Test::More 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::More \- yet another framework for writing test scripts |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 5 |
| 68 |
\& use Test::More tests => 23; |
| 69 |
\& # or |
| 70 |
\& use Test::More skip_all => $reason; |
| 71 |
\& # or |
| 72 |
\& use Test::More; # see done_testing() |
| 73 |
\& |
| 74 |
\& require_ok( \*(AqSome::Module\*(Aq ); |
| 75 |
\& |
| 76 |
\& # Various ways to say "ok" |
| 77 |
\& ok($got eq $expected, $test_name); |
| 78 |
\& |
| 79 |
\& is ($got, $expected, $test_name); |
| 80 |
\& isnt($got, $expected, $test_name); |
| 81 |
\& |
| 82 |
\& # Rather than print STDERR "# here\*(Aqs what went wrong\en" |
| 83 |
\& diag("here\*(Aqs what went wrong"); |
| 84 |
\& |
| 85 |
\& like ($got, qr/expected/, $test_name); |
| 86 |
\& unlike($got, qr/expected/, $test_name); |
| 87 |
\& |
| 88 |
\& cmp_ok($got, \*(Aq==\*(Aq, $expected, $test_name); |
| 89 |
\& |
| 90 |
\& is_deeply($got_complex_structure, $expected_complex_structure, $test_name); |
| 91 |
\& |
| 92 |
\& SKIP: { |
| 93 |
\& skip $why, $how_many unless $have_some_feature; |
| 94 |
\& |
| 95 |
\& ok( foo(), $test_name ); |
| 96 |
\& is( foo(42), 23, $test_name ); |
| 97 |
\& }; |
| 98 |
\& |
| 99 |
\& TODO: { |
| 100 |
\& local $TODO = $why; |
| 101 |
\& |
| 102 |
\& ok( foo(), $test_name ); |
| 103 |
\& is( foo(42), 23, $test_name ); |
| 104 |
\& }; |
| 105 |
\& |
| 106 |
\& can_ok($module, @methods); |
| 107 |
\& isa_ok($object, $class); |
| 108 |
\& |
| 109 |
\& pass($test_name); |
| 110 |
\& fail($test_name); |
| 111 |
\& |
| 112 |
\& BAIL_OUT($why); |
| 113 |
\& |
| 114 |
\& # UNIMPLEMENTED!!! |
| 115 |
\& my @status = Test::More::status; |
| 116 |
.Ve |
| 117 |
.SH DESCRIPTION |
| 118 |
.IX Header "DESCRIPTION" |
| 119 |
\&\fBSTOP!\fR If you're just getting started writing tests, have a look at |
| 120 |
Test2::Suite first. |
| 121 |
.PP |
| 122 |
This is a drop in replacement for Test::Simple which you can switch to once you |
| 123 |
get the hang of basic testing. |
| 124 |
.PP |
| 125 |
The purpose of this module is to provide a wide range of testing |
| 126 |
utilities. Various ways to say "ok" with better diagnostics, |
| 127 |
facilities to skip tests, test future features and compare complicated |
| 128 |
data structures. While you can do almost anything with a simple |
| 129 |
\&\f(CWok()\fR function, it doesn't provide good diagnostic output. |
| 130 |
.SS "I love it when a plan comes together" |
| 131 |
.IX Subsection "I love it when a plan comes together" |
| 132 |
Before anything else, you need a testing plan. This basically declares |
| 133 |
how many tests your script is going to run to protect against premature |
| 134 |
failure. |
| 135 |
.PP |
| 136 |
The preferred way to do this is to declare a plan when you \f(CW\*(C`use Test::More\*(C'\fR. |
| 137 |
.PP |
| 138 |
.Vb 1 |
| 139 |
\& use Test::More tests => 23; |
| 140 |
.Ve |
| 141 |
.PP |
| 142 |
There are cases when you will not know beforehand how many tests your |
| 143 |
script is going to run. In this case, you can declare your tests at |
| 144 |
the end. |
| 145 |
.PP |
| 146 |
.Vb 1 |
| 147 |
\& use Test::More; |
| 148 |
\& |
| 149 |
\& ... run your tests ... |
| 150 |
\& |
| 151 |
\& done_testing( $number_of_tests_run ); |
| 152 |
.Ve |
| 153 |
.PP |
| 154 |
\&\fBNOTE\fR \f(CWdone_testing()\fR should never be called in an \f(CW\*(C`END { ... }\*(C'\fR block. |
| 155 |
.PP |
| 156 |
Sometimes you really don't know how many tests were run, or it's too |
| 157 |
difficult to calculate. In which case you can leave off |
| 158 |
\&\f(CW$number_of_tests_run\fR. |
| 159 |
.PP |
| 160 |
In some cases, you'll want to completely skip an entire testing script. |
| 161 |
.PP |
| 162 |
.Vb 1 |
| 163 |
\& use Test::More skip_all => $skip_reason; |
| 164 |
.Ve |
| 165 |
.PP |
| 166 |
Your script will declare a skip with the reason why you skipped and |
| 167 |
exit immediately with a zero (success). See Test::Harness for |
| 168 |
details. |
| 169 |
.PP |
| 170 |
If you want to control what functions Test::More will export, you |
| 171 |
have to use the 'import' option. For example, to import everything |
| 172 |
but 'fail', you'd do: |
| 173 |
.PP |
| 174 |
.Vb 1 |
| 175 |
\& use Test::More tests => 23, import => [\*(Aq!fail\*(Aq]; |
| 176 |
.Ve |
| 177 |
.PP |
| 178 |
Alternatively, you can use the \f(CWplan()\fR function. Useful for when you |
| 179 |
have to calculate the number of tests. |
| 180 |
.PP |
| 181 |
.Vb 2 |
| 182 |
\& use Test::More; |
| 183 |
\& plan tests => keys %Stuff * 3; |
| 184 |
.Ve |
| 185 |
.PP |
| 186 |
or for deciding between running the tests at all: |
| 187 |
.PP |
| 188 |
.Vb 7 |
| 189 |
\& use Test::More; |
| 190 |
\& if( $^O eq \*(AqMacOS\*(Aq ) { |
| 191 |
\& plan skip_all => \*(AqTest irrelevant on MacOS\*(Aq; |
| 192 |
\& } |
| 193 |
\& else { |
| 194 |
\& plan tests => 42; |
| 195 |
\& } |
| 196 |
.Ve |
| 197 |
.IP \fBdone_testing\fR 4 |
| 198 |
.IX Item "done_testing" |
| 199 |
.Vb 2 |
| 200 |
\& done_testing(); |
| 201 |
\& done_testing($number_of_tests); |
| 202 |
.Ve |
| 203 |
.Sp |
| 204 |
If you don't know how many tests you're going to run, you can issue |
| 205 |
the plan when you're done running tests. |
| 206 |
.Sp |
| 207 |
\&\f(CW$number_of_tests\fR is the same as \f(CWplan()\fR, it's the number of tests you |
| 208 |
expected to run. You can omit this, in which case the number of tests |
| 209 |
you ran doesn't matter, just the fact that your tests ran to |
| 210 |
conclusion. |
| 211 |
.Sp |
| 212 |
This is safer than and replaces the "no_plan" plan. |
| 213 |
.Sp |
| 214 |
\&\fBNote:\fR You must never put \f(CWdone_testing()\fR inside an \f(CW\*(C`END { ... }\*(C'\fR block. |
| 215 |
The plan is there to ensure your test does not exit before testing has |
| 216 |
completed. If you use an END block you completely bypass this protection. |
| 217 |
.SS "Test names" |
| 218 |
.IX Subsection "Test names" |
| 219 |
By convention, each test is assigned a number in order. This is |
| 220 |
largely done automatically for you. However, it's often very useful to |
| 221 |
assign a name to each test. Which would you rather see: |
| 222 |
.PP |
| 223 |
.Vb 3 |
| 224 |
\& ok 4 |
| 225 |
\& not ok 5 |
| 226 |
\& ok 6 |
| 227 |
.Ve |
| 228 |
.PP |
| 229 |
or |
| 230 |
.PP |
| 231 |
.Vb 3 |
| 232 |
\& ok 4 \- basic multi\-variable |
| 233 |
\& not ok 5 \- simple exponential |
| 234 |
\& ok 6 \- force == mass * acceleration |
| 235 |
.Ve |
| 236 |
.PP |
| 237 |
The later gives you some idea of what failed. It also makes it easier |
| 238 |
to find the test in your script, simply search for "simple |
| 239 |
exponential". |
| 240 |
.PP |
| 241 |
All test functions take a name argument. It's optional, but highly |
| 242 |
suggested that you use it. |
| 243 |
.SS "I'm ok, you're not ok." |
| 244 |
.IX Subsection "I'm ok, you're not ok." |
| 245 |
The basic purpose of this module is to print out either "ok #" or "not |
| 246 |
ok #" depending on if a given test succeeded or failed. Everything |
| 247 |
else is just gravy. |
| 248 |
.PP |
| 249 |
All of the following print "ok" or "not ok" depending on if the test |
| 250 |
succeeded or failed. They all also return true or false, |
| 251 |
respectively. |
| 252 |
.IP \fBok\fR 4 |
| 253 |
.IX Item "ok" |
| 254 |
.Vb 1 |
| 255 |
\& ok($got eq $expected, $test_name); |
| 256 |
.Ve |
| 257 |
.Sp |
| 258 |
This simply evaluates any expression (\f(CW\*(C`$got eq $expected\*(C'\fR is just a |
| 259 |
simple example) and uses that to determine if the test succeeded or |
| 260 |
failed. A true expression passes, a false one fails. Very simple. |
| 261 |
.Sp |
| 262 |
For example: |
| 263 |
.Sp |
| 264 |
.Vb 4 |
| 265 |
\& ok( $exp{9} == 81, \*(Aqsimple exponential\*(Aq ); |
| 266 |
\& ok( Film\->can(\*(Aqdb_Main\*(Aq), \*(Aqset_db()\*(Aq ); |
| 267 |
\& ok( $p\->tests == 4, \*(Aqsaw tests\*(Aq ); |
| 268 |
\& ok( !grep(!defined $_, @items), \*(Aqall items defined\*(Aq ); |
| 269 |
.Ve |
| 270 |
.Sp |
| 271 |
(Mnemonic: "This is ok.") |
| 272 |
.Sp |
| 273 |
\&\f(CW$test_name\fR is a very short description of the test that will be printed |
| 274 |
out. It makes it very easy to find a test in your script when it fails |
| 275 |
and gives others an idea of your intentions. \f(CW$test_name\fR is optional, |
| 276 |
but we \fBvery\fR strongly encourage its use. |
| 277 |
.Sp |
| 278 |
Should an \f(CWok()\fR fail, it will produce some diagnostics: |
| 279 |
.Sp |
| 280 |
.Vb 3 |
| 281 |
\& not ok 18 \- sufficient mucus |
| 282 |
\& # Failed test \*(Aqsufficient mucus\*(Aq |
| 283 |
\& # in foo.t at line 42. |
| 284 |
.Ve |
| 285 |
.Sp |
| 286 |
This is the same as Test::Simple's \f(CWok()\fR routine. |
| 287 |
.IP \fBis\fR 4 |
| 288 |
.IX Item "is" |
| 289 |
.PD 0 |
| 290 |
.IP \fBisnt\fR 4 |
| 291 |
.IX Item "isnt" |
| 292 |
.PD |
| 293 |
.Vb 2 |
| 294 |
\& is ( $got, $expected, $test_name ); |
| 295 |
\& isnt( $got, $expected, $test_name ); |
| 296 |
.Ve |
| 297 |
.Sp |
| 298 |
Similar to \f(CWok()\fR, \f(CWis()\fR and \f(CWisnt()\fR compare their two arguments |
| 299 |
with \f(CW\*(C`eq\*(C'\fR and \f(CW\*(C`ne\*(C'\fR respectively and use the result of that to |
| 300 |
determine if the test succeeded or failed. So these: |
| 301 |
.Sp |
| 302 |
.Vb 2 |
| 303 |
\& # Is the ultimate answer 42? |
| 304 |
\& is( ultimate_answer(), 42, "Meaning of Life" ); |
| 305 |
\& |
| 306 |
\& # $foo isn\*(Aqt empty |
| 307 |
\& isnt( $foo, \*(Aq\*(Aq, "Got some foo" ); |
| 308 |
.Ve |
| 309 |
.Sp |
| 310 |
are similar to these: |
| 311 |
.Sp |
| 312 |
.Vb 2 |
| 313 |
\& ok( ultimate_answer() eq 42, "Meaning of Life" ); |
| 314 |
\& ok( $foo ne \*(Aq\*(Aq, "Got some foo" ); |
| 315 |
.Ve |
| 316 |
.Sp |
| 317 |
\&\f(CW\*(C`undef\*(C'\fR will only ever match \f(CW\*(C`undef\*(C'\fR. So you can test a value |
| 318 |
against \f(CW\*(C`undef\*(C'\fR like this: |
| 319 |
.Sp |
| 320 |
.Vb 1 |
| 321 |
\& is($not_defined, undef, "undefined as expected"); |
| 322 |
.Ve |
| 323 |
.Sp |
| 324 |
(Mnemonic: "This is that." "This isn't that.") |
| 325 |
.Sp |
| 326 |
So why use these? They produce better diagnostics on failure. \f(CWok()\fR |
| 327 |
cannot know what you are testing for (beyond the name), but \f(CWis()\fR and |
| 328 |
\&\f(CWisnt()\fR know what the test was and why it failed. For example this |
| 329 |
test: |
| 330 |
.Sp |
| 331 |
.Vb 2 |
| 332 |
\& my $foo = \*(Aqwaffle\*(Aq; my $bar = \*(Aqyarblokos\*(Aq; |
| 333 |
\& is( $foo, $bar, \*(AqIs foo the same as bar?\*(Aq ); |
| 334 |
.Ve |
| 335 |
.Sp |
| 336 |
Will produce something like this: |
| 337 |
.Sp |
| 338 |
.Vb 5 |
| 339 |
\& not ok 17 \- Is foo the same as bar? |
| 340 |
\& # Failed test \*(AqIs foo the same as bar?\*(Aq |
| 341 |
\& # in foo.t at line 139. |
| 342 |
\& # got: \*(Aqwaffle\*(Aq |
| 343 |
\& # expected: \*(Aqyarblokos\*(Aq |
| 344 |
.Ve |
| 345 |
.Sp |
| 346 |
So you can figure out what went wrong without rerunning the test. |
| 347 |
.Sp |
| 348 |
You are encouraged to use \f(CWis()\fR and \f(CWisnt()\fR over \f(CWok()\fR where possible, |
| 349 |
however do not be tempted to use them to find out if something is |
| 350 |
true or false! |
| 351 |
.Sp |
| 352 |
.Vb 2 |
| 353 |
\& # XXX BAD! |
| 354 |
\& is( exists $brooklyn{tree}, 1, \*(AqA tree grows in Brooklyn\*(Aq ); |
| 355 |
.Ve |
| 356 |
.Sp |
| 357 |
This does not check if \f(CW\*(C`exists $brooklyn{tree}\*(C'\fR is true, it checks if |
| 358 |
it returns 1. Very different. Similar caveats exist for false and 0. |
| 359 |
In these cases, use \f(CWok()\fR. |
| 360 |
.Sp |
| 361 |
.Vb 1 |
| 362 |
\& ok( exists $brooklyn{tree}, \*(AqA tree grows in Brooklyn\*(Aq ); |
| 363 |
.Ve |
| 364 |
.Sp |
| 365 |
A simple call to \f(CWisnt()\fR usually does not provide a strong test but there |
| 366 |
are cases when you cannot say much more about a value than that it is |
| 367 |
different from some other value: |
| 368 |
.Sp |
| 369 |
.Vb 1 |
| 370 |
\& new_ok $obj, "Foo"; |
| 371 |
\& |
| 372 |
\& my $clone = $obj\->clone; |
| 373 |
\& isa_ok $obj, "Foo", "Foo\->clone"; |
| 374 |
\& |
| 375 |
\& isnt $obj, $clone, "clone() produces a different object"; |
| 376 |
.Ve |
| 377 |
.Sp |
| 378 |
Historically we supported an \f(CW\*(C`isn\*(Aqt()\*(C'\fR function as an alias of |
| 379 |
\&\f(CWisnt()\fR, however in Perl 5.37.9 support for the use of aprostrophe as |
| 380 |
a package separator was deprecated and by Perl 5.42.0 support for it |
| 381 |
will have been removed completely. Accordingly use of \f(CW\*(C`isn\*(Aqt()\*(C'\fR is also |
| 382 |
deprecated, and will produce warnings when used unless 'deprecated' |
| 383 |
warnings are specifically disabled in the scope where it is used. You |
| 384 |
are strongly advised to migrate to using \f(CWisnt()\fR instead. |
| 385 |
.IP \fBlike\fR 4 |
| 386 |
.IX Item "like" |
| 387 |
.Vb 1 |
| 388 |
\& like( $got, qr/expected/, $test_name ); |
| 389 |
.Ve |
| 390 |
.Sp |
| 391 |
Similar to \f(CWok()\fR, \f(CWlike()\fR matches \f(CW$got\fR against the regex \f(CW\*(C`qr/expected/\*(C'\fR. |
| 392 |
.Sp |
| 393 |
So this: |
| 394 |
.Sp |
| 395 |
.Vb 1 |
| 396 |
\& like($got, qr/expected/, \*(Aqthis is like that\*(Aq); |
| 397 |
.Ve |
| 398 |
.Sp |
| 399 |
is similar to: |
| 400 |
.Sp |
| 401 |
.Vb 1 |
| 402 |
\& ok( $got =~ m/expected/, \*(Aqthis is like that\*(Aq); |
| 403 |
.Ve |
| 404 |
.Sp |
| 405 |
(Mnemonic "This is like that".) |
| 406 |
.Sp |
| 407 |
The second argument is a regular expression. It may be given as a |
| 408 |
regex reference (i.e. \f(CW\*(C`qr//\*(C'\fR) or (for better compatibility with older |
| 409 |
perls) as a string that looks like a regex (alternative delimiters are |
| 410 |
currently not supported): |
| 411 |
.Sp |
| 412 |
.Vb 1 |
| 413 |
\& like( $got, \*(Aq/expected/\*(Aq, \*(Aqthis is like that\*(Aq ); |
| 414 |
.Ve |
| 415 |
.Sp |
| 416 |
Regex options may be placed on the end (\f(CW\*(Aq/expected/i\*(Aq\fR). |
| 417 |
.Sp |
| 418 |
Its advantages over \f(CWok()\fR are similar to that of \f(CWis()\fR and \f(CWisnt()\fR. Better |
| 419 |
diagnostics on failure. |
| 420 |
.IP \fBunlike\fR 4 |
| 421 |
.IX Item "unlike" |
| 422 |
.Vb 1 |
| 423 |
\& unlike( $got, qr/expected/, $test_name ); |
| 424 |
.Ve |
| 425 |
.Sp |
| 426 |
Works exactly as \f(CWlike()\fR, only it checks if \f(CW$got\fR \fBdoes not\fR match the |
| 427 |
given pattern. |
| 428 |
.IP \fBcmp_ok\fR 4 |
| 429 |
.IX Item "cmp_ok" |
| 430 |
.Vb 1 |
| 431 |
\& cmp_ok( $got, $op, $expected, $test_name ); |
| 432 |
.Ve |
| 433 |
.Sp |
| 434 |
Halfway between \f(CWok()\fR and \f(CWis()\fR lies \f(CWcmp_ok()\fR. This allows you |
| 435 |
to compare two arguments using any binary perl operator. The test |
| 436 |
passes if the comparison is true and fails otherwise. |
| 437 |
.Sp |
| 438 |
.Vb 2 |
| 439 |
\& # ok( $got eq $expected ); |
| 440 |
\& cmp_ok( $got, \*(Aqeq\*(Aq, $expected, \*(Aqthis eq that\*(Aq ); |
| 441 |
\& |
| 442 |
\& # ok( $got == $expected ); |
| 443 |
\& cmp_ok( $got, \*(Aq==\*(Aq, $expected, \*(Aqthis == that\*(Aq ); |
| 444 |
\& |
| 445 |
\& # ok( $got && $expected ); |
| 446 |
\& cmp_ok( $got, \*(Aq&&\*(Aq, $expected, \*(Aqthis && that\*(Aq ); |
| 447 |
\& ...etc... |
| 448 |
.Ve |
| 449 |
.Sp |
| 450 |
Its advantage over \f(CWok()\fR is when the test fails you'll know what \f(CW$got\fR |
| 451 |
and \f(CW$expected\fR were: |
| 452 |
.Sp |
| 453 |
.Vb 5 |
| 454 |
\& not ok 1 |
| 455 |
\& # Failed test in foo.t at line 12. |
| 456 |
\& # \*(Aq23\*(Aq |
| 457 |
\& # && |
| 458 |
\& # undef |
| 459 |
.Ve |
| 460 |
.Sp |
| 461 |
It's also useful in those cases where you are comparing numbers and |
| 462 |
\&\f(CWis()\fR's use of \f(CW\*(C`eq\*(C'\fR will interfere: |
| 463 |
.Sp |
| 464 |
.Vb 1 |
| 465 |
\& cmp_ok( $big_hairy_number, \*(Aq==\*(Aq, $another_big_hairy_number ); |
| 466 |
.Ve |
| 467 |
.Sp |
| 468 |
It's especially useful when comparing greater-than or smaller-than |
| 469 |
relation between values: |
| 470 |
.Sp |
| 471 |
.Vb 1 |
| 472 |
\& cmp_ok( $some_value, \*(Aq<=\*(Aq, $upper_limit ); |
| 473 |
.Ve |
| 474 |
.IP \fBcan_ok\fR 4 |
| 475 |
.IX Item "can_ok" |
| 476 |
.Vb 2 |
| 477 |
\& can_ok($module, @methods); |
| 478 |
\& can_ok($object, @methods); |
| 479 |
.Ve |
| 480 |
.Sp |
| 481 |
Checks to make sure the \f(CW$module\fR or \f(CW$object\fR can do these \f(CW@methods\fR |
| 482 |
(works with functions, too). |
| 483 |
.Sp |
| 484 |
.Vb 1 |
| 485 |
\& can_ok(\*(AqFoo\*(Aq, qw(this that whatever)); |
| 486 |
.Ve |
| 487 |
.Sp |
| 488 |
is almost exactly like saying: |
| 489 |
.Sp |
| 490 |
.Vb 4 |
| 491 |
\& ok( Foo\->can(\*(Aqthis\*(Aq) && |
| 492 |
\& Foo\->can(\*(Aqthat\*(Aq) && |
| 493 |
\& Foo\->can(\*(Aqwhatever\*(Aq) |
| 494 |
\& ); |
| 495 |
.Ve |
| 496 |
.Sp |
| 497 |
only without all the typing and with a better interface. Handy for |
| 498 |
quickly testing an interface. |
| 499 |
.Sp |
| 500 |
No matter how many \f(CW@methods\fR you check, a single \f(CWcan_ok()\fR call counts |
| 501 |
as one test. If you desire otherwise, use: |
| 502 |
.Sp |
| 503 |
.Vb 3 |
| 504 |
\& foreach my $meth (@methods) { |
| 505 |
\& can_ok(\*(AqFoo\*(Aq, $meth); |
| 506 |
\& } |
| 507 |
.Ve |
| 508 |
.IP \fBisa_ok\fR 4 |
| 509 |
.IX Item "isa_ok" |
| 510 |
.Vb 3 |
| 511 |
\& isa_ok($object, $class, $object_name); |
| 512 |
\& isa_ok($subclass, $class, $object_name); |
| 513 |
\& isa_ok($ref, $type, $ref_name); |
| 514 |
.Ve |
| 515 |
.Sp |
| 516 |
Checks to see if the given \f(CW\*(C`$object\->isa($class)\*(C'\fR. Also checks to make |
| 517 |
sure the object was defined in the first place. Handy for this sort |
| 518 |
of thing: |
| 519 |
.Sp |
| 520 |
.Vb 2 |
| 521 |
\& my $obj = Some::Module\->new; |
| 522 |
\& isa_ok( $obj, \*(AqSome::Module\*(Aq ); |
| 523 |
.Ve |
| 524 |
.Sp |
| 525 |
where you'd otherwise have to write |
| 526 |
.Sp |
| 527 |
.Vb 2 |
| 528 |
\& my $obj = Some::Module\->new; |
| 529 |
\& ok( defined $obj && $obj\->isa(\*(AqSome::Module\*(Aq) ); |
| 530 |
.Ve |
| 531 |
.Sp |
| 532 |
to safeguard against your test script blowing up. |
| 533 |
.Sp |
| 534 |
You can also test a class, to make sure that it has the right ancestor: |
| 535 |
.Sp |
| 536 |
.Vb 1 |
| 537 |
\& isa_ok( \*(AqVole\*(Aq, \*(AqRodent\*(Aq ); |
| 538 |
.Ve |
| 539 |
.Sp |
| 540 |
It works on references, too: |
| 541 |
.Sp |
| 542 |
.Vb 1 |
| 543 |
\& isa_ok( $array_ref, \*(AqARRAY\*(Aq ); |
| 544 |
.Ve |
| 545 |
.Sp |
| 546 |
The diagnostics of this test normally just refer to 'the object'. If |
| 547 |
you'd like them to be more specific, you can supply an \f(CW$object_name\fR |
| 548 |
(for example 'Test customer'). |
| 549 |
.IP \fBnew_ok\fR 4 |
| 550 |
.IX Item "new_ok" |
| 551 |
.Vb 3 |
| 552 |
\& my $obj = new_ok( $class ); |
| 553 |
\& my $obj = new_ok( $class => \e@args ); |
| 554 |
\& my $obj = new_ok( $class => \e@args, $object_name ); |
| 555 |
.Ve |
| 556 |
.Sp |
| 557 |
A convenience function which combines creating an object and calling |
| 558 |
\&\f(CWisa_ok()\fR on that object. |
| 559 |
.Sp |
| 560 |
It is basically equivalent to: |
| 561 |
.Sp |
| 562 |
.Vb 2 |
| 563 |
\& my $obj = $class\->new(@args); |
| 564 |
\& isa_ok $obj, $class, $object_name; |
| 565 |
.Ve |
| 566 |
.Sp |
| 567 |
If \f(CW@args\fR is not given, an empty list will be used. |
| 568 |
.Sp |
| 569 |
This function only works on \f(CWnew()\fR and it assumes \f(CWnew()\fR will return |
| 570 |
just a single object which isa \f(CW$class\fR. |
| 571 |
.IP \fBsubtest\fR 4 |
| 572 |
.IX Item "subtest" |
| 573 |
.Vb 1 |
| 574 |
\& subtest $name => \e&code, @args; |
| 575 |
.Ve |
| 576 |
.Sp |
| 577 |
\&\f(CWsubtest()\fR runs the &code as its own little test with its own plan and |
| 578 |
its own result. The main test counts this as a single test using the |
| 579 |
result of the whole subtest to determine if its ok or not ok. |
| 580 |
.Sp |
| 581 |
For example... |
| 582 |
.Sp |
| 583 |
.Vb 1 |
| 584 |
\& use Test::More tests => 3; |
| 585 |
\& |
| 586 |
\& pass("First test"); |
| 587 |
\& |
| 588 |
\& subtest \*(AqAn example subtest\*(Aq => sub { |
| 589 |
\& plan tests => 2; |
| 590 |
\& |
| 591 |
\& pass("This is a subtest"); |
| 592 |
\& pass("So is this"); |
| 593 |
\& }; |
| 594 |
\& |
| 595 |
\& pass("Third test"); |
| 596 |
.Ve |
| 597 |
.Sp |
| 598 |
This would produce. |
| 599 |
.Sp |
| 600 |
.Vb 8 |
| 601 |
\& 1..3 |
| 602 |
\& ok 1 \- First test |
| 603 |
\& # Subtest: An example subtest |
| 604 |
\& 1..2 |
| 605 |
\& ok 1 \- This is a subtest |
| 606 |
\& ok 2 \- So is this |
| 607 |
\& ok 2 \- An example subtest |
| 608 |
\& ok 3 \- Third test |
| 609 |
.Ve |
| 610 |
.Sp |
| 611 |
A subtest may call \f(CW\*(C`skip_all\*(C'\fR. No tests will be run, but the subtest is |
| 612 |
considered a skip. |
| 613 |
.Sp |
| 614 |
.Vb 4 |
| 615 |
\& subtest \*(Aqskippy\*(Aq => sub { |
| 616 |
\& plan skip_all => \*(Aqcuz I said so\*(Aq; |
| 617 |
\& pass(\*(Aqthis test will never be run\*(Aq); |
| 618 |
\& }; |
| 619 |
.Ve |
| 620 |
.Sp |
| 621 |
Returns true if the subtest passed, false otherwise. |
| 622 |
.Sp |
| 623 |
Due to how subtests work, you may omit a plan if you desire. This adds an |
| 624 |
implicit \f(CWdone_testing()\fR to the end of your subtest. The following two |
| 625 |
subtests are equivalent: |
| 626 |
.Sp |
| 627 |
.Vb 5 |
| 628 |
\& subtest \*(Aqsubtest with implicit done_testing()\*(Aq, sub { |
| 629 |
\& ok 1, \*(Aqsubtests with an implicit done testing should work\*(Aq; |
| 630 |
\& ok 1, \*(Aq... and support more than one test\*(Aq; |
| 631 |
\& ok 1, \*(Aq... no matter how many tests are run\*(Aq; |
| 632 |
\& }; |
| 633 |
\& |
| 634 |
\& subtest \*(Aqsubtest with explicit done_testing()\*(Aq, sub { |
| 635 |
\& ok 1, \*(Aqsubtests with an explicit done testing should work\*(Aq; |
| 636 |
\& ok 1, \*(Aq... and support more than one test\*(Aq; |
| 637 |
\& ok 1, \*(Aq... no matter how many tests are run\*(Aq; |
| 638 |
\& done_testing(); |
| 639 |
\& }; |
| 640 |
.Ve |
| 641 |
.Sp |
| 642 |
Extra arguments given to \f(CW\*(C`subtest\*(C'\fR are passed to the callback. For example: |
| 643 |
.Sp |
| 644 |
.Vb 4 |
| 645 |
\& sub my_subtest { |
| 646 |
\& my $range = shift; |
| 647 |
\& ... |
| 648 |
\& } |
| 649 |
\& |
| 650 |
\& for my $range (1, 10, 100, 1000) { |
| 651 |
\& subtest "testing range $range", \e&my_subtest, $range; |
| 652 |
\& } |
| 653 |
.Ve |
| 654 |
.IP \fBpass\fR 4 |
| 655 |
.IX Item "pass" |
| 656 |
.PD 0 |
| 657 |
.IP \fBfail\fR 4 |
| 658 |
.IX Item "fail" |
| 659 |
.PD |
| 660 |
.Vb 2 |
| 661 |
\& pass($test_name); |
| 662 |
\& fail($test_name); |
| 663 |
.Ve |
| 664 |
.Sp |
| 665 |
Sometimes you just want to say that the tests have passed. Usually |
| 666 |
the case is you've got some complicated condition that is difficult to |
| 667 |
wedge into an \f(CWok()\fR. In this case, you can simply use \f(CWpass()\fR (to |
| 668 |
declare the test ok) or fail (for not ok). They are synonyms for |
| 669 |
\&\f(CWok(1)\fR and \f(CWok(0)\fR. |
| 670 |
.Sp |
| 671 |
Use these very, very, very sparingly. |
| 672 |
.SS "Module tests" |
| 673 |
.IX Subsection "Module tests" |
| 674 |
Sometimes you want to test if a module, or a list of modules, can |
| 675 |
successfully load. For example, you'll often want a first test which |
| 676 |
simply loads all the modules in the distribution to make sure they |
| 677 |
work before going on to do more complicated testing. |
| 678 |
.PP |
| 679 |
For such purposes we have \f(CW\*(C`use_ok\*(C'\fR and \f(CW\*(C`require_ok\*(C'\fR. |
| 680 |
.IP \fBrequire_ok\fR 4 |
| 681 |
.IX Item "require_ok" |
| 682 |
.Vb 2 |
| 683 |
\& require_ok($module); |
| 684 |
\& require_ok($file); |
| 685 |
.Ve |
| 686 |
.Sp |
| 687 |
Tries to \f(CW\*(C`require\*(C'\fR the given \f(CW$module\fR or \f(CW$file\fR. If it loads |
| 688 |
successfully, the test will pass. Otherwise it fails and displays the |
| 689 |
load error. |
| 690 |
.Sp |
| 691 |
\&\f(CW\*(C`require_ok\*(C'\fR will guess whether the input is a module name or a |
| 692 |
filename. |
| 693 |
.Sp |
| 694 |
No exception will be thrown if the load fails. |
| 695 |
.Sp |
| 696 |
.Vb 2 |
| 697 |
\& # require Some::Module |
| 698 |
\& require_ok "Some::Module"; |
| 699 |
\& |
| 700 |
\& # require "Some/File.pl"; |
| 701 |
\& require_ok "Some/File.pl"; |
| 702 |
\& |
| 703 |
\& # stop testing if any of your modules will not load |
| 704 |
\& for my $module (@module) { |
| 705 |
\& require_ok $module or BAIL_OUT "Can\*(Aqt load $module"; |
| 706 |
\& } |
| 707 |
.Ve |
| 708 |
.IP \fBuse_ok\fR 4 |
| 709 |
.IX Item "use_ok" |
| 710 |
.Vb 2 |
| 711 |
\& BEGIN { use_ok($module); } |
| 712 |
\& BEGIN { use_ok($module, @imports); } |
| 713 |
.Ve |
| 714 |
.Sp |
| 715 |
Like \f(CW\*(C`require_ok\*(C'\fR, but it will \f(CW\*(C`use\*(C'\fR the \f(CW$module\fR in question and |
| 716 |
only loads modules, not files. |
| 717 |
.Sp |
| 718 |
If you just want to test a module can be loaded, use \f(CW\*(C`require_ok\*(C'\fR. |
| 719 |
.Sp |
| 720 |
If you just want to load a module in a test, we recommend simply using |
| 721 |
\&\f(CW\*(C`use\*(C'\fR directly. It will cause the test to stop. |
| 722 |
.Sp |
| 723 |
It's recommended that you run \f(CWuse_ok()\fR inside a BEGIN block so its |
| 724 |
functions are exported at compile-time and prototypes are properly |
| 725 |
honored. |
| 726 |
.Sp |
| 727 |
If \f(CW@imports\fR are given, they are passed through to the use. So this: |
| 728 |
.Sp |
| 729 |
.Vb 1 |
| 730 |
\& BEGIN { use_ok(\*(AqSome::Module\*(Aq, qw(foo bar)) } |
| 731 |
.Ve |
| 732 |
.Sp |
| 733 |
is like doing this: |
| 734 |
.Sp |
| 735 |
.Vb 1 |
| 736 |
\& use Some::Module qw(foo bar); |
| 737 |
.Ve |
| 738 |
.Sp |
| 739 |
Version numbers can be checked like so: |
| 740 |
.Sp |
| 741 |
.Vb 2 |
| 742 |
\& # Just like "use Some::Module 1.02" |
| 743 |
\& BEGIN { use_ok(\*(AqSome::Module\*(Aq, 1.02) } |
| 744 |
.Ve |
| 745 |
.Sp |
| 746 |
Don't try to do this: |
| 747 |
.Sp |
| 748 |
.Vb 2 |
| 749 |
\& BEGIN { |
| 750 |
\& use_ok(\*(AqSome::Module\*(Aq); |
| 751 |
\& |
| 752 |
\& ...some code that depends on the use... |
| 753 |
\& ...happening at compile time... |
| 754 |
\& } |
| 755 |
.Ve |
| 756 |
.Sp |
| 757 |
because the notion of "compile-time" is relative. Instead, you want: |
| 758 |
.Sp |
| 759 |
.Vb 2 |
| 760 |
\& BEGIN { use_ok(\*(AqSome::Module\*(Aq) } |
| 761 |
\& BEGIN { ...some code that depends on the use... } |
| 762 |
.Ve |
| 763 |
.Sp |
| 764 |
If you want the equivalent of \f(CW\*(C`use Foo ()\*(C'\fR, use a module but not |
| 765 |
import anything, use \f(CW\*(C`require_ok\*(C'\fR. |
| 766 |
.Sp |
| 767 |
.Vb 1 |
| 768 |
\& BEGIN { require_ok "Foo" } |
| 769 |
.Ve |
| 770 |
.SS "Complex data structures" |
| 771 |
.IX Subsection "Complex data structures" |
| 772 |
Not everything is a simple eq check or regex. There are times you |
| 773 |
need to see if two data structures are equivalent. For these |
| 774 |
instances Test::More provides a handful of useful functions. |
| 775 |
.PP |
| 776 |
\&\fBNOTE\fR I'm not quite sure what will happen with filehandles. |
| 777 |
.IP \fBis_deeply\fR 4 |
| 778 |
.IX Item "is_deeply" |
| 779 |
.Vb 1 |
| 780 |
\& is_deeply( $got, $expected, $test_name ); |
| 781 |
.Ve |
| 782 |
.Sp |
| 783 |
Similar to \f(CWis()\fR, except that if \f(CW$got\fR and \f(CW$expected\fR are references, it |
| 784 |
does a deep comparison walking each data structure to see if they are |
| 785 |
equivalent. If the two structures are different, it will display the |
| 786 |
place where they start differing. |
| 787 |
.Sp |
| 788 |
\&\f(CWis_deeply()\fR compares the dereferenced values of references, the |
| 789 |
references themselves (except for their type) are ignored. This means |
| 790 |
aspects such as blessing and ties are not considered "different". |
| 791 |
.Sp |
| 792 |
\&\f(CWis_deeply()\fR currently has very limited handling of function reference |
| 793 |
and globs. It merely checks if they have the same referent. This may |
| 794 |
improve in the future. |
| 795 |
.Sp |
| 796 |
Test::Differences and Test::Deep provide more in-depth functionality |
| 797 |
along these lines. |
| 798 |
.Sp |
| 799 |
\&\fBNOTE\fR \fBis_deeply()\fR has limitations when it comes to comparing strings and |
| 800 |
refs: |
| 801 |
.Sp |
| 802 |
.Vb 4 |
| 803 |
\& my $path = path(\*(Aq.\*(Aq); |
| 804 |
\& my $hash = {}; |
| 805 |
\& is_deeply( $path, "$path" ); # ok |
| 806 |
\& is_deeply( $hash, "$hash" ); # fail |
| 807 |
.Ve |
| 808 |
.Sp |
| 809 |
This happens because is_deeply will unoverload all arguments unconditionally. |
| 810 |
It is probably best not to use is_deeply with overloading. For legacy reasons |
| 811 |
this is not likely to ever be fixed. If you would like a much better tool for |
| 812 |
this you should see Test2::Suite Specifically Test2::Tools::Compare has |
| 813 |
an \f(CWis()\fR function that works like \f(CW\*(C`is_deeply\*(C'\fR with many improvements. |
| 814 |
.SS Diagnostics |
| 815 |
.IX Subsection "Diagnostics" |
| 816 |
If you pick the right test function, you'll usually get a good idea of |
| 817 |
what went wrong when it failed. But sometimes it doesn't work out |
| 818 |
that way. So here we have ways for you to write your own diagnostic |
| 819 |
messages which are safer than just \f(CW\*(C`print STDERR\*(C'\fR. |
| 820 |
.IP \fBdiag\fR 4 |
| 821 |
.IX Item "diag" |
| 822 |
.Vb 1 |
| 823 |
\& diag(@diagnostic_message); |
| 824 |
.Ve |
| 825 |
.Sp |
| 826 |
Prints a diagnostic message which is guaranteed not to interfere with |
| 827 |
test output. Like \f(CW\*(C`print\*(C'\fR \f(CW@diagnostic_message\fR is simply concatenated |
| 828 |
together. |
| 829 |
.Sp |
| 830 |
Returns false, so as to preserve failure. |
| 831 |
.Sp |
| 832 |
Handy for this sort of thing: |
| 833 |
.Sp |
| 834 |
.Vb 2 |
| 835 |
\& ok( grep(/foo/, @users), "There\*(Aqs a foo user" ) or |
| 836 |
\& diag("Since there\*(Aqs no foo, check that /etc/bar is set up right"); |
| 837 |
.Ve |
| 838 |
.Sp |
| 839 |
which would produce: |
| 840 |
.Sp |
| 841 |
.Vb 4 |
| 842 |
\& not ok 42 \- There\*(Aqs a foo user |
| 843 |
\& # Failed test \*(AqThere\*(Aqs a foo user\*(Aq |
| 844 |
\& # in foo.t at line 52. |
| 845 |
\& # Since there\*(Aqs no foo, check that /etc/bar is set up right. |
| 846 |
.Ve |
| 847 |
.Sp |
| 848 |
You might remember \f(CW\*(C`ok() or diag()\*(C'\fR with the mnemonic \f(CWopen() or |
| 849 |
die()\fR. |
| 850 |
.Sp |
| 851 |
\&\fBNOTE\fR The exact formatting of the diagnostic output is still |
| 852 |
changing, but it is guaranteed that whatever you throw at it won't |
| 853 |
interfere with the test. |
| 854 |
.IP \fBnote\fR 4 |
| 855 |
.IX Item "note" |
| 856 |
.Vb 1 |
| 857 |
\& note(@diagnostic_message); |
| 858 |
.Ve |
| 859 |
.Sp |
| 860 |
Like \f(CWdiag()\fR, except the message will not be seen when the test is run |
| 861 |
in a harness. It will only be visible in the verbose TAP stream. |
| 862 |
.Sp |
| 863 |
Handy for putting in notes which might be useful for debugging, but |
| 864 |
don't indicate a problem. |
| 865 |
.Sp |
| 866 |
.Vb 1 |
| 867 |
\& note("Tempfile is $tempfile"); |
| 868 |
.Ve |
| 869 |
.IP \fBexplain\fR 4 |
| 870 |
.IX Item "explain" |
| 871 |
.Vb 1 |
| 872 |
\& my @dump = explain @diagnostic_message; |
| 873 |
.Ve |
| 874 |
.Sp |
| 875 |
Will dump the contents of any references in a human readable format. |
| 876 |
Usually you want to pass this into \f(CW\*(C`note\*(C'\fR or \f(CW\*(C`diag\*(C'\fR. |
| 877 |
.Sp |
| 878 |
Handy for things like... |
| 879 |
.Sp |
| 880 |
.Vb 1 |
| 881 |
\& is_deeply($have, $want) || diag explain $have; |
| 882 |
.Ve |
| 883 |
.Sp |
| 884 |
or |
| 885 |
.Sp |
| 886 |
.Vb 2 |
| 887 |
\& note explain \e%args; |
| 888 |
\& Some::Class\->method(%args); |
| 889 |
.Ve |
| 890 |
.SS "Conditional tests" |
| 891 |
.IX Subsection "Conditional tests" |
| 892 |
Sometimes running a test under certain conditions will cause the |
| 893 |
test script to die. A certain function or method isn't implemented |
| 894 |
(such as \f(CWfork()\fR on MacOS), some resource isn't available (like a |
| 895 |
net connection) or a module isn't available. In these cases it's |
| 896 |
necessary to skip tests, or declare that they are supposed to fail |
| 897 |
but will work in the future (a todo test). |
| 898 |
.PP |
| 899 |
For more details on the mechanics of skip and todo tests see |
| 900 |
Test::Harness. |
| 901 |
.PP |
| 902 |
The way Test::More handles this is with a named block. Basically, a |
| 903 |
block of tests which can be skipped over or made todo. It's best if I |
| 904 |
just show you... |
| 905 |
.IP "\fBSKIP: BLOCK\fR" 4 |
| 906 |
.IX Item "SKIP: BLOCK" |
| 907 |
.Vb 2 |
| 908 |
\& SKIP: { |
| 909 |
\& skip $why, $how_many if $condition; |
| 910 |
\& |
| 911 |
\& ...normal testing code goes here... |
| 912 |
\& } |
| 913 |
.Ve |
| 914 |
.Sp |
| 915 |
This declares a block of tests that might be skipped, \f(CW$how_many\fR tests |
| 916 |
there are, \f(CW$why\fR and under what \f(CW$condition\fR to skip them. An example is |
| 917 |
the easiest way to illustrate: |
| 918 |
.Sp |
| 919 |
.Vb 2 |
| 920 |
\& SKIP: { |
| 921 |
\& eval { require HTML::Lint }; |
| 922 |
\& |
| 923 |
\& skip "HTML::Lint not installed", 2 if $@; |
| 924 |
\& |
| 925 |
\& my $lint = new HTML::Lint; |
| 926 |
\& isa_ok( $lint, "HTML::Lint" ); |
| 927 |
\& |
| 928 |
\& $lint\->parse( $html ); |
| 929 |
\& is( $lint\->errors, 0, "No errors found in HTML" ); |
| 930 |
\& } |
| 931 |
.Ve |
| 932 |
.Sp |
| 933 |
If the user does not have HTML::Lint installed, the whole block of |
| 934 |
code \fIwon't be run at all\fR. Test::More will output special ok's |
| 935 |
which Test::Harness interprets as skipped, but passing, tests. |
| 936 |
.Sp |
| 937 |
It's important that \f(CW$how_many\fR accurately reflects the number of tests |
| 938 |
in the SKIP block so the # of tests run will match up with your plan. |
| 939 |
If your plan is \f(CW\*(C`no_plan\*(C'\fR \f(CW$how_many\fR is optional and will default to 1. |
| 940 |
.Sp |
| 941 |
It's perfectly safe to nest SKIP blocks. Each SKIP block must have |
| 942 |
the label \f(CW\*(C`SKIP\*(C'\fR, or Test::More can't work its magic. |
| 943 |
.Sp |
| 944 |
You don't skip tests which are failing because there's a bug in your |
| 945 |
program, or for which you don't yet have code written. For that you |
| 946 |
use TODO. Read on. |
| 947 |
.IP "\fBTODO: BLOCK\fR" 4 |
| 948 |
.IX Item "TODO: BLOCK" |
| 949 |
.Vb 2 |
| 950 |
\& TODO: { |
| 951 |
\& local $TODO = $why if $condition; |
| 952 |
\& |
| 953 |
\& ...normal testing code goes here... |
| 954 |
\& } |
| 955 |
.Ve |
| 956 |
.Sp |
| 957 |
Declares a block of tests you expect to fail and \f(CW$why\fR. Perhaps it's |
| 958 |
because you haven't fixed a bug or haven't finished a new feature: |
| 959 |
.Sp |
| 960 |
.Vb 2 |
| 961 |
\& TODO: { |
| 962 |
\& local $TODO = "URI::Geller not finished"; |
| 963 |
\& |
| 964 |
\& my $card = "Eight of clubs"; |
| 965 |
\& is( URI::Geller\->your_card, $card, \*(AqIs THIS your card?\*(Aq ); |
| 966 |
\& |
| 967 |
\& my $spoon; |
| 968 |
\& URI::Geller\->bend_spoon; |
| 969 |
\& is( $spoon, \*(Aqbent\*(Aq, "Spoon bending, that\*(Aqs original" ); |
| 970 |
\& } |
| 971 |
.Ve |
| 972 |
.Sp |
| 973 |
With a todo block, the tests inside are expected to fail. Test::More |
| 974 |
will run the tests normally, but print out special flags indicating |
| 975 |
they are "todo". Test::Harness will interpret failures as being ok. |
| 976 |
Should anything succeed, it will report it as an unexpected success. |
| 977 |
You then know the thing you had todo is done and can remove the |
| 978 |
TODO flag. |
| 979 |
.Sp |
| 980 |
The nice part about todo tests, as opposed to simply commenting out a |
| 981 |
block of tests, is that it is like having a programmatic todo list. You know |
| 982 |
how much work is left to be done, you're aware of what bugs there are, |
| 983 |
and you'll know immediately when they're fixed. |
| 984 |
.Sp |
| 985 |
Once a todo test starts succeeding, simply move it outside the block. |
| 986 |
When the block is empty, delete it. |
| 987 |
.Sp |
| 988 |
Note that, if you leave \f(CW$TODO\fR unset or undef, Test::More reports failures |
| 989 |
as normal. This can be useful to mark the tests as expected to fail only |
| 990 |
in certain conditions, e.g.: |
| 991 |
.Sp |
| 992 |
.Vb 2 |
| 993 |
\& TODO: { |
| 994 |
\& local $TODO = "$^O doesn\*(Aqt work yet. :(" if !_os_is_supported($^O); |
| 995 |
\& |
| 996 |
\& ... |
| 997 |
\& } |
| 998 |
.Ve |
| 999 |
.IP \fBtodo_skip\fR 4 |
| 1000 |
.IX Item "todo_skip" |
| 1001 |
.Vb 2 |
| 1002 |
\& TODO: { |
| 1003 |
\& todo_skip $why, $how_many if $condition; |
| 1004 |
\& |
| 1005 |
\& ...normal testing code... |
| 1006 |
\& } |
| 1007 |
.Ve |
| 1008 |
.Sp |
| 1009 |
With todo tests, it's best to have the tests actually run. That way |
| 1010 |
you'll know when they start passing. Sometimes this isn't possible. |
| 1011 |
Often a failing test will cause the whole program to die or hang, even |
| 1012 |
inside an \f(CW\*(C`eval BLOCK\*(C'\fR with and using \f(CW\*(C`alarm\*(C'\fR. In these extreme |
| 1013 |
cases you have no choice but to skip over the broken tests entirely. |
| 1014 |
.Sp |
| 1015 |
The syntax and behavior is similar to a \f(CW\*(C`SKIP: BLOCK\*(C'\fR except the |
| 1016 |
tests will be marked as failing but todo. Test::Harness will |
| 1017 |
interpret them as passing. |
| 1018 |
.IP "When do I use SKIP vs. TODO?" 4 |
| 1019 |
.IX Item "When do I use SKIP vs. TODO?" |
| 1020 |
\&\fBIf it's something the user might not be able to do\fR, use SKIP. |
| 1021 |
This includes optional modules that aren't installed, running under |
| 1022 |
an OS that doesn't have some feature (like \f(CWfork()\fR or symlinks), or maybe |
| 1023 |
you need an Internet connection and one isn't available. |
| 1024 |
.Sp |
| 1025 |
\&\fBIf it's something the programmer hasn't done yet\fR, use TODO. This |
| 1026 |
is for any code you haven't written yet, or bugs you have yet to fix, |
| 1027 |
but want to put tests in your testing script (always a good idea). |
| 1028 |
.SS "Test control" |
| 1029 |
.IX Subsection "Test control" |
| 1030 |
.IP \fBBAIL_OUT\fR 4 |
| 1031 |
.IX Item "BAIL_OUT" |
| 1032 |
.Vb 1 |
| 1033 |
\& BAIL_OUT($reason); |
| 1034 |
.Ve |
| 1035 |
.Sp |
| 1036 |
Indicates to the harness that things are going so badly all testing |
| 1037 |
should terminate. This includes the running of any additional test scripts. |
| 1038 |
.Sp |
| 1039 |
This is typically used when testing cannot continue such as a critical |
| 1040 |
module failing to compile or a necessary external utility not being |
| 1041 |
available such as a database connection failing. |
| 1042 |
.Sp |
| 1043 |
The test will exit with 255. |
| 1044 |
.Sp |
| 1045 |
For even better control look at Test::Most. |
| 1046 |
.SS "Discouraged comparison functions" |
| 1047 |
.IX Subsection "Discouraged comparison functions" |
| 1048 |
The use of the following functions is discouraged as they are not |
| 1049 |
actually testing functions and produce no diagnostics to help figure |
| 1050 |
out what went wrong. They were written before \f(CWis_deeply()\fR existed |
| 1051 |
because I couldn't figure out how to display a useful diff of two |
| 1052 |
arbitrary data structures. |
| 1053 |
.PP |
| 1054 |
These functions are usually used inside an \f(CWok()\fR. |
| 1055 |
.PP |
| 1056 |
.Vb 1 |
| 1057 |
\& ok( eq_array(\e@got, \e@expected) ); |
| 1058 |
.Ve |
| 1059 |
.PP |
| 1060 |
\&\f(CWis_deeply()\fR can do that better and with diagnostics. |
| 1061 |
.PP |
| 1062 |
.Vb 1 |
| 1063 |
\& is_deeply( \e@got, \e@expected ); |
| 1064 |
.Ve |
| 1065 |
.PP |
| 1066 |
They may be deprecated in future versions. |
| 1067 |
.IP \fBeq_array\fR 4 |
| 1068 |
.IX Item "eq_array" |
| 1069 |
.Vb 1 |
| 1070 |
\& my $is_eq = eq_array(\e@got, \e@expected); |
| 1071 |
.Ve |
| 1072 |
.Sp |
| 1073 |
Checks if two arrays are equivalent. This is a deep check, so |
| 1074 |
multi-level structures are handled correctly. |
| 1075 |
.IP \fBeq_hash\fR 4 |
| 1076 |
.IX Item "eq_hash" |
| 1077 |
.Vb 1 |
| 1078 |
\& my $is_eq = eq_hash(\e%got, \e%expected); |
| 1079 |
.Ve |
| 1080 |
.Sp |
| 1081 |
Determines if the two hashes contain the same keys and values. This |
| 1082 |
is a deep check. |
| 1083 |
.IP \fBeq_set\fR 4 |
| 1084 |
.IX Item "eq_set" |
| 1085 |
.Vb 1 |
| 1086 |
\& my $is_eq = eq_set(\e@got, \e@expected); |
| 1087 |
.Ve |
| 1088 |
.Sp |
| 1089 |
Similar to \f(CWeq_array()\fR, except the order of the elements is \fBnot\fR |
| 1090 |
important. This is a deep check, but the irrelevancy of order only |
| 1091 |
applies to the top level. |
| 1092 |
.Sp |
| 1093 |
.Vb 1 |
| 1094 |
\& ok( eq_set(\e@got, \e@expected) ); |
| 1095 |
.Ve |
| 1096 |
.Sp |
| 1097 |
Is better written: |
| 1098 |
.Sp |
| 1099 |
.Vb 1 |
| 1100 |
\& is_deeply( [sort @got], [sort @expected] ); |
| 1101 |
.Ve |
| 1102 |
.Sp |
| 1103 |
\&\fBNOTE\fR By historical accident, this is not a true set comparison. |
| 1104 |
While the order of elements does not matter, duplicate elements do. |
| 1105 |
.Sp |
| 1106 |
\&\fBNOTE\fR \f(CWeq_set()\fR does not know how to deal with references at the top |
| 1107 |
level. The following is an example of a comparison which might not work: |
| 1108 |
.Sp |
| 1109 |
.Vb 1 |
| 1110 |
\& eq_set([\e1, \e2], [\e2, \e1]); |
| 1111 |
.Ve |
| 1112 |
.Sp |
| 1113 |
Test::Deep contains much better set comparison functions. |
| 1114 |
.SS "Extending and Embedding Test::More" |
| 1115 |
.IX Subsection "Extending and Embedding Test::More" |
| 1116 |
Sometimes the Test::More interface isn't quite enough. Fortunately, |
| 1117 |
Test::More is built on top of Test::Builder which provides a single, |
| 1118 |
unified backend for any test library to use. This means two test |
| 1119 |
libraries which both use Test::Builder \fBcan\fR be used together in the |
| 1120 |
same program. |
| 1121 |
.PP |
| 1122 |
If you simply want to do a little tweaking of how the tests behave, |
| 1123 |
you can access the underlying Test::Builder object like so: |
| 1124 |
.IP \fBbuilder\fR 4 |
| 1125 |
.IX Item "builder" |
| 1126 |
.Vb 1 |
| 1127 |
\& my $test_builder = Test::More\->builder; |
| 1128 |
.Ve |
| 1129 |
.Sp |
| 1130 |
Returns the Test::Builder object underlying Test::More for you to play |
| 1131 |
with. |
| 1132 |
.SH "EXIT CODES" |
| 1133 |
.IX Header "EXIT CODES" |
| 1134 |
If all your tests passed, Test::Builder will exit with zero (which is |
| 1135 |
normal). If anything failed it will exit with how many failed. If |
| 1136 |
you run less (or more) tests than you planned, the missing (or extras) |
| 1137 |
will be considered failures. If no tests were ever run Test::Builder |
| 1138 |
will throw a warning and exit with 255. If the test died, even after |
| 1139 |
having successfully completed all its tests, it will still be |
| 1140 |
considered a failure and will exit with 255. |
| 1141 |
.PP |
| 1142 |
So the exit codes are... |
| 1143 |
.PP |
| 1144 |
.Vb 3 |
| 1145 |
\& 0 all tests successful |
| 1146 |
\& 255 test died or all passed but wrong # of tests run |
| 1147 |
\& any other number how many failed (including missing or extras) |
| 1148 |
.Ve |
| 1149 |
.PP |
| 1150 |
If you fail more than 254 tests, it will be reported as 254. |
| 1151 |
.PP |
| 1152 |
\&\fBNOTE\fR This behavior may go away in future versions. |
| 1153 |
.SH COMPATIBILITY |
| 1154 |
.IX Header "COMPATIBILITY" |
| 1155 |
Test::More works with Perls as old as 5.8.1. |
| 1156 |
.PP |
| 1157 |
Thread support is not very reliable before 5.10.1, but that's |
| 1158 |
because threads are not very reliable before 5.10.1. |
| 1159 |
.PP |
| 1160 |
Although Test::More has been a core module in versions of Perl since 5.6.2, Test::More has evolved since then, and not all of the features you're used to will be present in the shipped version of Test::More. If you are writing a module, don't forget to indicate in your package metadata the minimum version of Test::More that you require. For instance, if you want to use \f(CWdone_testing()\fR but want your test script to run on Perl 5.10.0, you will need to explicitly require Test::More > 0.88. |
| 1161 |
.PP |
| 1162 |
Key feature milestones include: |
| 1163 |
.IP subtests 4 |
| 1164 |
.IX Item "subtests" |
| 1165 |
Subtests were released in Test::More 0.94, which came with Perl 5.12.0. Subtests did not implicitly call \f(CWdone_testing()\fR until 0.96; the first Perl with that fix was Perl 5.14.0 with 0.98. |
| 1166 |
.ie n .IP done_testing() 4 |
| 1167 |
.el .IP \f(CWdone_testing()\fR 4 |
| 1168 |
.IX Item "done_testing()" |
| 1169 |
This was released in Test::More 0.88 and first shipped with Perl in 5.10.1 as part of Test::More 0.92. |
| 1170 |
.ie n .IP cmp_ok() 4 |
| 1171 |
.el .IP \f(CWcmp_ok()\fR 4 |
| 1172 |
.IX Item "cmp_ok()" |
| 1173 |
Although \f(CWcmp_ok()\fR was introduced in 0.40, 0.86 fixed an important bug to make it safe for overloaded objects; the fixed first shipped with Perl in 5.10.1 as part of Test::More 0.92. |
| 1174 |
.ie n .IP "new_ok() note() and explain()" 4 |
| 1175 |
.el .IP "\f(CWnew_ok()\fR \f(CWnote()\fR and \f(CWexplain()\fR" 4 |
| 1176 |
.IX Item "new_ok() note() and explain()" |
| 1177 |
These were was released in Test::More 0.82, and first shipped with Perl in 5.10.1 as part of Test::More 0.92. |
| 1178 |
.PP |
| 1179 |
There is a full version history in the Changes file, and the Test::More versions included as core can be found using Module::CoreList: |
| 1180 |
.PP |
| 1181 |
.Vb 1 |
| 1182 |
\& $ corelist \-a Test::More |
| 1183 |
.Ve |
| 1184 |
.SH "CAVEATS and NOTES" |
| 1185 |
.IX Header "CAVEATS and NOTES" |
| 1186 |
.IP "utf8 / ""Wide character in print""" 4 |
| 1187 |
.IX Item "utf8 / ""Wide character in print""" |
| 1188 |
If you use utf8 or other non-ASCII characters with Test::More you |
| 1189 |
might get a "Wide character in print" warning. Using |
| 1190 |
\&\f(CW\*(C`binmode STDOUT, ":utf8"\*(C'\fR will not fix it. |
| 1191 |
Test::Builder (which powers |
| 1192 |
Test::More) duplicates STDOUT and STDERR. So any changes to them, |
| 1193 |
including changing their output disciplines, will not be seen by |
| 1194 |
Test::More. |
| 1195 |
.Sp |
| 1196 |
One work around is to apply encodings to STDOUT and STDERR as early |
| 1197 |
as possible and before Test::More (or any other Test module) loads. |
| 1198 |
.Sp |
| 1199 |
.Vb 2 |
| 1200 |
\& use open \*(Aq:std\*(Aq, \*(Aq:encoding(utf8)\*(Aq; |
| 1201 |
\& use Test::More; |
| 1202 |
.Ve |
| 1203 |
.Sp |
| 1204 |
A more direct work around is to change the filehandles used by |
| 1205 |
Test::Builder. |
| 1206 |
.Sp |
| 1207 |
.Vb 4 |
| 1208 |
\& my $builder = Test::More\->builder; |
| 1209 |
\& binmode $builder\->output, ":encoding(utf8)"; |
| 1210 |
\& binmode $builder\->failure_output, ":encoding(utf8)"; |
| 1211 |
\& binmode $builder\->todo_output, ":encoding(utf8)"; |
| 1212 |
.Ve |
| 1213 |
.IP "Overloaded objects" 4 |
| 1214 |
.IX Item "Overloaded objects" |
| 1215 |
String overloaded objects are compared \fBas strings\fR (or in \f(CWcmp_ok()\fR's |
| 1216 |
case, strings or numbers as appropriate to the comparison op). This |
| 1217 |
prevents Test::More from piercing an object's interface allowing |
| 1218 |
better blackbox testing. So if a function starts returning overloaded |
| 1219 |
objects instead of bare strings your tests won't notice the |
| 1220 |
difference. This is good. |
| 1221 |
.Sp |
| 1222 |
However, it does mean that functions like \f(CWis_deeply()\fR cannot be used to |
| 1223 |
test the internals of string overloaded objects. In this case I would |
| 1224 |
suggest Test::Deep which contains more flexible testing functions for |
| 1225 |
complex data structures. |
| 1226 |
.IP Threads 4 |
| 1227 |
.IX Item "Threads" |
| 1228 |
Test::More will only be aware of threads if \f(CW\*(C`use threads\*(C'\fR has been done |
| 1229 |
\&\fIbefore\fR Test::More is loaded. This is ok: |
| 1230 |
.Sp |
| 1231 |
.Vb 2 |
| 1232 |
\& use threads; |
| 1233 |
\& use Test::More; |
| 1234 |
.Ve |
| 1235 |
.Sp |
| 1236 |
This may cause problems: |
| 1237 |
.Sp |
| 1238 |
.Vb 2 |
| 1239 |
\& use Test::More |
| 1240 |
\& use threads; |
| 1241 |
.Ve |
| 1242 |
.Sp |
| 1243 |
5.8.1 and above are supported. Anything below that has too many bugs. |
| 1244 |
.SH HISTORY |
| 1245 |
.IX Header "HISTORY" |
| 1246 |
This is a case of convergent evolution with Joshua Pritikin's Test |
| 1247 |
module. I was largely unaware of its existence when I'd first |
| 1248 |
written my own \f(CWok()\fR routines. This module exists because I can't |
| 1249 |
figure out how to easily wedge test names into Test's interface (along |
| 1250 |
with a few other problems). |
| 1251 |
.PP |
| 1252 |
The goal here is to have a testing utility that's simple to learn, |
| 1253 |
quick to use and difficult to trip yourself up with while still |
| 1254 |
providing more flexibility than the existing Test.pm. As such, the |
| 1255 |
names of the most common routines are kept tiny, special cases and |
| 1256 |
magic side-effects are kept to a minimum. WYSIWYG. |
| 1257 |
.SH "SEE ALSO" |
| 1258 |
.IX Header "SEE ALSO" |
| 1259 |
.SS "" |
| 1260 |
.IX Subsection "" |
| 1261 |
.SS ALTERNATIVES |
| 1262 |
.IX Subsection "ALTERNATIVES" |
| 1263 |
Test2::Suite is the most recent and modern set of tools for testing. |
| 1264 |
.PP |
| 1265 |
Test::Simple if all this confuses you and you just want to write |
| 1266 |
some tests. You can upgrade to Test::More later (it's forward |
| 1267 |
compatible). |
| 1268 |
.PP |
| 1269 |
Test::Legacy tests written with Test.pm, the original testing |
| 1270 |
module, do not play well with other testing libraries. Test::Legacy |
| 1271 |
emulates the Test.pm interface and does play well with others. |
| 1272 |
.SS "ADDITIONAL LIBRARIES" |
| 1273 |
.IX Subsection "ADDITIONAL LIBRARIES" |
| 1274 |
Test::Differences for more ways to test complex data structures. |
| 1275 |
And it plays well with Test::More. |
| 1276 |
.PP |
| 1277 |
Test::Class is like xUnit but more perlish. |
| 1278 |
.PP |
| 1279 |
Test::Deep gives you more powerful complex data structure testing. |
| 1280 |
.PP |
| 1281 |
Test::Inline shows the idea of embedded testing. |
| 1282 |
.PP |
| 1283 |
Mock::Quick The ultimate mocking library. Easily spawn objects defined on |
| 1284 |
the fly. Can also override, block, or reimplement packages as needed. |
| 1285 |
.PP |
| 1286 |
Test::FixtureBuilder Quickly define fixture data for unit tests. |
| 1287 |
.SS "OTHER COMPONENTS" |
| 1288 |
.IX Subsection "OTHER COMPONENTS" |
| 1289 |
Test::Harness is the test runner and output interpreter for Perl. |
| 1290 |
It's the thing that powers \f(CW\*(C`make test\*(C'\fR and where the \f(CW\*(C`prove\*(C'\fR utility |
| 1291 |
comes from. |
| 1292 |
.SS BUNDLES |
| 1293 |
.IX Subsection "BUNDLES" |
| 1294 |
Test::Most Most commonly needed test functions and features. |
| 1295 |
.SH AUTHORS |
| 1296 |
.IX Header "AUTHORS" |
| 1297 |
Michael G Schwern <[email protected]> with much inspiration |
| 1298 |
from Joshua Pritikin's Test module and lots of help from Barrie |
| 1299 |
Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and |
| 1300 |
the perl-qa gang. |
| 1301 |
.SH MAINTAINERS |
| 1302 |
.IX Header "MAINTAINERS" |
| 1303 |
.IP "Chad Granum <[email protected]>" 4 |
| 1304 |
.IX Item "Chad Granum <[email protected]>" |
| 1305 |
.SH BUGS |
| 1306 |
.IX Header "BUGS" |
| 1307 |
See <https://github.com/Test\-More/test\-more/issues> to report and view bugs. |
| 1308 |
.SH SOURCE |
| 1309 |
.IX Header "SOURCE" |
| 1310 |
The source code repository for Test::More can be found at |
| 1311 |
<https://github.com/Test\-More/test\-more/>. |
| 1312 |
.SH COPYRIGHT |
| 1313 |
.IX Header "COPYRIGHT" |
| 1314 |
Copyright 2001\-2008 by Michael G Schwern <[email protected]>. |
| 1315 |
.PP |
| 1316 |
This program is free software; you can redistribute it and/or |
| 1317 |
modify it under the same terms as Perl itself. |
| 1318 |
.PP |
| 1319 |
See <https://dev.perl.org/licenses/> |