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