Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
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::Tutorial 3" | |
132 | .TH Test::Tutorial 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Test::Tutorial \- A tutorial about writing really basic tests | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | \&\fI\s-1AHHHHHHH\s0!!!! \s-1NOT\s0 \s-1TESTING\s0! Anything but testing! | |
138 | Beat me, whip me, send me to Detroit, but don't make | |
139 | me write tests!\fR | |
140 | .PP | |
141 | \&\fI*sob*\fR | |
142 | .PP | |
143 | \&\fIBesides, I don't know how to write the damned things.\fR | |
144 | .PP | |
145 | Is this you? Is writing tests right up there with writing | |
146 | documentation and having your fingernails pulled out? Did you open up | |
147 | a test and read | |
148 | .PP | |
149 | .Vb 1 | |
150 | \& ######## We start with some black magic | |
151 | .Ve | |
152 | .PP | |
153 | and decide that's quite enough for you? | |
154 | .PP | |
155 | It's ok. That's all gone now. We've done all the black magic for | |
156 | you. And here are the tricks... | |
157 | .Sh "Nuts and bolts of testing." | |
158 | .IX Subsection "Nuts and bolts of testing." | |
159 | Here's the most basic test program. | |
160 | .PP | |
161 | .Vb 1 | |
162 | \& #!/usr/bin/perl -w | |
163 | .Ve | |
164 | .PP | |
165 | .Vb 1 | |
166 | \& print "1..1\en"; | |
167 | .Ve | |
168 | .PP | |
169 | .Vb 1 | |
170 | \& print 1 + 1 == 2 ? "ok 1\en" : "not ok 1\en"; | |
171 | .Ve | |
172 | .PP | |
173 | since 1 + 1 is 2, it prints: | |
174 | .PP | |
175 | .Vb 2 | |
176 | \& 1..1 | |
177 | \& ok 1 | |
178 | .Ve | |
179 | .PP | |
180 | What this says is: \f(CW1..1\fR \*(L"I'm going to run one test.\*(R" [1] \f(CW\*(C`ok 1\*(C'\fR | |
181 | \&\*(L"The first test passed\*(R". And that's about all magic there is to | |
182 | testing. Your basic unit of testing is the \fIok\fR. For each thing you | |
183 | test, an \f(CW\*(C`ok\*(C'\fR is printed. Simple. \fBTest::Harness\fR interprets your test | |
184 | results to determine if you succeeded or failed (more on that later). | |
185 | .PP | |
186 | Writing all these print statements rapidly gets tedious. Fortunately, | |
187 | there's \fBTest::Simple\fR. It has one function, \f(CW\*(C`ok()\*(C'\fR. | |
188 | .PP | |
189 | .Vb 1 | |
190 | \& #!/usr/bin/perl -w | |
191 | .Ve | |
192 | .PP | |
193 | .Vb 1 | |
194 | \& use Test::Simple tests => 1; | |
195 | .Ve | |
196 | .PP | |
197 | .Vb 1 | |
198 | \& ok( 1 + 1 == 2 ); | |
199 | .Ve | |
200 | .PP | |
201 | and that does the same thing as the code above. \f(CW\*(C`ok()\*(C'\fR is the backbone | |
202 | of Perl testing, and we'll be using it instead of roll-your-own from | |
203 | here on. If \f(CW\*(C`ok()\*(C'\fR gets a true value, the test passes. False, it | |
204 | fails. | |
205 | .PP | |
206 | .Vb 1 | |
207 | \& #!/usr/bin/perl -w | |
208 | .Ve | |
209 | .PP | |
210 | .Vb 3 | |
211 | \& use Test::Simple tests => 2; | |
212 | \& ok( 1 + 1 == 2 ); | |
213 | \& ok( 2 + 2 == 5 ); | |
214 | .Ve | |
215 | .PP | |
216 | from that comes | |
217 | .PP | |
218 | .Vb 5 | |
219 | \& 1..2 | |
220 | \& ok 1 | |
221 | \& not ok 2 | |
222 | \& # Failed test (test.pl at line 5) | |
223 | \& # Looks like you failed 1 tests of 2. | |
224 | .Ve | |
225 | .PP | |
226 | \&\f(CW1..2\fR \*(L"I'm going to run two tests.\*(R" This number is used to ensure | |
227 | your test program ran all the way through and didn't die or skip some | |
228 | tests. \f(CW\*(C`ok 1\*(C'\fR \*(L"The first test passed.\*(R" \f(CW\*(C`not ok 2\*(C'\fR \*(L"The second test | |
229 | failed\*(R". Test::Simple helpfully prints out some extra commentary about | |
230 | your tests. | |
231 | .PP | |
232 | It's not scary. Come, hold my hand. We're going to give an example | |
233 | of testing a module. For our example, we'll be testing a date | |
234 | library, \fBDate::ICal\fR. It's on \s-1CPAN\s0, so download a copy and follow | |
235 | along. [2] | |
236 | .Sh "Where to start?" | |
237 | .IX Subsection "Where to start?" | |
238 | This is the hardest part of testing, where do you start? People often | |
239 | get overwhelmed at the apparent enormity of the task of testing a | |
240 | whole module. Best place to start is at the beginning. Date::ICal is | |
241 | an object-oriented module, and that means you start by making an | |
242 | object. So we test \f(CW\*(C`new()\*(C'\fR. | |
243 | .PP | |
244 | .Vb 1 | |
245 | \& #!/usr/bin/perl -w | |
246 | .Ve | |
247 | .PP | |
248 | .Vb 1 | |
249 | \& use Test::Simple tests => 2; | |
250 | .Ve | |
251 | .PP | |
252 | .Vb 1 | |
253 | \& use Date::ICal; | |
254 | .Ve | |
255 | .PP | |
256 | .Vb 3 | |
257 | \& my $ical = Date::ICal->new; # create an object | |
258 | \& ok( defined $ical ); # check that we got something | |
259 | \& ok( $ical->isa('Date::ICal') ); # and it's the right class | |
260 | .Ve | |
261 | .PP | |
262 | run that and you should get: | |
263 | .PP | |
264 | .Vb 3 | |
265 | \& 1..2 | |
266 | \& ok 1 | |
267 | \& ok 2 | |
268 | .Ve | |
269 | .PP | |
270 | congratulations, you've written your first useful test. | |
271 | .Sh "Names" | |
272 | .IX Subsection "Names" | |
273 | That output isn't terribly descriptive, is it? When you have two | |
274 | tests you can figure out which one is #2, but what if you have 102? | |
275 | .PP | |
276 | Each test can be given a little descriptive name as the second | |
277 | argument to \f(CW\*(C`ok()\*(C'\fR. | |
278 | .PP | |
279 | .Vb 1 | |
280 | \& use Test::Simple tests => 2; | |
281 | .Ve | |
282 | .PP | |
283 | .Vb 2 | |
284 | \& ok( defined $ical, 'new() returned something' ); | |
285 | \& ok( $ical->isa('Date::ICal'), " and it's the right class" ); | |
286 | .Ve | |
287 | .PP | |
288 | So now you'd see... | |
289 | .PP | |
290 | .Vb 3 | |
291 | \& 1..2 | |
292 | \& ok 1 - new() returned something | |
293 | \& ok 2 - and it's the right class | |
294 | .Ve | |
295 | .Sh "Test the manual" | |
296 | .IX Subsection "Test the manual" | |
297 | Simplest way to build up a decent testing suite is to just test what | |
298 | the manual says it does. [3] Let's pull something out of the | |
299 | \&\*(L"\s-1SYNOPSIS\s0\*(R" in Date::ICal and test that all its bits work. | |
300 | .PP | |
301 | .Vb 1 | |
302 | \& #!/usr/bin/perl -w | |
303 | .Ve | |
304 | .PP | |
305 | .Vb 1 | |
306 | \& use Test::Simple tests => 8; | |
307 | .Ve | |
308 | .PP | |
309 | .Vb 1 | |
310 | \& use Date::ICal; | |
311 | .Ve | |
312 | .PP | |
313 | .Vb 3 | |
314 | \& $ical = Date::ICal->new( year => 1964, month => 10, day => 16, | |
315 | \& hour => 16, min => 12, sec => 47, | |
316 | \& tz => '0530' ); | |
317 | .Ve | |
318 | .PP | |
319 | .Vb 8 | |
320 | \& ok( defined $ical, 'new() returned something' ); | |
321 | \& ok( $ical->isa('Date::ICal'), " and it's the right class" ); | |
322 | \& ok( $ical->sec == 47, ' sec()' ); | |
323 | \& ok( $ical->min == 12, ' min()' ); | |
324 | \& ok( $ical->hour == 16, ' hour()' ); | |
325 | \& ok( $ical->day == 17, ' day()' ); | |
326 | \& ok( $ical->month == 10, ' month()' ); | |
327 | \& ok( $ical->year == 1964, ' year()' ); | |
328 | .Ve | |
329 | .PP | |
330 | run that and you get: | |
331 | .PP | |
332 | .Vb 11 | |
333 | \& 1..8 | |
334 | \& ok 1 - new() returned something | |
335 | \& ok 2 - and it's the right class | |
336 | \& ok 3 - sec() | |
337 | \& ok 4 - min() | |
338 | \& ok 5 - hour() | |
339 | \& not ok 6 - day() | |
340 | \& # Failed test (- at line 16) | |
341 | \& ok 7 - month() | |
342 | \& ok 8 - year() | |
343 | \& # Looks like you failed 1 tests of 8. | |
344 | .Ve | |
345 | .PP | |
346 | Whoops, a failure! [4] Test::Simple helpfully lets us know on what line | |
347 | the failure occurred, but not much else. We were supposed to get 17, | |
348 | but we didn't. What did we get?? Dunno. We'll have to re-run the | |
349 | test in the debugger or throw in some print statements to find out. | |
350 | .PP | |
351 | Instead, we'll switch from \fBTest::Simple\fR to \fBTest::More\fR. \fBTest::More\fR | |
352 | does everything \fBTest::Simple\fR does, and more! In fact, Test::More does | |
353 | things \fIexactly\fR the way Test::Simple does. You can literally swap | |
354 | Test::Simple out and put Test::More in its place. That's just what | |
355 | we're going to do. | |
356 | .PP | |
357 | Test::More does more than Test::Simple. The most important difference | |
358 | at this point is it provides more informative ways to say \*(L"ok\*(R". | |
359 | Although you can write almost any test with a generic \f(CW\*(C`ok()\*(C'\fR, it | |
360 | can't tell you what went wrong. Instead, we'll use the \f(CW\*(C`is()\*(C'\fR | |
361 | function, which lets us declare that something is supposed to be the | |
362 | same as something else: | |
363 | .PP | |
364 | .Vb 1 | |
365 | \& #!/usr/bin/perl -w | |
366 | .Ve | |
367 | .PP | |
368 | .Vb 1 | |
369 | \& use Test::More tests => 8; | |
370 | .Ve | |
371 | .PP | |
372 | .Vb 1 | |
373 | \& use Date::ICal; | |
374 | .Ve | |
375 | .PP | |
376 | .Vb 3 | |
377 | \& $ical = Date::ICal->new( year => 1964, month => 10, day => 16, | |
378 | \& hour => 16, min => 12, sec => 47, | |
379 | \& tz => '0530' ); | |
380 | .Ve | |
381 | .PP | |
382 | .Vb 8 | |
383 | \& ok( defined $ical, 'new() returned something' ); | |
384 | \& ok( $ical->isa('Date::ICal'), " and it's the right class" ); | |
385 | \& is( $ical->sec, 47, ' sec()' ); | |
386 | \& is( $ical->min, 12, ' min()' ); | |
387 | \& is( $ical->hour, 16, ' hour()' ); | |
388 | \& is( $ical->day, 17, ' day()' ); | |
389 | \& is( $ical->month, 10, ' month()' ); | |
390 | \& is( $ical->year, 1964, ' year()' ); | |
391 | .Ve | |
392 | .PP | |
393 | "Is \f(CW\*(C`$ical\->sec\*(C'\fR 47?\*(L" \*(R"Is \f(CW\*(C`$ical\->min\*(C'\fR 12?" With \f(CW\*(C`is()\*(C'\fR in place, | |
394 | you get some more information | |
395 | .PP | |
396 | .Vb 13 | |
397 | \& 1..8 | |
398 | \& ok 1 - new() returned something | |
399 | \& ok 2 - and it's the right class | |
400 | \& ok 3 - sec() | |
401 | \& ok 4 - min() | |
402 | \& ok 5 - hour() | |
403 | \& not ok 6 - day() | |
404 | \& # Failed test (- at line 16) | |
405 | \& # got: '16' | |
406 | \& # expected: '17' | |
407 | \& ok 7 - month() | |
408 | \& ok 8 - year() | |
409 | \& # Looks like you failed 1 tests of 8. | |
410 | .Ve | |
411 | .PP | |
412 | letting us know that \f(CW\*(C`$ical\->day\*(C'\fR returned 16, but we expected 17. A | |
413 | quick check shows that the code is working fine, we made a mistake | |
414 | when writing up the tests. Just change it to: | |
415 | .PP | |
416 | .Vb 1 | |
417 | \& is( $ical->day, 16, ' day()' ); | |
418 | .Ve | |
419 | .PP | |
420 | and everything works. | |
421 | .PP | |
422 | So any time you're doing a \*(L"this equals that\*(R" sort of test, use \f(CW\*(C`is()\*(C'\fR. | |
423 | It even works on arrays. The test is always in scalar context, so you | |
424 | can test how many elements are in a list this way. [5] | |
425 | .PP | |
426 | .Vb 1 | |
427 | \& is( @foo, 5, 'foo has 5 elements' ); | |
428 | .Ve | |
429 | .Sh "Sometimes the tests are wrong" | |
430 | .IX Subsection "Sometimes the tests are wrong" | |
431 | Which brings us to a very important lesson. Code has bugs. Tests are | |
432 | code. Ergo, tests have bugs. A failing test could mean a bug in the | |
433 | code, but don't discount the possibility that the test is wrong. | |
434 | .PP | |
435 | On the flip side, don't be tempted to prematurely declare a test | |
436 | incorrect just because you're having trouble finding the bug. | |
437 | Invalidating a test isn't something to be taken lightly, and don't use | |
438 | it as a cop out to avoid work. | |
439 | .Sh "Testing lots of values" | |
440 | .IX Subsection "Testing lots of values" | |
441 | We're going to be wanting to test a lot of dates here, trying to trick | |
442 | the code with lots of different edge cases. Does it work before 1970? | |
443 | After 2038? Before 1904? Do years after 10,000 give it trouble? | |
444 | Does it get leap years right? We could keep repeating the code above, | |
445 | or we could set up a little try/expect loop. | |
446 | .PP | |
447 | .Vb 2 | |
448 | \& use Test::More tests => 32; | |
449 | \& use Date::ICal; | |
450 | .Ve | |
451 | .PP | |
452 | .Vb 12 | |
453 | \& my %ICal_Dates = ( | |
454 | \& # An ICal string And the year, month, date | |
455 | \& # hour, minute and second we expect. | |
456 | \& '19971024T120000' => # from the docs. | |
457 | \& [ 1997, 10, 24, 12, 0, 0 ], | |
458 | \& '20390123T232832' => # after the Unix epoch | |
459 | \& [ 2039, 1, 23, 23, 28, 32 ], | |
460 | \& '19671225T000000' => # before the Unix epoch | |
461 | \& [ 1967, 12, 25, 0, 0, 0 ], | |
462 | \& '18990505T232323' => # before the MacOS epoch | |
463 | \& [ 1899, 5, 5, 23, 23, 23 ], | |
464 | \& ); | |
465 | .Ve | |
466 | .PP | |
467 | .Vb 2 | |
468 | \& while( my($ical_str, $expect) = each %ICal_Dates ) { | |
469 | \& my $ical = Date::ICal->new( ical => $ical_str ); | |
470 | .Ve | |
471 | .PP | |
472 | .Vb 2 | |
473 | \& ok( defined $ical, "new(ical => '$ical_str')" ); | |
474 | \& ok( $ical->isa('Date::ICal'), " and it's the right class" ); | |
475 | .Ve | |
476 | .PP | |
477 | .Vb 7 | |
478 | \& is( $ical->year, $expect->[0], ' year()' ); | |
479 | \& is( $ical->month, $expect->[1], ' month()' ); | |
480 | \& is( $ical->day, $expect->[2], ' day()' ); | |
481 | \& is( $ical->hour, $expect->[3], ' hour()' ); | |
482 | \& is( $ical->min, $expect->[4], ' min()' ); | |
483 | \& is( $ical->sec, $expect->[5], ' sec()' ); | |
484 | \& } | |
485 | .Ve | |
486 | .PP | |
487 | So now we can test bunches of dates by just adding them to | |
488 | \&\f(CW%ICal_Dates\fR. Now that it's less work to test with more dates, you'll | |
489 | be inclined to just throw more in as you think of them. | |
490 | Only problem is, every time we add to that we have to keep adjusting | |
491 | the \f(CW\*(C`use Test::More tests => ##\*(C'\fR line. That can rapidly get | |
492 | annoying. There's two ways to make this work better. | |
493 | .PP | |
494 | First, we can calculate the plan dynamically using the \f(CW\*(C`plan()\*(C'\fR | |
495 | function. | |
496 | .PP | |
497 | .Vb 2 | |
498 | \& use Test::More; | |
499 | \& use Date::ICal; | |
500 | .Ve | |
501 | .PP | |
502 | .Vb 3 | |
503 | \& my %ICal_Dates = ( | |
504 | \& ...same as before... | |
505 | \& ); | |
506 | .Ve | |
507 | .PP | |
508 | .Vb 2 | |
509 | \& # For each key in the hash we're running 8 tests. | |
510 | \& plan tests => keys %ICal_Dates * 8; | |
511 | .Ve | |
512 | .PP | |
513 | Or to be even more flexible, we use \f(CW\*(C`no_plan\*(C'\fR. This means we're just | |
514 | running some tests, don't know how many. [6] | |
515 | .PP | |
516 | .Vb 1 | |
517 | \& use Test::More 'no_plan'; # instead of tests => 32 | |
518 | .Ve | |
519 | .PP | |
520 | now we can just add tests and not have to do all sorts of math to | |
521 | figure out how many we're running. | |
522 | .Sh "Informative names" | |
523 | .IX Subsection "Informative names" | |
524 | Take a look at this line here | |
525 | .PP | |
526 | .Vb 1 | |
527 | \& ok( defined $ical, "new(ical => '$ical_str')" ); | |
528 | .Ve | |
529 | .PP | |
530 | we've added more detail about what we're testing and the ICal string | |
531 | itself we're trying out to the name. So you get results like: | |
532 | .PP | |
533 | .Vb 8 | |
534 | \& ok 25 - new(ical => '19971024T120000') | |
535 | \& ok 26 - and it's the right class | |
536 | \& ok 27 - year() | |
537 | \& ok 28 - month() | |
538 | \& ok 29 - day() | |
539 | \& ok 30 - hour() | |
540 | \& ok 31 - min() | |
541 | \& ok 32 - sec() | |
542 | .Ve | |
543 | .PP | |
544 | if something in there fails, you'll know which one it was and that | |
545 | will make tracking down the problem easier. So try to put a bit of | |
546 | debugging information into the test names. | |
547 | .PP | |
548 | Describe what the tests test, to make debugging a failed test easier | |
549 | for you or for the next person who runs your test. | |
550 | .Sh "Skipping tests" | |
551 | .IX Subsection "Skipping tests" | |
552 | Poking around in the existing Date::ICal tests, I found this in | |
553 | \&\fIt/01sanity.t\fR [7] | |
554 | .PP | |
555 | .Vb 1 | |
556 | \& #!/usr/bin/perl -w | |
557 | .Ve | |
558 | .PP | |
559 | .Vb 2 | |
560 | \& use Test::More tests => 7; | |
561 | \& use Date::ICal; | |
562 | .Ve | |
563 | .PP | |
564 | .Vb 3 | |
565 | \& # Make sure epoch time is being handled sanely. | |
566 | \& my $t1 = Date::ICal->new( epoch => 0 ); | |
567 | \& is( $t1->epoch, 0, "Epoch time of 0" ); | |
568 | .Ve | |
569 | .PP | |
570 | .Vb 2 | |
571 | \& # XXX This will only work on unix systems. | |
572 | \& is( $t1->ical, '19700101Z', " epoch to ical" ); | |
573 | .Ve | |
574 | .PP | |
575 | .Vb 3 | |
576 | \& is( $t1->year, 1970, " year()" ); | |
577 | \& is( $t1->month, 1, " month()" ); | |
578 | \& is( $t1->day, 1, " day()" ); | |
579 | .Ve | |
580 | .PP | |
581 | .Vb 3 | |
582 | \& # like the tests above, but starting with ical instead of epoch | |
583 | \& my $t2 = Date::ICal->new( ical => '19700101Z' ); | |
584 | \& is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" ); | |
585 | .Ve | |
586 | .PP | |
587 | .Vb 1 | |
588 | \& is( $t2->epoch, 0, " and back to ICal" ); | |
589 | .Ve | |
590 | .PP | |
591 | The beginning of the epoch is different on most non-Unix operating | |
592 | systems [8]. Even though Perl smooths out the differences for the most | |
593 | part, certain ports do it differently. MacPerl is one off the top of | |
594 | my head. [9] We \fIknow\fR this will never work on MacOS. So rather than | |
595 | just putting a comment in the test, we can explicitly say it's never | |
596 | going to work and skip the test. | |
597 | .PP | |
598 | .Vb 2 | |
599 | \& use Test::More tests => 7; | |
600 | \& use Date::ICal; | |
601 | .Ve | |
602 | .PP | |
603 | .Vb 3 | |
604 | \& # Make sure epoch time is being handled sanely. | |
605 | \& my $t1 = Date::ICal->new( epoch => 0 ); | |
606 | \& is( $t1->epoch, 0, "Epoch time of 0" ); | |
607 | .Ve | |
608 | .PP | |
609 | .Vb 3 | |
610 | \& SKIP: { | |
611 | \& skip('epoch to ICal not working on MacOS', 6) | |
612 | \& if $^O eq 'MacOS'; | |
613 | .Ve | |
614 | .PP | |
615 | .Vb 1 | |
616 | \& is( $t1->ical, '19700101Z', " epoch to ical" ); | |
617 | .Ve | |
618 | .PP | |
619 | .Vb 3 | |
620 | \& is( $t1->year, 1970, " year()" ); | |
621 | \& is( $t1->month, 1, " month()" ); | |
622 | \& is( $t1->day, 1, " day()" ); | |
623 | .Ve | |
624 | .PP | |
625 | .Vb 3 | |
626 | \& # like the tests above, but starting with ical instead of epoch | |
627 | \& my $t2 = Date::ICal->new( ical => '19700101Z' ); | |
628 | \& is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" ); | |
629 | .Ve | |
630 | .PP | |
631 | .Vb 2 | |
632 | \& is( $t2->epoch, 0, " and back to ICal" ); | |
633 | \& } | |
634 | .Ve | |
635 | .PP | |
636 | A little bit of magic happens here. When running on anything but | |
637 | MacOS, all the tests run normally. But when on MacOS, \f(CW\*(C`skip()\*(C'\fR causes | |
638 | the entire contents of the \s-1SKIP\s0 block to be jumped over. It's never | |
639 | run. Instead, it prints special output that tells Test::Harness that | |
640 | the tests have been skipped. | |
641 | .PP | |
642 | .Vb 8 | |
643 | \& 1..7 | |
644 | \& ok 1 - Epoch time of 0 | |
645 | \& ok 2 # skip epoch to ICal not working on MacOS | |
646 | \& ok 3 # skip epoch to ICal not working on MacOS | |
647 | \& ok 4 # skip epoch to ICal not working on MacOS | |
648 | \& ok 5 # skip epoch to ICal not working on MacOS | |
649 | \& ok 6 # skip epoch to ICal not working on MacOS | |
650 | \& ok 7 # skip epoch to ICal not working on MacOS | |
651 | .Ve | |
652 | .PP | |
653 | This means your tests won't fail on MacOS. This means less emails | |
654 | from MacPerl users telling you about failing tests that you know will | |
655 | never work. You've got to be careful with skip tests. These are for | |
656 | tests which don't work and \fInever will\fR. It is not for skipping | |
657 | genuine bugs (we'll get to that in a moment). | |
658 | .PP | |
659 | The tests are wholly and completely skipped. [10] This will work. | |
660 | .PP | |
661 | .Vb 2 | |
662 | \& SKIP: { | |
663 | \& skip("I don't wanna die!"); | |
664 | .Ve | |
665 | .PP | |
666 | .Vb 2 | |
667 | \& die, die, die, die, die; | |
668 | \& } | |
669 | .Ve | |
670 | .Sh "Todo tests" | |
671 | .IX Subsection "Todo tests" | |
672 | Thumbing through the Date::ICal man page, I came across this: | |
673 | .PP | |
674 | .Vb 1 | |
675 | \& ical | |
676 | .Ve | |
677 | .PP | |
678 | .Vb 1 | |
679 | \& $ical_string = $ical->ical; | |
680 | .Ve | |
681 | .PP | |
682 | .Vb 2 | |
683 | \& Retrieves, or sets, the date on the object, using any | |
684 | \& valid ICal date/time string. | |
685 | .Ve | |
686 | .PP | |
687 | \&\*(L"Retrieves or sets\*(R". Hmmm, didn't see a test for using \f(CW\*(C`ical()\*(C'\fR to set | |
688 | the date in the Date::ICal test suite. So I'll write one. | |
689 | .PP | |
690 | .Vb 2 | |
691 | \& use Test::More tests => 1; | |
692 | \& use Date::ICal; | |
693 | .Ve | |
694 | .PP | |
695 | .Vb 3 | |
696 | \& my $ical = Date::ICal->new; | |
697 | \& $ical->ical('20201231Z'); | |
698 | \& is( $ical->ical, '20201231Z', 'Setting via ical()' ); | |
699 | .Ve | |
700 | .PP | |
701 | run that and I get | |
702 | .PP | |
703 | .Vb 6 | |
704 | \& 1..1 | |
705 | \& not ok 1 - Setting via ical() | |
706 | \& # Failed test (- at line 6) | |
707 | \& # got: '20010814T233649Z' | |
708 | \& # expected: '20201231Z' | |
709 | \& # Looks like you failed 1 tests of 1. | |
710 | .Ve | |
711 | .PP | |
712 | Whoops! Looks like it's unimplemented. Let's assume we don't have | |
713 | the time to fix this. [11] Normally, you'd just comment out the test | |
714 | and put a note in a todo list somewhere. Instead, we're going to | |
715 | explicitly state \*(L"this test will fail\*(R" by wrapping it in a \f(CW\*(C`TODO\*(C'\fR block. | |
716 | .PP | |
717 | .Vb 1 | |
718 | \& use Test::More tests => 1; | |
719 | .Ve | |
720 | .PP | |
721 | .Vb 2 | |
722 | \& TODO: { | |
723 | \& local $TODO = 'ical($ical) not yet implemented'; | |
724 | .Ve | |
725 | .PP | |
726 | .Vb 2 | |
727 | \& my $ical = Date::ICal->new; | |
728 | \& $ical->ical('20201231Z'); | |
729 | .Ve | |
730 | .PP | |
731 | .Vb 2 | |
732 | \& is( $ical->ical, '20201231Z', 'Setting via ical()' ); | |
733 | \& } | |
734 | .Ve | |
735 | .PP | |
736 | Now when you run, it's a little different: | |
737 | .PP | |
738 | .Vb 4 | |
739 | \& 1..1 | |
740 | \& not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented | |
741 | \& # got: '20010822T201551Z' | |
742 | \& # expected: '20201231Z' | |
743 | .Ve | |
744 | .PP | |
745 | Test::More doesn't say \*(L"Looks like you failed 1 tests of 1\*(R". That '# | |
746 | \&\s-1TODO\s0' tells Test::Harness \*(L"this is supposed to fail\*(R" and it treats a | |
747 | failure as a successful test. So you can write tests even before | |
748 | you've fixed the underlying code. | |
749 | .PP | |
750 | If a \s-1TODO\s0 test passes, Test::Harness will report it \*(L"\s-1UNEXPECTEDLY\s0 | |
751 | \&\s-1SUCCEEDED\s0\*(R". When that happens, you simply remove the \s-1TODO\s0 block with | |
752 | \&\f(CW\*(C`local $TODO\*(C'\fR and turn it into a real test. | |
753 | .Sh "Testing with taint mode." | |
754 | .IX Subsection "Testing with taint mode." | |
755 | Taint mode is a funny thing. It's the globalest of all global | |
756 | features. Once you turn it on, it affects \fIall\fR code in your program | |
757 | and \fIall\fR modules used (and all the modules they use). If a single | |
758 | piece of code isn't taint clean, the whole thing explodes. With that | |
759 | in mind, it's very important to ensure your module works under taint | |
760 | mode. | |
761 | .PP | |
762 | It's very simple to have your tests run under taint mode. Just throw | |
763 | a \f(CW\*(C`\-T\*(C'\fR into the \f(CW\*(C`#!\*(C'\fR line. Test::Harness will read the switches | |
764 | in \f(CW\*(C`#!\*(C'\fR and use them to run your tests. | |
765 | .PP | |
766 | .Vb 1 | |
767 | \& #!/usr/bin/perl -Tw | |
768 | .Ve | |
769 | .PP | |
770 | .Vb 1 | |
771 | \& ...test normally here... | |
772 | .Ve | |
773 | .PP | |
774 | So when you say \f(CW\*(C`make test\*(C'\fR it will be run with taint mode and | |
775 | warnings on. | |
776 | .SH "FOOTNOTES" | |
777 | .IX Header "FOOTNOTES" | |
778 | .IP "1" 4 | |
779 | .IX Item "1" | |
780 | The first number doesn't really mean anything, but it has to be 1. | |
781 | It's the second number that's important. | |
782 | .IP "2" 4 | |
783 | .IX Item "2" | |
784 | For those following along at home, I'm using version 1.31. It has | |
785 | some bugs, which is good \*(-- we'll uncover them with our tests. | |
786 | .IP "3" 4 | |
787 | .IX Item "3" | |
788 | You can actually take this one step further and test the manual | |
789 | itself. Have a look at \fBTest::Inline\fR (formerly \fBPod::Tests\fR). | |
790 | .IP "4" 4 | |
791 | .IX Item "4" | |
792 | Yes, there's a mistake in the test suite. What! Me, contrived? | |
793 | .IP "5" 4 | |
794 | .IX Item "5" | |
795 | We'll get to testing the contents of lists later. | |
796 | .IP "6" 4 | |
797 | .IX Item "6" | |
798 | But what happens if your test program dies halfway through?! Since we | |
799 | didn't say how many tests we're going to run, how can we know it | |
800 | failed? No problem, Test::More employs some magic to catch that death | |
801 | and turn the test into a failure, even if every test passed up to that | |
802 | point. | |
803 | .IP "7" 4 | |
804 | .IX Item "7" | |
805 | I cleaned it up a little. | |
806 | .IP "8" 4 | |
807 | .IX Item "8" | |
808 | Most Operating Systems record time as the number of seconds since a | |
809 | certain date. This date is the beginning of the epoch. Unix's starts | |
810 | at midnight January 1st, 1970 \s-1GMT\s0. | |
811 | .IP "9" 4 | |
812 | .IX Item "9" | |
813 | MacOS's epoch is midnight January 1st, 1904. \s-1VMS\s0's is midnight, | |
814 | November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a | |
815 | problem. | |
816 | .IP "10" 4 | |
817 | .IX Item "10" | |
818 | As long as the code inside the \s-1SKIP\s0 block at least compiles. Please | |
819 | don't ask how. No, it's not a filter. | |
820 | .IP "11" 4 | |
821 | .IX Item "11" | |
822 | Do \s-1NOT\s0 be tempted to use \s-1TODO\s0 tests as a way to avoid fixing simple | |
823 | bugs! | |
824 | .SH "AUTHORS" | |
825 | .IX Header "AUTHORS" | |
826 | Michael G Schwern <schwern@pobox.com> and the perl-qa dancers! | |
827 | .SH "COPYRIGHT" | |
828 | .IX Header "COPYRIGHT" | |
829 | Copyright 2001 by Michael G Schwern <schwern@pobox.com>. | |
830 | .PP | |
831 | This documentation is free; you can redistribute it and/or modify it | |
832 | under the same terms as Perl itself. | |
833 | .PP | |
834 | Irrespective of its distribution, all code examples in these files | |
835 | are hereby placed into the public domain. You are permitted and | |
836 | encouraged to use this code in your own programs for fun | |
837 | or for profit as you see fit. A simple comment in the code giving | |
838 | credit would be courteous but is not required. |