Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "Test::More 3" | |
132 | .TH Test::More 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Test::More \- yet another framework for writing test scripts | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 5 | |
138 | \& use Test::More tests => $Num_Tests; | |
139 | \& # or | |
140 | \& use Test::More qw(no_plan); | |
141 | \& # or | |
142 | \& use Test::More skip_all => $reason; | |
143 | .Ve | |
144 | .PP | |
145 | .Vb 2 | |
146 | \& BEGIN { use_ok( 'Some::Module' ); } | |
147 | \& require_ok( 'Some::Module' ); | |
148 | .Ve | |
149 | .PP | |
150 | .Vb 2 | |
151 | \& # Various ways to say "ok" | |
152 | \& ok($this eq $that, $test_name); | |
153 | .Ve | |
154 | .PP | |
155 | .Vb 2 | |
156 | \& is ($this, $that, $test_name); | |
157 | \& isnt($this, $that, $test_name); | |
158 | .Ve | |
159 | .PP | |
160 | .Vb 2 | |
161 | \& # Rather than print STDERR "# here's what went wrong\en" | |
162 | \& diag("here's what went wrong"); | |
163 | .Ve | |
164 | .PP | |
165 | .Vb 2 | |
166 | \& like ($this, qr/that/, $test_name); | |
167 | \& unlike($this, qr/that/, $test_name); | |
168 | .Ve | |
169 | .PP | |
170 | .Vb 1 | |
171 | \& cmp_ok($this, '==', $that, $test_name); | |
172 | .Ve | |
173 | .PP | |
174 | .Vb 1 | |
175 | \& is_deeply($complex_structure1, $complex_structure2, $test_name); | |
176 | .Ve | |
177 | .PP | |
178 | .Vb 2 | |
179 | \& SKIP: { | |
180 | \& skip $why, $how_many unless $have_some_feature; | |
181 | .Ve | |
182 | .PP | |
183 | .Vb 3 | |
184 | \& ok( foo(), $test_name ); | |
185 | \& is( foo(42), 23, $test_name ); | |
186 | \& }; | |
187 | .Ve | |
188 | .PP | |
189 | .Vb 2 | |
190 | \& TODO: { | |
191 | \& local $TODO = $why; | |
192 | .Ve | |
193 | .PP | |
194 | .Vb 3 | |
195 | \& ok( foo(), $test_name ); | |
196 | \& is( foo(42), 23, $test_name ); | |
197 | \& }; | |
198 | .Ve | |
199 | .PP | |
200 | .Vb 2 | |
201 | \& can_ok($module, @methods); | |
202 | \& isa_ok($object, $class); | |
203 | .Ve | |
204 | .PP | |
205 | .Vb 2 | |
206 | \& pass($test_name); | |
207 | \& fail($test_name); | |
208 | .Ve | |
209 | .PP | |
210 | .Vb 4 | |
211 | \& # Utility comparison functions. | |
212 | \& eq_array(\e@this, \e@that); | |
213 | \& eq_hash(\e%this, \e%that); | |
214 | \& eq_set(\e@this, \e@that); | |
215 | .Ve | |
216 | .PP | |
217 | .Vb 2 | |
218 | \& # UNIMPLEMENTED!!! | |
219 | \& my @status = Test::More::status; | |
220 | .Ve | |
221 | .PP | |
222 | .Vb 2 | |
223 | \& # UNIMPLEMENTED!!! | |
224 | \& BAIL_OUT($why); | |
225 | .Ve | |
226 | .SH "DESCRIPTION" | |
227 | .IX Header "DESCRIPTION" | |
228 | \&\fB\s-1STOP\s0!\fR If you're just getting started writing tests, have a look at | |
229 | Test::Simple first. This is a drop in replacement for Test::Simple | |
230 | which you can switch to once you get the hang of basic testing. | |
231 | .PP | |
232 | The purpose of this module is to provide a wide range of testing | |
233 | utilities. Various ways to say \*(L"ok\*(R" with better diagnostics, | |
234 | facilities to skip tests, test future features and compare complicated | |
235 | data structures. While you can do almost anything with a simple | |
236 | \&\f(CW\*(C`ok()\*(C'\fR function, it doesn't provide good diagnostic output. | |
237 | .Sh "I love it when a plan comes together" | |
238 | .IX Subsection "I love it when a plan comes together" | |
239 | Before anything else, you need a testing plan. This basically declares | |
240 | how many tests your script is going to run to protect against premature | |
241 | failure. | |
242 | .PP | |
243 | The preferred way to do this is to declare a plan when you \f(CW\*(C`use Test::More\*(C'\fR. | |
244 | .PP | |
245 | .Vb 1 | |
246 | \& use Test::More tests => $Num_Tests; | |
247 | .Ve | |
248 | .PP | |
249 | There are rare cases when you will not know beforehand how many tests | |
250 | your script is going to run. In this case, you can declare that you | |
251 | have no plan. (Try to avoid using this as it weakens your test.) | |
252 | .PP | |
253 | .Vb 1 | |
254 | \& use Test::More qw(no_plan); | |
255 | .Ve | |
256 | .PP | |
257 | In some cases, you'll want to completely skip an entire testing script. | |
258 | .PP | |
259 | .Vb 1 | |
260 | \& use Test::More skip_all => $skip_reason; | |
261 | .Ve | |
262 | .PP | |
263 | Your script will declare a skip with the reason why you skipped and | |
264 | exit immediately with a zero (success). See Test::Harness for | |
265 | details. | |
266 | .PP | |
267 | If you want to control what functions Test::More will export, you | |
268 | have to use the 'import' option. For example, to import everything | |
269 | but 'fail', you'd do: | |
270 | .PP | |
271 | .Vb 1 | |
272 | \& use Test::More tests => 23, import => ['!fail']; | |
273 | .Ve | |
274 | .PP | |
275 | Alternatively, you can use the \fIplan()\fR function. Useful for when you | |
276 | have to calculate the number of tests. | |
277 | .PP | |
278 | .Vb 2 | |
279 | \& use Test::More; | |
280 | \& plan tests => keys %Stuff * 3; | |
281 | .Ve | |
282 | .PP | |
283 | or for deciding between running the tests at all: | |
284 | .PP | |
285 | .Vb 7 | |
286 | \& use Test::More; | |
287 | \& if( $^O eq 'MacOS' ) { | |
288 | \& plan skip_all => 'Test irrelevant on MacOS'; | |
289 | \& } | |
290 | \& else { | |
291 | \& plan tests => 42; | |
292 | \& } | |
293 | .Ve | |
294 | .Sh "Test names" | |
295 | .IX Subsection "Test names" | |
296 | By convention, each test is assigned a number in order. This is | |
297 | largely done automatically for you. However, it's often very useful to | |
298 | assign a name to each test. Which would you rather see: | |
299 | .PP | |
300 | .Vb 3 | |
301 | \& ok 4 | |
302 | \& not ok 5 | |
303 | \& ok 6 | |
304 | .Ve | |
305 | .PP | |
306 | or | |
307 | .PP | |
308 | .Vb 3 | |
309 | \& ok 4 - basic multi-variable | |
310 | \& not ok 5 - simple exponential | |
311 | \& ok 6 - force == mass * acceleration | |
312 | .Ve | |
313 | .PP | |
314 | The later gives you some idea of what failed. It also makes it easier | |
315 | to find the test in your script, simply search for \*(L"simple | |
316 | exponential\*(R". | |
317 | .PP | |
318 | All test functions take a name argument. It's optional, but highly | |
319 | suggested that you use it. | |
320 | .Sh "I'm ok, you're not ok." | |
321 | .IX Subsection "I'm ok, you're not ok." | |
322 | The basic purpose of this module is to print out either \*(L"ok #\*(R" or \*(L"not | |
323 | ok #\*(R" depending on if a given test succeeded or failed. Everything | |
324 | else is just gravy. | |
325 | .PP | |
326 | All of the following print \*(L"ok\*(R" or \*(L"not ok\*(R" depending on if the test | |
327 | succeeded or failed. They all also return true or false, | |
328 | respectively. | |
329 | .IP "\fBok\fR" 4 | |
330 | .IX Item "ok" | |
331 | .Vb 1 | |
332 | \& ok($this eq $that, $test_name); | |
333 | .Ve | |
334 | .Sp | |
335 | This simply evaluates any expression (\f(CW\*(C`$this eq $that\*(C'\fR is just a | |
336 | simple example) and uses that to determine if the test succeeded or | |
337 | failed. A true expression passes, a false one fails. Very simple. | |
338 | .Sp | |
339 | For example: | |
340 | .Sp | |
341 | .Vb 4 | |
342 | \& ok( $exp{9} == 81, 'simple exponential' ); | |
343 | \& ok( Film->can('db_Main'), 'set_db()' ); | |
344 | \& ok( $p->tests == 4, 'saw tests' ); | |
345 | \& ok( !grep !defined $_, @items, 'items populated' ); | |
346 | .Ve | |
347 | .Sp | |
348 | (Mnemonic: \*(L"This is ok.\*(R") | |
349 | .Sp | |
350 | $test_name is a very short description of the test that will be printed | |
351 | out. It makes it very easy to find a test in your script when it fails | |
352 | and gives others an idea of your intentions. \f(CW$test_name\fR is optional, | |
353 | but we \fBvery\fR strongly encourage its use. | |
354 | .Sp | |
355 | Should an \fIok()\fR fail, it will produce some diagnostics: | |
356 | .Sp | |
357 | .Vb 2 | |
358 | \& not ok 18 - sufficient mucus | |
359 | \& # Failed test 18 (foo.t at line 42) | |
360 | .Ve | |
361 | .Sp | |
362 | This is actually Test::Simple's \fIok()\fR routine. | |
363 | .IP "\fBis\fR" 4 | |
364 | .IX Item "is" | |
365 | .PD 0 | |
366 | .IP "\fBisnt\fR" 4 | |
367 | .IX Item "isnt" | |
368 | .PD | |
369 | .Vb 2 | |
370 | \& is ( $this, $that, $test_name ); | |
371 | \& isnt( $this, $that, $test_name ); | |
372 | .Ve | |
373 | .Sp | |
374 | Similar to \fIok()\fR, \fIis()\fR and \fIisnt()\fR compare their two arguments | |
375 | with \f(CW\*(C`eq\*(C'\fR and \f(CW\*(C`ne\*(C'\fR respectively and use the result of that to | |
376 | determine if the test succeeded or failed. So these: | |
377 | .Sp | |
378 | .Vb 2 | |
379 | \& # Is the ultimate answer 42? | |
380 | \& is( ultimate_answer(), 42, "Meaning of Life" ); | |
381 | .Ve | |
382 | .Sp | |
383 | .Vb 2 | |
384 | \& # $foo isn't empty | |
385 | \& isnt( $foo, '', "Got some foo" ); | |
386 | .Ve | |
387 | .Sp | |
388 | are similar to these: | |
389 | .Sp | |
390 | .Vb 2 | |
391 | \& ok( ultimate_answer() eq 42, "Meaning of Life" ); | |
392 | \& ok( $foo ne '', "Got some foo" ); | |
393 | .Ve | |
394 | .Sp | |
395 | (Mnemonic: \*(L"This is that.\*(R" \*(L"This isn't that.\*(R") | |
396 | .Sp | |
397 | So why use these? They produce better diagnostics on failure. \fIok()\fR | |
398 | cannot know what you are testing for (beyond the name), but \fIis()\fR and | |
399 | \&\fIisnt()\fR know what the test was and why it failed. For example this | |
400 | test: | |
401 | .Sp | |
402 | .Vb 2 | |
403 | \& my $foo = 'waffle'; my $bar = 'yarblokos'; | |
404 | \& is( $foo, $bar, 'Is foo the same as bar?' ); | |
405 | .Ve | |
406 | .Sp | |
407 | Will produce something like this: | |
408 | .Sp | |
409 | .Vb 4 | |
410 | \& not ok 17 - Is foo the same as bar? | |
411 | \& # Failed test 1 (foo.t at line 139) | |
412 | \& # got: 'waffle' | |
413 | \& # expected: 'yarblokos' | |
414 | .Ve | |
415 | .Sp | |
416 | So you can figure out what went wrong without rerunning the test. | |
417 | .Sp | |
418 | You are encouraged to use \fIis()\fR and \fIisnt()\fR over \fIok()\fR where possible, | |
419 | however do not be tempted to use them to find out if something is | |
420 | true or false! | |
421 | .Sp | |
422 | .Vb 2 | |
423 | \& # XXX BAD! $pope->isa('Catholic') eq 1 | |
424 | \& is( $pope->isa('Catholic'), 1, 'Is the Pope Catholic?' ); | |
425 | .Ve | |
426 | .Sp | |
427 | This does not check if \f(CW\*(C`$pope\-\*(C'\fRisa('Catholic')> is true, it checks if | |
428 | it returns 1. Very different. Similar caveats exist for false and 0. | |
429 | In these cases, use \fIok()\fR. | |
430 | .Sp | |
431 | .Vb 1 | |
432 | \& ok( $pope->isa('Catholic') ), 'Is the Pope Catholic?' ); | |
433 | .Ve | |
434 | .Sp | |
435 | For those grammatical pedants out there, there's an \f(CW\*(C`isn't()\*(C'\fR | |
436 | function which is an alias of \fIisnt()\fR. | |
437 | .IP "\fBlike\fR" 4 | |
438 | .IX Item "like" | |
439 | .Vb 1 | |
440 | \& like( $this, qr/that/, $test_name ); | |
441 | .Ve | |
442 | .Sp | |
443 | Similar to \fIok()\fR, \fIlike()\fR matches \f(CW$this\fR against the regex \f(CW\*(C`qr/that/\*(C'\fR. | |
444 | .Sp | |
445 | So this: | |
446 | .Sp | |
447 | .Vb 1 | |
448 | \& like($this, qr/that/, 'this is like that'); | |
449 | .Ve | |
450 | .Sp | |
451 | is similar to: | |
452 | .Sp | |
453 | .Vb 1 | |
454 | \& ok( $this =~ /that/, 'this is like that'); | |
455 | .Ve | |
456 | .Sp | |
457 | (Mnemonic \*(L"This is like that\*(R".) | |
458 | .Sp | |
459 | The second argument is a regular expression. It may be given as a | |
460 | regex reference (i.e. \f(CW\*(C`qr//\*(C'\fR) or (for better compatibility with older | |
461 | perls) as a string that looks like a regex (alternative delimiters are | |
462 | currently not supported): | |
463 | .Sp | |
464 | .Vb 1 | |
465 | \& like( $this, '/that/', 'this is like that' ); | |
466 | .Ve | |
467 | .Sp | |
468 | Regex options may be placed on the end (\f(CW'/that/i'\fR). | |
469 | .Sp | |
470 | Its advantages over \fIok()\fR are similar to that of \fIis()\fR and \fIisnt()\fR. Better | |
471 | diagnostics on failure. | |
472 | .IP "\fBunlike\fR" 4 | |
473 | .IX Item "unlike" | |
474 | .Vb 1 | |
475 | \& unlike( $this, qr/that/, $test_name ); | |
476 | .Ve | |
477 | .Sp | |
478 | Works exactly as \fIlike()\fR, only it checks if \f(CW$this\fR \fBdoes not\fR match the | |
479 | given pattern. | |
480 | .IP "\fBcmp_ok\fR" 4 | |
481 | .IX Item "cmp_ok" | |
482 | .Vb 1 | |
483 | \& cmp_ok( $this, $op, $that, $test_name ); | |
484 | .Ve | |
485 | .Sp | |
486 | Halfway between \fIok()\fR and \fIis()\fR lies \fIcmp_ok()\fR. This allows you to | |
487 | compare two arguments using any binary perl operator. | |
488 | .Sp | |
489 | .Vb 2 | |
490 | \& # ok( $this eq $that ); | |
491 | \& cmp_ok( $this, 'eq', $that, 'this eq that' ); | |
492 | .Ve | |
493 | .Sp | |
494 | .Vb 2 | |
495 | \& # ok( $this == $that ); | |
496 | \& cmp_ok( $this, '==', $that, 'this == that' ); | |
497 | .Ve | |
498 | .Sp | |
499 | .Vb 3 | |
500 | \& # ok( $this && $that ); | |
501 | \& cmp_ok( $this, '&&', $that, 'this || that' ); | |
502 | \& ...etc... | |
503 | .Ve | |
504 | .Sp | |
505 | Its advantage over \fIok()\fR is when the test fails you'll know what \f(CW$this\fR | |
506 | and \f(CW$that\fR were: | |
507 | .Sp | |
508 | .Vb 5 | |
509 | \& not ok 1 | |
510 | \& # Failed test (foo.t at line 12) | |
511 | \& # '23' | |
512 | \& # && | |
513 | \& # undef | |
514 | .Ve | |
515 | .Sp | |
516 | It's also useful in those cases where you are comparing numbers and | |
517 | \&\fIis()\fR's use of \f(CW\*(C`eq\*(C'\fR will interfere: | |
518 | .Sp | |
519 | .Vb 1 | |
520 | \& cmp_ok( $big_hairy_number, '==', $another_big_hairy_number ); | |
521 | .Ve | |
522 | .IP "\fBcan_ok\fR" 4 | |
523 | .IX Item "can_ok" | |
524 | .Vb 2 | |
525 | \& can_ok($module, @methods); | |
526 | \& can_ok($object, @methods); | |
527 | .Ve | |
528 | .Sp | |
529 | Checks to make sure the \f(CW$module\fR or \f(CW$object\fR can do these \f(CW@methods\fR | |
530 | (works with functions, too). | |
531 | .Sp | |
532 | .Vb 1 | |
533 | \& can_ok('Foo', qw(this that whatever)); | |
534 | .Ve | |
535 | .Sp | |
536 | is almost exactly like saying: | |
537 | .Sp | |
538 | .Vb 4 | |
539 | \& ok( Foo->can('this') && | |
540 | \& Foo->can('that') && | |
541 | \& Foo->can('whatever') | |
542 | \& ); | |
543 | .Ve | |
544 | .Sp | |
545 | only without all the typing and with a better interface. Handy for | |
546 | quickly testing an interface. | |
547 | .Sp | |
548 | No matter how many \f(CW@methods\fR you check, a single \fIcan_ok()\fR call counts | |
549 | as one test. If you desire otherwise, use: | |
550 | .Sp | |
551 | .Vb 3 | |
552 | \& foreach my $meth (@methods) { | |
553 | \& can_ok('Foo', $meth); | |
554 | \& } | |
555 | .Ve | |
556 | .IP "\fBisa_ok\fR" 4 | |
557 | .IX Item "isa_ok" | |
558 | .Vb 2 | |
559 | \& isa_ok($object, $class, $object_name); | |
560 | \& isa_ok($ref, $type, $ref_name); | |
561 | .Ve | |
562 | .Sp | |
563 | Checks to see if the given \f(CW$object\fR\->isa($class). Also checks to make | |
564 | sure the object was defined in the first place. Handy for this sort | |
565 | of thing: | |
566 | .Sp | |
567 | .Vb 2 | |
568 | \& my $obj = Some::Module->new; | |
569 | \& isa_ok( $obj, 'Some::Module' ); | |
570 | .Ve | |
571 | .Sp | |
572 | where you'd otherwise have to write | |
573 | .Sp | |
574 | .Vb 2 | |
575 | \& my $obj = Some::Module->new; | |
576 | \& ok( defined $obj && $obj->isa('Some::Module') ); | |
577 | .Ve | |
578 | .Sp | |
579 | to safeguard against your test script blowing up. | |
580 | .Sp | |
581 | It works on references, too: | |
582 | .Sp | |
583 | .Vb 1 | |
584 | \& isa_ok( $array_ref, 'ARRAY' ); | |
585 | .Ve | |
586 | .Sp | |
587 | The diagnostics of this test normally just refer to 'the object'. If | |
588 | you'd like them to be more specific, you can supply an \f(CW$object_name\fR | |
589 | (for example 'Test customer'). | |
590 | .IP "\fBpass\fR" 4 | |
591 | .IX Item "pass" | |
592 | .PD 0 | |
593 | .IP "\fBfail\fR" 4 | |
594 | .IX Item "fail" | |
595 | .PD | |
596 | .Vb 2 | |
597 | \& pass($test_name); | |
598 | \& fail($test_name); | |
599 | .Ve | |
600 | .Sp | |
601 | Sometimes you just want to say that the tests have passed. Usually | |
602 | the case is you've got some complicated condition that is difficult to | |
603 | wedge into an \fIok()\fR. In this case, you can simply use \fIpass()\fR (to | |
604 | declare the test ok) or fail (for not ok). They are synonyms for | |
605 | \&\fIok\fR\|(1) and \fIok\fR\|(0). | |
606 | .Sp | |
607 | Use these very, very, very sparingly. | |
608 | .Sh "Diagnostics" | |
609 | .IX Subsection "Diagnostics" | |
610 | If you pick the right test function, you'll usually get a good idea of | |
611 | what went wrong when it failed. But sometimes it doesn't work out | |
612 | that way. So here we have ways for you to write your own diagnostic | |
613 | messages which are safer than just \f(CW\*(C`print STDERR\*(C'\fR. | |
614 | .IP "\fBdiag\fR" 4 | |
615 | .IX Item "diag" | |
616 | .Vb 1 | |
617 | \& diag(@diagnostic_message); | |
618 | .Ve | |
619 | .Sp | |
620 | Prints a diagnostic message which is guaranteed not to interfere with | |
621 | test output. Handy for this sort of thing: | |
622 | .Sp | |
623 | .Vb 2 | |
624 | \& ok( grep(/foo/, @users), "There's a foo user" ) or | |
625 | \& diag("Since there's no foo, check that /etc/bar is set up right"); | |
626 | .Ve | |
627 | .Sp | |
628 | which would produce: | |
629 | .Sp | |
630 | .Vb 3 | |
631 | \& not ok 42 - There's a foo user | |
632 | \& # Failed test (foo.t at line 52) | |
633 | \& # Since there's no foo, check that /etc/bar is set up right. | |
634 | .Ve | |
635 | .Sp | |
636 | You might remember \f(CW\*(C`ok() or diag()\*(C'\fR with the mnemonic \f(CW\*(C`open() or | |
637 | die()\*(C'\fR. | |
638 | .Sp | |
639 | \&\fB\s-1NOTE\s0\fR The exact formatting of the diagnostic output is still | |
640 | changing, but it is guaranteed that whatever you throw at it it won't | |
641 | interfere with the test. | |
642 | .Sh "Module tests" | |
643 | .IX Subsection "Module tests" | |
644 | You usually want to test if the module you're testing loads ok, rather | |
645 | than just vomiting if its load fails. For such purposes we have | |
646 | \&\f(CW\*(C`use_ok\*(C'\fR and \f(CW\*(C`require_ok\*(C'\fR. | |
647 | .IP "\fBuse_ok\fR" 4 | |
648 | .IX Item "use_ok" | |
649 | .Vb 2 | |
650 | \& BEGIN { use_ok($module); } | |
651 | \& BEGIN { use_ok($module, @imports); } | |
652 | .Ve | |
653 | .Sp | |
654 | These simply use the given \f(CW$module\fR and test to make sure the load | |
655 | happened ok. It's recommended that you run \fIuse_ok()\fR inside a \s-1BEGIN\s0 | |
656 | block so its functions are exported at compile-time and prototypes are | |
657 | properly honored. | |
658 | .Sp | |
659 | If \f(CW@imports\fR are given, they are passed through to the use. So this: | |
660 | .Sp | |
661 | .Vb 1 | |
662 | \& BEGIN { use_ok('Some::Module', qw(foo bar)) } | |
663 | .Ve | |
664 | .Sp | |
665 | is like doing this: | |
666 | .Sp | |
667 | .Vb 1 | |
668 | \& use Some::Module qw(foo bar); | |
669 | .Ve | |
670 | .Sp | |
671 | don't try to do this: | |
672 | .Sp | |
673 | .Vb 2 | |
674 | \& BEGIN { | |
675 | \& use_ok('Some::Module'); | |
676 | .Ve | |
677 | .Sp | |
678 | .Vb 3 | |
679 | \& ...some code that depends on the use... | |
680 | \& ...happening at compile time... | |
681 | \& } | |
682 | .Ve | |
683 | .Sp | |
684 | instead, you want: | |
685 | .Sp | |
686 | .Vb 2 | |
687 | \& BEGIN { use_ok('Some::Module') } | |
688 | \& BEGIN { ...some code that depends on the use... } | |
689 | .Ve | |
690 | .IP "\fBrequire_ok\fR" 4 | |
691 | .IX Item "require_ok" | |
692 | .Vb 1 | |
693 | \& require_ok($module); | |
694 | .Ve | |
695 | .Sp | |
696 | Like \fIuse_ok()\fR, except it requires the \f(CW$module\fR. | |
697 | .Sh "Conditional tests" | |
698 | .IX Subsection "Conditional tests" | |
699 | Sometimes running a test under certain conditions will cause the | |
700 | test script to die. A certain function or method isn't implemented | |
701 | (such as \fIfork()\fR on MacOS), some resource isn't available (like a | |
702 | net connection) or a module isn't available. In these cases it's | |
703 | necessary to skip tests, or declare that they are supposed to fail | |
704 | but will work in the future (a todo test). | |
705 | .PP | |
706 | For more details on the mechanics of skip and todo tests see | |
707 | Test::Harness. | |
708 | .PP | |
709 | The way Test::More handles this is with a named block. Basically, a | |
710 | block of tests which can be skipped over or made todo. It's best if I | |
711 | just show you... | |
712 | .IP "\fB\s-1SKIP:\s0 \s-1BLOCK\s0\fR" 4 | |
713 | .IX Item "SKIP: BLOCK" | |
714 | .Vb 2 | |
715 | \& SKIP: { | |
716 | \& skip $why, $how_many if $condition; | |
717 | .Ve | |
718 | .Sp | |
719 | .Vb 2 | |
720 | \& ...normal testing code goes here... | |
721 | \& } | |
722 | .Ve | |
723 | .Sp | |
724 | This declares a block of tests that might be skipped, \f(CW$how_many\fR tests | |
725 | there are, \f(CW$why\fR and under what \f(CW$condition\fR to skip them. An example is | |
726 | the easiest way to illustrate: | |
727 | .Sp | |
728 | .Vb 2 | |
729 | \& SKIP: { | |
730 | \& eval { require HTML::Lint }; | |
731 | .Ve | |
732 | .Sp | |
733 | .Vb 1 | |
734 | \& skip "HTML::Lint not installed", 2 if $@; | |
735 | .Ve | |
736 | .Sp | |
737 | .Vb 2 | |
738 | \& my $lint = new HTML::Lint; | |
739 | \& ok( $lint, "Created object" ); | |
740 | .Ve | |
741 | .Sp | |
742 | .Vb 3 | |
743 | \& $lint->parse( $html ); | |
744 | \& is( scalar $lint->errors, 0, "No errors found in HTML" ); | |
745 | \& } | |
746 | .Ve | |
747 | .Sp | |
748 | If the user does not have HTML::Lint installed, the whole block of | |
749 | code \fIwon't be run at all\fR. Test::More will output special ok's | |
750 | which Test::Harness interprets as skipped, but passing, tests. | |
751 | It's important that \f(CW$how_many\fR accurately reflects the number of tests | |
752 | in the \s-1SKIP\s0 block so the # of tests run will match up with your plan. | |
753 | .Sp | |
754 | It's perfectly safe to nest \s-1SKIP\s0 blocks. Each \s-1SKIP\s0 block must have | |
755 | the label \f(CW\*(C`SKIP\*(C'\fR, or Test::More can't work its magic. | |
756 | .Sp | |
757 | You don't skip tests which are failing because there's a bug in your | |
758 | program, or for which you don't yet have code written. For that you | |
759 | use \s-1TODO\s0. Read on. | |
760 | .IP "\fB\s-1TODO:\s0 \s-1BLOCK\s0\fR" 4 | |
761 | .IX Item "TODO: BLOCK" | |
762 | .Vb 2 | |
763 | \& TODO: { | |
764 | \& local $TODO = $why if $condition; | |
765 | .Ve | |
766 | .Sp | |
767 | .Vb 2 | |
768 | \& ...normal testing code goes here... | |
769 | \& } | |
770 | .Ve | |
771 | .Sp | |
772 | Declares a block of tests you expect to fail and \f(CW$why\fR. Perhaps it's | |
773 | because you haven't fixed a bug or haven't finished a new feature: | |
774 | .Sp | |
775 | .Vb 2 | |
776 | \& TODO: { | |
777 | \& local $TODO = "URI::Geller not finished"; | |
778 | .Ve | |
779 | .Sp | |
780 | .Vb 2 | |
781 | \& my $card = "Eight of clubs"; | |
782 | \& is( URI::Geller->your_card, $card, 'Is THIS your card?' ); | |
783 | .Ve | |
784 | .Sp | |
785 | .Vb 4 | |
786 | \& my $spoon; | |
787 | \& URI::Geller->bend_spoon; | |
788 | \& is( $spoon, 'bent', "Spoon bending, that's original" ); | |
789 | \& } | |
790 | .Ve | |
791 | .Sp | |
792 | With a todo block, the tests inside are expected to fail. Test::More | |
793 | will run the tests normally, but print out special flags indicating | |
794 | they are \*(L"todo\*(R". Test::Harness will interpret failures as being ok. | |
795 | Should anything succeed, it will report it as an unexpected success. | |
796 | You then know the thing you had todo is done and can remove the | |
797 | \&\s-1TODO\s0 flag. | |
798 | .Sp | |
799 | The nice part about todo tests, as opposed to simply commenting out a | |
800 | block of tests, is it's like having a programmatic todo list. You know | |
801 | how much work is left to be done, you're aware of what bugs there are, | |
802 | and you'll know immediately when they're fixed. | |
803 | .Sp | |
804 | Once a todo test starts succeeding, simply move it outside the block. | |
805 | When the block is empty, delete it. | |
806 | .IP "\fBtodo_skip\fR" 4 | |
807 | .IX Item "todo_skip" | |
808 | .Vb 2 | |
809 | \& TODO: { | |
810 | \& todo_skip $why, $how_many if $condition; | |
811 | .Ve | |
812 | .Sp | |
813 | .Vb 2 | |
814 | \& ...normal testing code... | |
815 | \& } | |
816 | .Ve | |
817 | .Sp | |
818 | With todo tests, it's best to have the tests actually run. That way | |
819 | you'll know when they start passing. Sometimes this isn't possible. | |
820 | Often a failing test will cause the whole program to die or hang, even | |
821 | inside an \f(CW\*(C`eval BLOCK\*(C'\fR with and using \f(CW\*(C`alarm\*(C'\fR. In these extreme | |
822 | cases you have no choice but to skip over the broken tests entirely. | |
823 | .Sp | |
824 | The syntax and behavior is similar to a \f(CW\*(C`SKIP: BLOCK\*(C'\fR except the | |
825 | tests will be marked as failing but todo. Test::Harness will | |
826 | interpret them as passing. | |
827 | .IP "When do I use \s-1SKIP\s0 vs. \s-1TODO\s0?" 4 | |
828 | .IX Item "When do I use SKIP vs. TODO?" | |
829 | \&\fBIf it's something the user might not be able to do\fR, use \s-1SKIP\s0. | |
830 | This includes optional modules that aren't installed, running under | |
831 | an \s-1OS\s0 that doesn't have some feature (like \fIfork()\fR or symlinks), or maybe | |
832 | you need an Internet connection and one isn't available. | |
833 | .Sp | |
834 | \&\fBIf it's something the programmer hasn't done yet\fR, use \s-1TODO\s0. This | |
835 | is for any code you haven't written yet, or bugs you have yet to fix, | |
836 | but want to put tests in your testing script (always a good idea). | |
837 | .Sh "Comparison functions" | |
838 | .IX Subsection "Comparison functions" | |
839 | Not everything is a simple eq check or regex. There are times you | |
840 | need to see if two arrays are equivalent, for instance. For these | |
841 | instances, Test::More provides a handful of useful functions. | |
842 | .PP | |
843 | \&\fB\s-1NOTE\s0\fR These are \s-1NOT\s0 well-tested on circular references. Nor am I | |
844 | quite sure what will happen with filehandles. | |
845 | .IP "\fBis_deeply\fR" 4 | |
846 | .IX Item "is_deeply" | |
847 | .Vb 1 | |
848 | \& is_deeply( $this, $that, $test_name ); | |
849 | .Ve | |
850 | .Sp | |
851 | Similar to \fIis()\fR, except that if \f(CW$this\fR and \f(CW$that\fR are hash or array | |
852 | references, it does a deep comparison walking each data structure to | |
853 | see if they are equivalent. If the two structures are different, it | |
854 | will display the place where they start differing. | |
855 | .Sp | |
856 | Barrie Slaymaker's Test::Differences module provides more in-depth | |
857 | functionality along these lines, and it plays well with Test::More. | |
858 | .Sp | |
859 | \&\fB\s-1NOTE\s0\fR Display of scalar refs is not quite 100% | |
860 | .IP "\fBeq_array\fR" 4 | |
861 | .IX Item "eq_array" | |
862 | .Vb 1 | |
863 | \& eq_array(\e@this, \e@that); | |
864 | .Ve | |
865 | .Sp | |
866 | Checks if two arrays are equivalent. This is a deep check, so | |
867 | multi-level structures are handled correctly. | |
868 | .IP "\fBeq_hash\fR" 4 | |
869 | .IX Item "eq_hash" | |
870 | .Vb 1 | |
871 | \& eq_hash(\e%this, \e%that); | |
872 | .Ve | |
873 | .Sp | |
874 | Determines if the two hashes contain the same keys and values. This | |
875 | is a deep check. | |
876 | .IP "\fBeq_set\fR" 4 | |
877 | .IX Item "eq_set" | |
878 | .Vb 1 | |
879 | \& eq_set(\e@this, \e@that); | |
880 | .Ve | |
881 | .Sp | |
882 | Similar to \fIeq_array()\fR, except the order of the elements is \fBnot\fR | |
883 | important. This is a deep check, but the irrelevancy of order only | |
884 | applies to the top level. | |
885 | .Sh "Extending and Embedding Test::More" | |
886 | .IX Subsection "Extending and Embedding Test::More" | |
887 | Sometimes the Test::More interface isn't quite enough. Fortunately, | |
888 | Test::More is built on top of Test::Builder which provides a single, | |
889 | unified backend for any test library to use. This means two test | |
890 | libraries which both use Test::Builder \fBcan be used together in the | |
891 | same program\fR. | |
892 | .PP | |
893 | If you simply want to do a little tweaking of how the tests behave, | |
894 | you can access the underlying Test::Builder object like so: | |
895 | .IP "\fBbuilder\fR" 4 | |
896 | .IX Item "builder" | |
897 | .Vb 1 | |
898 | \& my $test_builder = Test::More->builder; | |
899 | .Ve | |
900 | .Sp | |
901 | Returns the Test::Builder object underlying Test::More for you to play | |
902 | with. | |
903 | .SH "NOTES" | |
904 | .IX Header "NOTES" | |
905 | Test::More is \fBexplicitly\fR tested all the way back to perl 5.004. | |
906 | .PP | |
907 | Test::More is thread-safe for perl 5.8.0 and up. | |
908 | .SH "BUGS and CAVEATS" | |
909 | .IX Header "BUGS and CAVEATS" | |
910 | .IP "Making your own \fIok()\fR" 4 | |
911 | .IX Item "Making your own ok()" | |
912 | If you are trying to extend Test::More, don't. Use Test::Builder | |
913 | instead. | |
914 | .IP "The eq_* family has some caveats." 4 | |
915 | .IX Item "The eq_* family has some caveats." | |
916 | .PD 0 | |
917 | .IP "Test::Harness upgrades" 4 | |
918 | .IX Item "Test::Harness upgrades" | |
919 | .PD | |
920 | no_plan and todo depend on new Test::Harness features and fixes. If | |
921 | you're going to distribute tests that use no_plan or todo your | |
922 | end-users will have to upgrade Test::Harness to the latest one on | |
923 | \&\s-1CPAN\s0. If you avoid no_plan and \s-1TODO\s0 tests, the stock Test::Harness | |
924 | will work fine. | |
925 | .Sp | |
926 | If you simply depend on Test::More, it's own dependencies will cause a | |
927 | Test::Harness upgrade. | |
928 | .SH "HISTORY" | |
929 | .IX Header "HISTORY" | |
930 | This is a case of convergent evolution with Joshua Pritikin's Test | |
931 | module. I was largely unaware of its existence when I'd first | |
932 | written my own \fIok()\fR routines. This module exists because I can't | |
933 | figure out how to easily wedge test names into Test's interface (along | |
934 | with a few other problems). | |
935 | .PP | |
936 | The goal here is to have a testing utility that's simple to learn, | |
937 | quick to use and difficult to trip yourself up with while still | |
938 | providing more flexibility than the existing Test.pm. As such, the | |
939 | names of the most common routines are kept tiny, special cases and | |
940 | magic side-effects are kept to a minimum. \s-1WYSIWYG\s0. | |
941 | .SH "SEE ALSO" | |
942 | .IX Header "SEE ALSO" | |
943 | Test::Simple if all this confuses you and you just want to write | |
944 | some tests. You can upgrade to Test::More later (it's forward | |
945 | compatible). | |
946 | .PP | |
947 | Test::Differences for more ways to test complex data structures. | |
948 | And it plays well with Test::More. | |
949 | .PP | |
950 | Test is the old testing module. Its main benefit is that it has | |
951 | been distributed with Perl since 5.004_05. | |
952 | .PP | |
953 | Test::Harness for details on how your test results are interpreted | |
954 | by Perl. | |
955 | .PP | |
956 | Test::Unit describes a very featureful unit testing interface. | |
957 | .PP | |
958 | Test::Inline shows the idea of embedded testing. | |
959 | .PP | |
960 | SelfTest is another approach to embedded testing. | |
961 | .SH "AUTHORS" | |
962 | .IX Header "AUTHORS" | |
963 | Michael G Schwern <schwern@pobox.com> with much inspiration | |
964 | from Joshua Pritikin's Test module and lots of help from Barrie | |
965 | Slaymaker, Tony Bowden, chromatic and the perl-qa gang. | |
966 | .SH "COPYRIGHT" | |
967 | .IX Header "COPYRIGHT" | |
968 | Copyright 2001 by Michael G Schwern <schwern@pobox.com>. | |
969 | .PP | |
970 | This program is free software; you can redistribute it and/or | |
971 | modify it under the same terms as Perl itself. | |
972 | .PP | |
973 | See \fIhttp://www.perl.com/perl/misc/Artistic.html\fR |