Only 21 tests, skipping or not.
[p5sagit/p5-mst-13.2.git] / lib / Test / Tutorial.pod
CommitLineData
9dc10e63 1=head1 NAME
2
3Test::Tutorial - A tutorial about writing really basic tests
4
5=head1 DESCRIPTION
6
9dc10e63 7
0cd946aa 8B<AHHHHHHH!!!! NOT TESTING! Anything but testing!
9Beat me, whip me, send me to Detroit, but don't make
10me write tests!>
11
12B<*sob*>
13
14B<Besides, I don't know how to write the damned things.>
9dc10e63 15
9dc10e63 16
17Is this you? Is writing tests right up there with writing
0cd946aa 18documentation and having your fingernails pulled out? Did you open up
19a test and read
20
21 ######## We start with some black magic
22
23and decide that's quite enough for you?
24
25It's ok. That's all gone now. We've done all the black magic for
26you. And here are the tricks...
9dc10e63 27
28
29=head2 Nuts and bolts of testing.
30
31Here's the most basic test program.
32
33 #!/usr/bin/perl -w
34
35 print "1..1\n";
36
37 print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";
38
39since 1 + 1 is 2, it prints:
40
41 1..1
42 ok 1
43
44What this says is: C<1..1> "I'm going to run one test." [1] C<ok 1>
45"The first test passed". And that's about all magic there is to
46testing. Your basic unit of testing is the 'ok'. For each thing you
47test, an 'ok' is printed. Simple. Test::Harness interprets your test
48results to determine if you succeeded or failed (more on that later).
49
50Writing all these print statements rapidly gets tedious. Fortunately,
51there's Test::Simple. It has one function, ok().
52
53 #!/usr/bin/perl -w
54
55 use Test::Simple tests => 1;
56
57 ok( 1 + 1 == 2 );
58
59and that does the same thing as the code above. ok() is the backbone
60of Perl testing, and we'll be using it instead of roll-your-own from
61here on. If ok() gets a true value, the test passes. False, it
62fails.
63
64 #!/usr/bin/perl -w
65
66 use Test::Simple tests => 2;
67 ok( 1 + 1 == 2 );
68 ok( 2 + 2 == 5 );
69
70from that comes
71
72 1..2
73 ok 1
74 not ok 2
75 # Failed test (test.pl at line 5)
76 # Looks like you failed 1 tests of 2.
77
78C<1..2> "I'm going to run two tests." This number is used to ensure
79your test program ran all the way through and didn't die or skip some
80tests. C<ok 1> "The first test passed." C<not ok 2> "The second test
81failed". Test::Simple helpfuly prints out some extra commentary about
82your tests.
83
84It's not scary. Come, hold my hand. We're going to give an example
85of testing a module. For our example, we'll be testing a date
86library, Date::ICal. It's on CPAN, so download a copy and follow
87along. [2]
88
89
90=head2 Where to start?
91
92This is the hardest part of testing, where do you start? People often
93get overwhelmed at the apparent enormity of the task of testing a
94whole module. Best place to start is at the beginning. Date::ICal is
95an object-oriented module, and that means you start by making an
96object. So we test new().
97
98 #!/usr/bin/perl -w
99
100 use Test::Simple tests => 2;
101
102 use Date::ICal;
103
104 my $ical = Date::ICal->new; # create an object
105 ok( defined $ical ); # check that we got something
106 ok( $ical->isa('Date::ICal') ); # and it's the right class
107
108run that and you should get:
109
110 1..2
111 ok 1
112 ok 2
113
114congratulations, you've written your first useful test.
115
116
117=head2 Names
118
119That output isn't terribly descriptive, is it? When you have two
120tests you can figure out which one is #2, but what if you have 102?
121
122Each test can be given a little descriptive name as the second
123argument to ok().
124
125 use Test::Simple tests => 2;
126
127 ok( defined $ical, 'new() returned something' );
128 ok( $ical->isa('Date::ICal'), " and it's the right class" );
129
130So now you'd see...
131
132 1..2
133 ok 1 - new() returned something
134 ok 2 - and it's the right class
135
136
137=head2 Test the manual
138
139Simplest way to build up a decent testing suite is to just test what
140the manual says it does. [3] Let's pull something out of of the
141Date::ICal SYNOPSIS and test that all it's bits work.
142
143 #!/usr/bin/perl -w
144
145 use Test::Simple tests => 8;
146
147 use Date::ICal;
148
149 $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
150 hour => 16, min => 12, sec => 47,
151 tz => '0530' );
152
153 ok( defined $ical, 'new() returned something' );
154 ok( $ical->isa('Date::ICal'), " and it's the right class" );
155 ok( $ical->sec == 47, ' sec()' );
156 ok( $ical->min == 12, ' min()' );
157 ok( $ical->hour == 16, ' hour()' );
158 ok( $ical->day == 17, ' day()' );
159 ok( $ical->month == 10, ' month()' );
160 ok( $ical->year == 1964, ' year()' );
161
162run that and you get:
163
164 1..8
165 ok 1 - new() returned something
166 ok 2 - and it's the right class
167 ok 3 - sec()
168 ok 4 - min()
169 ok 5 - hour()
170 not ok 6 - day()
171 # Failed test (- at line 16)
172 ok 7 - month()
173 ok 8 - year()
174 # Looks like you failed 1 tests of 8.
175
176Whoops, a failure! [4] Test::Simple helpfully lets us know on what line
177the failure occured, but not much else. We were supposed to get 17,
178but we didn't. What did we get?? Dunno. We'll have to re-run the
179test in the debugger or throw in some print statements to find out.
180
181Instead, we'll switch from Test::Simple to Test::More. Test::More
182does everything Test::Simple does, and more! In fact, Test::More does
183things I<exactly> the way Test::Simple does. You can literally swap
184Test::Simple out and put Test::More in its place. That's just what
185we're going to do.
186
187Test::More provides more informative ways to say 'ok'. ok() is nice
188and generic, you can write almost any test with it, but it can't tell
189you what went wrong. For that, we use the is() function.
190
191 #!/usr/bin/perl -w
192
193 use Test::More tests => 8;
194
195 use Date::ICal;
196
197 $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
198 hour => 16, min => 12, sec => 47,
199 tz => '0530' );
200
201 ok( defined $ical, 'new() returned something' );
202 ok( $ical->isa('Date::ICal'), " and it's the right class" );
203 is( $ical->sec, 47, ' sec()' );
204 is( $ical->min, 12, ' min()' );
205 is( $ical->hour, 16, ' hour()' );
206 is( $ical->day, 17, ' day()' );
207 is( $ical->month, 10, ' month()' );
208 is( $ical->year, 1964, ' year()' );
209
210"Is C<$ical->sec> 47?" "Is C<$ical->min> 12?" With is() in place,
211you get some more information
212
213 1..8
214 ok 1 - new() returned something
215 ok 2 - and it's the right class
216 ok 3 - sec()
217 ok 4 - min()
218 ok 5 - hour()
219 not ok 6 - day()
220 # Failed test (- at line 16)
221 # got: '16'
222 # expected: '17'
223 ok 7 - month()
224 ok 8 - year()
225 # Looks like you failed 1 tests of 8.
226
227letting us know that $ical->day returned 16, but we expected 17. A
228quick check shows that the code is working fine, we made a mistake
229when writing up the tests. Just change it to:
230
231 is( $ical->day, 16, ' day()' );
232
233and everything works.
234
235So any time you're doing a "this equals that" sort of test, use is().
236It even works on arrays. The test is always in scalar context, so you
237can test how many elements are in a list this way. [5]
238
239 is( @foo, 5, 'foo has 5 elements' );
240
241
242=head2 Sometimes the tests are wrong
243
244Which brings us to a very important lesson. Code has bugs. Tests are
245code. Ergo, tests have bugs. A failing test could mean a bug in the
246code, but don't discount the possibility that the test is wrong.
247
248On the flip side, don't be tempted to prematurely declare a test
249incorrect just because you're having trouble finding the bug.
250Invalidating a test isn't something to be taken lightly, and don't use
251it as a cop out to avoid work.
252
253
254=head2 Testing lots of values
255
256We're going to be wanting to test a lot of dates here, trying to trick
257the code with lots of different edge cases. Does it work before 1970?
258After 2038? Before 1904? Do years after 10,000 give it trouble?
259Does it get leap years right? We could keep repeating the code above,
260or we could set up a little try/expect loop.
261
262 use Test::More tests => 32;
263 use Date::ICal;
264
265 my %ICal_Dates = (
266 # An ICal string And the year, month, date
267 # hour, minute and second we expect.
268 '19971024T120000' => # from the docs.
269 [ 1997, 10, 24, 12, 0, 0 ],
270 '20390123T232832' => # after the Unix epoch
271 [ 2039, 1, 23, 23, 28, 32 ],
272 '19671225T000000' => # before the Unix epoch
273 [ 1967, 12, 25, 0, 0, 0 ],
274 '18990505T232323' => # before the MacOS epoch
275 [ 1899, 5, 5, 23, 23, 23 ],
276 );
277
278
279 while( my($ical_str, $expect) = each %ICal_Dates ) {
280 my $ical = Date::ICal->new( ical => $ical_str );
281
282 ok( defined $ical, "new(ical => '$ical_str')" );
283 ok( $ical->isa('Date::ICal'), " and it's the right class" );
284
285 is( $ical->year, $expect->[0], ' year()' );
286 is( $ical->month, $expect->[1], ' month()' );
287 is( $ical->day, $expect->[2], ' day()' );
288 is( $ical->hour, $expect->[3], ' hour()' );
289 is( $ical->min, $expect->[4], ' min()' );
290 is( $ical->sec, $expect->[5], ' sec()' );
291 }
292
293So now we can test bunches of dates by just adding them to
294%ICal_Dates. Now that it's less work to test with more dates, you'll
295be inclined to just throw more in as you think of them.
296Only problem is, every time we add to that we have to keep adjusting
297the C<use Test::More tests => ##> line. That can rapidly get
298annoying. Instead we use 'no_plan'. This means we're just running
299some tests, don't know how many. [6]
300
301 use Test::More 'no_plan'; # instead of tests => 32
302
303now we can just add tests and not have to do all sorts of math to
304figure out how many we're running.
305
306
307=head2 Informative names
308
309Take a look at this line here
310
311 ok( defined $ical, "new(ical => '$ical_str')" );
312
313we've added more detail about what we're testing and the ICal string
314itself we're trying out to the name. So you get results like:
315
316 ok 25 - new(ical => '19971024T120000')
317 ok 26 - and it's the right class
318 ok 27 - year()
319 ok 28 - month()
320 ok 29 - day()
321 ok 30 - hour()
322 ok 31 - min()
323 ok 32 - sec()
324
325if something in there fails, you'll know which one it was and that
326will make tracking down the problem easier. So try to put a bit of
327debugging information into the test names.
328
329
330=head2 Skipping tests
331
332Poking around in the existing Date::ICal tests, I found this in
333t/01sanity.t [7]
334
335 #!/usr/bin/perl -w
336
337 use Test::More tests => 7;
338 use Date::ICal;
339
340 # Make sure epoch time is being handled sanely.
341 my $t1 = Date::ICal->new( epoch => 0 );
342 is( $t1->epoch, 0, "Epoch time of 0" );
343
344 # XXX This will only work on unix systems.
345 is( $t1->ical, '19700101Z', " epoch to ical" );
346
347 is( $t1->year, 1970, " year()" );
348 is( $t1->month, 1, " month()" );
349 is( $t1->day, 1, " day()" );
350
351 # like the tests above, but starting with ical instead of epoch
352 my $t2 = Date::ICal->new( ical => '19700101Z' );
353 is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );
354
355 is( $t2->epoch, 0, " and back to ICal" );
356
357The beginning of the epoch is different on most non-Unix operating
358systems [8]. Even though Perl smooths out the differences for the most
359part, certain ports do it differently. MacPerl is one off the top of
360my head. [9] We I<know> this will never work on MacOS. So rather than
361just putting a comment in the test, we can explicitly say it's never
362going to work and skip the test.
363
364 use Test::More tests => 7;
365 use Date::ICal;
366
367 # Make sure epoch time is being handled sanely.
368 my $t1 = Date::ICal->new( epoch => 0 );
369 is( $t1->epoch, 0, "Epoch time of 0" );
370
371 SKIP: {
372 skip('epoch to ICal not working on MacOS', 6)
373 if $^O eq 'MacOS';
374
375 is( $t1->ical, '19700101Z', " epoch to ical" );
376
377 is( $t1->year, 1970, " year()" );
378 is( $t1->month, 1, " month()" );
379 is( $t1->day, 1, " day()" );
380
381 # like the tests above, but starting with ical instead of epoch
382 my $t2 = Date::ICal->new( ical => '19700101Z' );
383 is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );
384
385 is( $t2->epoch, 0, " and back to ICal" );
386 }
387
388A little bit of magic happens here. When running on anything but
389MacOS, all the tests run normally. But when on MacOS, skip() causes
390the entire contents of the SKIP block to be jumped over. It's never
391run. Instead, it prints special output that tells Test::Harness that
392the tests have been skipped.
393
394 1..7
395 ok 1 - Epoch time of 0
396 ok 2 # skip epoch to ICal not working on MacOS
397 ok 3 # skip epoch to ICal not working on MacOS
398 ok 4 # skip epoch to ICal not working on MacOS
399 ok 5 # skip epoch to ICal not working on MacOS
400 ok 6 # skip epoch to ICal not working on MacOS
401 ok 7 # skip epoch to ICal not working on MacOS
402
403This means your tests won't fail on MacOS. This means less emails
404from MacPerl users telling you about failing tests that you know will
405never work. You've got to be careful with skip tests. These are for
406tests which don't work and B<never will>. It is not for skipping
407genuine bugs (we'll get to that in a moment).
408
409The tests are wholely and completely skipped. [10] This will work.
410
411 SKIP: {
412 skip("I don't wanna die!");
413
414 die, die, die, die, die;
415 }
416
417
418=head2 Todo tests
419
420Thumbing through the Date::ICal man page, I came across this:
421
422 ical
423
424 $ical_string = $ical->ical;
425
426 Retrieves, or sets, the date on the object, using any
427 valid ICal date/time string.
428
429"Retrieves or sets". Hmmm, didn't see a test for using ical() to set
430the date in the Date::ICal test suite. So I'll write one.
431
432 use Test::More tests => 1;
433
434 my $ical = Date::ICal->new;
435 $ical->ical('20201231Z');
436 is( $ical->ical, '20201231Z', 'Setting via ical()' );
437
438run that and I get
439
440 1..1
441 not ok 1 - Setting via ical()
442 # Failed test (- at line 6)
443 # got: '20010814T233649Z'
444 # expected: '20201231Z'
445 # Looks like you failed 1 tests of 1.
446
447Whoops! Looks like it's unimplemented. Let's assume we don't have
448the time to fix this. [11] Normally, you'd just comment out the test
449and put a note in a todo list somewhere. Instead, we're going to
450explicitly state "this test will fail" by wraping it in a TODO block.
451
452 use Test::More tests => 1;
453
454 TODO: {
455 local $TODO = 'ical($ical) not yet implemented';
456
457 my $ical = Date::ICal->new;
458 $ical->ical('20201231Z');
459
460 is( $ical->ical, '20201231Z', 'Setting via ical()' );
461 }
462
463Now when you run, it's a little different:
464
465 1..1
466 not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented
467 # got: '20010822T201551Z'
468 # expected: '20201231Z'
469
470Test::More doesn't say "Looks like you failed 1 tests of 1". That '#
471TODO' tells Test::Harness "this is supposed to fail" and it treats a
472failure as a successful test. So you can write tests even before
473you've fixed the underlying code.
474
475If a TODO test passes, Test::Harness will report it "UNEXPECTEDLY
476SUCCEEDED". When that happens, you simply remove the TODO block and
477C<local $TODO> and turn it into a real test.
478
479
480=head2 Testing with taint mode.
481
482Taint mode is a funny thing. It's the globalest of all global
483features. Once you turn it on it effects B<all> code in your program
484and B<all> modules used (and all the modules they use). If a single
485piece of code isn't taint clean, the whole thing explodes. With that
486in mind, it's very important to ensure your module works under taint
487mode.
488
489It's very simple to have your tests run under taint mode. Just throw
490a -T into the #! line. Test::Harness will read the switches in #! and
491use them to run your tests.
492
493 #!/usr/bin/perl -Tw
494
495 use Test::More 'no_plan';
496
497 ...test normally here...
498
499So when you say "make test" it will be run with taint mode and
500warnings on.
501
502
503=head1 FOOTNOTES
504
505=over 4
506
507=item 1
508
509The first number doesn't really mean anything, but it has to be 1.
510It's the second number that's important.
511
512=item 2
513
514For those following along at home, I'm using version 1.31. It has
515some bugs, which is good -- we'll uncover them with our tests.
516
517=item 3
518
519You can actually take this one step further and test the manual
520itself. Have a look at Pod::Tests (soon to be Test::Inline).
521
522=item 4
523
524Yes, there's a mistake in the test suite. What! Me, contrived?
525
526=item 5
527
528We'll get to testing the contents of lists later.
529
530=item 6
531
532But what happens if your test program dies halfway through?! Since we
533didn't say how many tests we're going to run, how can we know it
534failed? No problem, Test::More employs some magic to catch that death
535and turn the test into a failure, even if every test passed up to that
536point.
537
538=item 7
539
540I cleaned it up a little.
541
542=item 8
543
544Most Operating Systems record time as the number of seconds since a
545certain date. This date is the beginning of the epoch. Unix's starts
546at midnight January 1st, 1970 GMT.
547
548=item 9
549
550MacOS's epoch is midnight January 1st, 1904. VMS's is midnight,
551November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a
552problem.
553
554=item 10
555
556As long as the code inside the SKIP block at least compiles. Please
557don't ask how. No, it's not a filter.
558
559=item 11
560
561Do NOT be tempted to use TODO tests as a way to avoid fixing simple
562bugs!
563
564=back