xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test/Tutorial.pod (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1b39c5158Smillert=head1 NAME
2b39c5158Smillert
3b39c5158SmillertTest::Tutorial - A tutorial about writing really basic tests
4b39c5158Smillert
5b39c5158Smillert=head1 DESCRIPTION
6b39c5158Smillert
7b39c5158Smillert
8b39c5158SmillertI<AHHHHHHH!!!!  NOT TESTING!  Anything but testing!
9b39c5158SmillertBeat me, whip me, send me to Detroit, but don't make
10b39c5158Smillertme write tests!>
11b39c5158Smillert
12b39c5158SmillertI<*sob*>
13b39c5158Smillert
14b39c5158SmillertI<Besides, I don't know how to write the damned things.>
15b39c5158Smillert
16b39c5158Smillert
17b39c5158SmillertIs this you?  Is writing tests right up there with writing
18b39c5158Smillertdocumentation and having your fingernails pulled out?  Did you open up
19b39c5158Smillerta test and read
20b39c5158Smillert
21b39c5158Smillert    ######## We start with some black magic
22b39c5158Smillert
23b39c5158Smillertand decide that's quite enough for you?
24b39c5158Smillert
25b39c5158SmillertIt's ok.  That's all gone now.  We've done all the black magic for
26b39c5158Smillertyou.  And here are the tricks...
27b39c5158Smillert
28b39c5158Smillert
29b39c5158Smillert=head2 Nuts and bolts of testing.
30b39c5158Smillert
31b39c5158SmillertHere's the most basic test program.
32b39c5158Smillert
33b39c5158Smillert    #!/usr/bin/perl -w
34b39c5158Smillert
35b39c5158Smillert    print "1..1\n";
36b39c5158Smillert
37b39c5158Smillert    print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";
38b39c5158Smillert
396fb12b70Safresh1Because 1 + 1 is 2, it prints:
40b39c5158Smillert
41b39c5158Smillert    1..1
42b39c5158Smillert    ok 1
43b39c5158Smillert
44b39c5158SmillertWhat this says is: C<1..1> "I'm going to run one test." [1] C<ok 1>
45b39c5158Smillert"The first test passed".  And that's about all magic there is to
46b39c5158Smillerttesting.  Your basic unit of testing is the I<ok>.  For each thing you
476fb12b70Safresh1test, an C<ok> is printed.  Simple.  L<Test::Harness> interprets your test
48b39c5158Smillertresults to determine if you succeeded or failed (more on that later).
49b39c5158Smillert
50b39c5158SmillertWriting all these print statements rapidly gets tedious.  Fortunately,
516fb12b70Safresh1there's L<Test::Simple>.  It has one function, C<ok()>.
52b39c5158Smillert
53b39c5158Smillert    #!/usr/bin/perl -w
54b39c5158Smillert
55b39c5158Smillert    use Test::Simple tests => 1;
56b39c5158Smillert
57b39c5158Smillert    ok( 1 + 1 == 2 );
58b39c5158Smillert
596fb12b70Safresh1That does the same thing as the previous code.  C<ok()> is the backbone
60b39c5158Smillertof Perl testing, and we'll be using it instead of roll-your-own from
61b39c5158Smillerthere on.  If C<ok()> gets a true value, the test passes.  False, it
62b39c5158Smillertfails.
63b39c5158Smillert
64b39c5158Smillert    #!/usr/bin/perl -w
65b39c5158Smillert
66b39c5158Smillert    use Test::Simple tests => 2;
67b39c5158Smillert    ok( 1 + 1 == 2 );
68b39c5158Smillert    ok( 2 + 2 == 5 );
69b39c5158Smillert
706fb12b70Safresh1From that comes:
71b39c5158Smillert
72b39c5158Smillert    1..2
73b39c5158Smillert    ok 1
74b39c5158Smillert    not ok 2
75b39c5158Smillert    #     Failed test (test.pl at line 5)
76b39c5158Smillert    # Looks like you failed 1 tests of 2.
77b39c5158Smillert
786fb12b70Safresh1C<1..2> "I'm going to run two tests."  This number is a I<plan>. It helps to
796fb12b70Safresh1ensure your test program ran all the way through and didn't die or skip some
806fb12b70Safresh1tests.  C<ok 1> "The first test passed."  C<not ok 2> "The second test failed".
816fb12b70Safresh1Test::Simple helpfully prints out some extra commentary about your tests.
82b39c5158Smillert
83b39c5158SmillertIt's not scary.  Come, hold my hand.  We're going to give an example
84b39c5158Smillertof testing a module.  For our example, we'll be testing a date
856fb12b70Safresh1library, L<Date::ICal>.  It's on CPAN, so download a copy and follow
86b39c5158Smillertalong. [2]
87b39c5158Smillert
88b39c5158Smillert
89b39c5158Smillert=head2 Where to start?
90b39c5158Smillert
916fb12b70Safresh1This is the hardest part of testing, where do you start?  People often get
926fb12b70Safresh1overwhelmed at the apparent enormity of the task of testing a whole module.
93b8851fccSafresh1The best place to start is at the beginning.  L<Date::ICal> is an
946fb12b70Safresh1object-oriented module, and that means you start by making an object.  Test
956fb12b70Safresh1C<new()>.
96b39c5158Smillert
97b39c5158Smillert    #!/usr/bin/perl -w
98b39c5158Smillert
996fb12b70Safresh1    # assume these two lines are in all subsequent examples
1006fb12b70Safresh1    use strict;
1016fb12b70Safresh1    use warnings;
1026fb12b70Safresh1
103b39c5158Smillert    use Test::Simple tests => 2;
104b39c5158Smillert
105b39c5158Smillert    use Date::ICal;
106b39c5158Smillert
107b39c5158Smillert    my $ical = Date::ICal->new;         # create an object
108b39c5158Smillert    ok( defined $ical );                # check that we got something
109b39c5158Smillert    ok( $ical->isa('Date::ICal') );     # and it's the right class
110b39c5158Smillert
1116fb12b70Safresh1Run that and you should get:
112b39c5158Smillert
113b39c5158Smillert    1..2
114b39c5158Smillert    ok 1
115b39c5158Smillert    ok 2
116b39c5158Smillert
1176fb12b70Safresh1Congratulations! You've written your first useful test.
118b39c5158Smillert
119b39c5158Smillert
120b39c5158Smillert=head2 Names
121b39c5158Smillert
1226fb12b70Safresh1That output isn't terribly descriptive, is it?  When you have two tests you can
1236fb12b70Safresh1figure out which one is #2, but what if you have 102 tests?
124b39c5158Smillert
125b39c5158SmillertEach test can be given a little descriptive name as the second
126b39c5158Smillertargument to C<ok()>.
127b39c5158Smillert
128b39c5158Smillert    use Test::Simple tests => 2;
129b39c5158Smillert
130b39c5158Smillert    ok( defined $ical,              'new() returned something' );
131b39c5158Smillert    ok( $ical->isa('Date::ICal'),   "  and it's the right class" );
132b39c5158Smillert
1336fb12b70Safresh1Now you'll see:
134b39c5158Smillert
135b39c5158Smillert    1..2
136b39c5158Smillert    ok 1 - new() returned something
137b39c5158Smillert    ok 2 -   and it's the right class
138b39c5158Smillert
139b39c5158Smillert
140b39c5158Smillert=head2 Test the manual
141b39c5158Smillert
1426fb12b70Safresh1The simplest way to build up a decent testing suite is to just test what
143b39c5158Smillertthe manual says it does. [3] Let's pull something out of the
144b39c5158SmillertL<Date::ICal/SYNOPSIS> and test that all its bits work.
145b39c5158Smillert
146b39c5158Smillert    #!/usr/bin/perl -w
147b39c5158Smillert
148b39c5158Smillert    use Test::Simple tests => 8;
149b39c5158Smillert
150b39c5158Smillert    use Date::ICal;
151b39c5158Smillert
152b39c5158Smillert    $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
153b39c5158Smillert                             hour => 16,   min   => 12, sec => 47,
154b39c5158Smillert                             tz   => '0530' );
155b39c5158Smillert
156b39c5158Smillert    ok( defined $ical,            'new() returned something' );
157b39c5158Smillert    ok( $ical->isa('Date::ICal'), "  and it's the right class" );
158b39c5158Smillert    ok( $ical->sec   == 47,       '  sec()'   );
159b39c5158Smillert    ok( $ical->min   == 12,       '  min()'   );
160b39c5158Smillert    ok( $ical->hour  == 16,       '  hour()'  );
161b39c5158Smillert    ok( $ical->day   == 17,       '  day()'   );
162b39c5158Smillert    ok( $ical->month == 10,       '  month()' );
163b39c5158Smillert    ok( $ical->year  == 1964,     '  year()'  );
164b39c5158Smillert
1656fb12b70Safresh1Run that and you get:
166b39c5158Smillert
167b39c5158Smillert    1..8
168b39c5158Smillert    ok 1 - new() returned something
169b39c5158Smillert    ok 2 -   and it's the right class
170b39c5158Smillert    ok 3 -   sec()
171b39c5158Smillert    ok 4 -   min()
172b39c5158Smillert    ok 5 -   hour()
173b39c5158Smillert    not ok 6 -   day()
174b39c5158Smillert    #     Failed test (- at line 16)
175b39c5158Smillert    ok 7 -   month()
176b39c5158Smillert    ok 8 -   year()
177b39c5158Smillert    # Looks like you failed 1 tests of 8.
178b39c5158Smillert
179b8851fccSafresh1Whoops, a failure! [4] L<Test::Simple> helpfully lets us know on what line the
1806fb12b70Safresh1failure occurred, but not much else.  We were supposed to get 17, but we
1816fb12b70Safresh1didn't.  What did we get??  Dunno.  You could re-run the test in the debugger
1826fb12b70Safresh1or throw in some print statements to find out.
183b39c5158Smillert
184b8851fccSafresh1Instead, switch from L<Test::Simple> to L<Test::More>.  L<Test::More>
185b8851fccSafresh1does everything L<Test::Simple> does, and more!  In fact, L<Test::More> does
186b8851fccSafresh1things I<exactly> the way L<Test::Simple> does.  You can literally swap
187b8851fccSafresh1L<Test::Simple> out and put L<Test::More> in its place.  That's just what
188b39c5158Smillertwe're going to do.
189b39c5158Smillert
190b8851fccSafresh1L<Test::More> does more than L<Test::Simple>.  The most important difference at
1916fb12b70Safresh1this point is it provides more informative ways to say "ok".  Although you can
1926fb12b70Safresh1write almost any test with a generic C<ok()>, it can't tell you what went
1936fb12b70Safresh1wrong.  The C<is()> function lets us declare that something is supposed to be
1946fb12b70Safresh1the same as something else:
195b39c5158Smillert
196b39c5158Smillert    use Test::More tests => 8;
197b39c5158Smillert
198b39c5158Smillert    use Date::ICal;
199b39c5158Smillert
200b39c5158Smillert    $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
201b39c5158Smillert                             hour => 16,   min   => 12, sec => 47,
202b39c5158Smillert                             tz   => '0530' );
203b39c5158Smillert
204b39c5158Smillert    ok( defined $ical,            'new() returned something' );
205b39c5158Smillert    ok( $ical->isa('Date::ICal'), "  and it's the right class" );
206b39c5158Smillert    is( $ical->sec,     47,       '  sec()'   );
207b39c5158Smillert    is( $ical->min,     12,       '  min()'   );
208b39c5158Smillert    is( $ical->hour,    16,       '  hour()'  );
209b39c5158Smillert    is( $ical->day,     17,       '  day()'   );
210b39c5158Smillert    is( $ical->month,   10,       '  month()' );
211b39c5158Smillert    is( $ical->year,    1964,     '  year()'  );
212b39c5158Smillert
213b8851fccSafresh1"Is C<< $ical->sec >> 47?"  "Is C<< $ical->min >> 12?"  With C<is()> in place,
2146fb12b70Safresh1you get more information:
215b39c5158Smillert
216b39c5158Smillert    1..8
217b39c5158Smillert    ok 1 - new() returned something
218b39c5158Smillert    ok 2 -   and it's the right class
219b39c5158Smillert    ok 3 -   sec()
220b39c5158Smillert    ok 4 -   min()
221b39c5158Smillert    ok 5 -   hour()
222b39c5158Smillert    not ok 6 -   day()
223b39c5158Smillert    #     Failed test (- at line 16)
224b39c5158Smillert    #          got: '16'
225b39c5158Smillert    #     expected: '17'
226b39c5158Smillert    ok 7 -   month()
227b39c5158Smillert    ok 8 -   year()
228b39c5158Smillert    # Looks like you failed 1 tests of 8.
229b39c5158Smillert
230b8851fccSafresh1Aha. C<< $ical->day >> returned 16, but we expected 17.  A
231b39c5158Smillertquick check shows that the code is working fine, we made a mistake
2326fb12b70Safresh1when writing the tests.  Change it to:
233b39c5158Smillert
234b39c5158Smillert    is( $ical->day,     16,       '  day()'   );
235b39c5158Smillert
2366fb12b70Safresh1... and everything works.
237b39c5158Smillert
2386fb12b70Safresh1Any time you're doing a "this equals that" sort of test, use C<is()>.
239b39c5158SmillertIt even works on arrays.  The test is always in scalar context, so you
2406fb12b70Safresh1can test how many elements are in an array this way. [5]
241b39c5158Smillert
242b39c5158Smillert    is( @foo, 5, 'foo has 5 elements' );
243b39c5158Smillert
244b39c5158Smillert
245b39c5158Smillert=head2 Sometimes the tests are wrong
246b39c5158Smillert
2476fb12b70Safresh1This brings up a very important lesson.  Code has bugs.  Tests are
248b39c5158Smillertcode.  Ergo, tests have bugs.  A failing test could mean a bug in the
249b39c5158Smillertcode, but don't discount the possibility that the test is wrong.
250b39c5158Smillert
251b39c5158SmillertOn the flip side, don't be tempted to prematurely declare a test
252b39c5158Smillertincorrect just because you're having trouble finding the bug.
253b39c5158SmillertInvalidating a test isn't something to be taken lightly, and don't use
254b39c5158Smillertit as a cop out to avoid work.
255b39c5158Smillert
256b39c5158Smillert
257b39c5158Smillert=head2 Testing lots of values
258b39c5158Smillert
259b39c5158SmillertWe're going to be wanting to test a lot of dates here, trying to trick
260b39c5158Smillertthe code with lots of different edge cases.  Does it work before 1970?
261b39c5158SmillertAfter 2038?  Before 1904?  Do years after 10,000 give it trouble?
262b39c5158SmillertDoes it get leap years right?  We could keep repeating the code above,
263b39c5158Smillertor we could set up a little try/expect loop.
264b39c5158Smillert
265b39c5158Smillert    use Test::More tests => 32;
266b39c5158Smillert    use Date::ICal;
267b39c5158Smillert
268b39c5158Smillert    my %ICal_Dates = (
269b39c5158Smillert            # An ICal string     And the year, month, day
270b39c5158Smillert            #                    hour, minute and second we expect.
271b39c5158Smillert            '19971024T120000' =>    # from the docs.
272b39c5158Smillert                                [ 1997, 10, 24, 12,  0,  0 ],
273b39c5158Smillert            '20390123T232832' =>    # after the Unix epoch
274b39c5158Smillert                                [ 2039,  1, 23, 23, 28, 32 ],
275b39c5158Smillert            '19671225T000000' =>    # before the Unix epoch
276b39c5158Smillert                                [ 1967, 12, 25,  0,  0,  0 ],
277b39c5158Smillert            '18990505T232323' =>    # before the MacOS epoch
278b39c5158Smillert                                [ 1899,  5,  5, 23, 23, 23 ],
279b39c5158Smillert    );
280b39c5158Smillert
281b39c5158Smillert
282b39c5158Smillert    while( my($ical_str, $expect) = each %ICal_Dates ) {
283b39c5158Smillert        my $ical = Date::ICal->new( ical => $ical_str );
284b39c5158Smillert
285b39c5158Smillert        ok( defined $ical,            "new(ical => '$ical_str')" );
286b39c5158Smillert        ok( $ical->isa('Date::ICal'), "  and it's the right class" );
287b39c5158Smillert
288b39c5158Smillert        is( $ical->year,    $expect->[0],     '  year()'  );
289b39c5158Smillert        is( $ical->month,   $expect->[1],     '  month()' );
290b39c5158Smillert        is( $ical->day,     $expect->[2],     '  day()'   );
291b39c5158Smillert        is( $ical->hour,    $expect->[3],     '  hour()'  );
292b39c5158Smillert        is( $ical->min,     $expect->[4],     '  min()'   );
293b39c5158Smillert        is( $ical->sec,     $expect->[5],     '  sec()'   );
294b39c5158Smillert    }
295b39c5158Smillert
2966fb12b70Safresh1Now we can test bunches of dates by just adding them to
297b39c5158SmillertC<%ICal_Dates>.  Now that it's less work to test with more dates, you'll
298b39c5158Smillertbe inclined to just throw more in as you think of them.
299b39c5158SmillertOnly problem is, every time we add to that we have to keep adjusting
300*9f11ffb7Safresh1the C<< use Test::More tests => ## >> line.  That can rapidly get
3016fb12b70Safresh1annoying.  There are ways to make this work better.
302b39c5158Smillert
303b39c5158SmillertFirst, we can calculate the plan dynamically using the C<plan()>
304b39c5158Smillertfunction.
305b39c5158Smillert
306b39c5158Smillert    use Test::More;
307b39c5158Smillert    use Date::ICal;
308b39c5158Smillert
309b39c5158Smillert    my %ICal_Dates = (
310b39c5158Smillert        ...same as before...
311b39c5158Smillert    );
312b39c5158Smillert
313b39c5158Smillert    # For each key in the hash we're running 8 tests.
314b39c5158Smillert    plan tests => keys(%ICal_Dates) * 8;
315b39c5158Smillert
316b39c5158Smillert    ...and then your tests...
317b39c5158Smillert
3186fb12b70Safresh1To be even more flexible, use C<done_testing>.  This means we're just
319b39c5158Smillertrunning some tests, don't know how many. [6]
320b39c5158Smillert
3216fb12b70Safresh1    use Test::More;   # instead of tests => 32
322b39c5158Smillert
3236fb12b70Safresh1    ... # tests here
3246fb12b70Safresh1
3256fb12b70Safresh1    done_testing();   # reached the end safely
3266fb12b70Safresh1
327b8851fccSafresh1If you don't specify a plan, L<Test::More> expects to see C<done_testing()>
3286fb12b70Safresh1before your program exits. It will warn you if you forget it. You can give
3296fb12b70Safresh1C<done_testing()> an optional number of tests you expected to run, and if the
330b8851fccSafresh1number ran differs, L<Test::More> will give you another kind of warning.
331b39c5158Smillert
332b39c5158Smillert
333b39c5158Smillert=head2 Informative names
334b39c5158Smillert
3356fb12b70Safresh1Take a look at the line:
336b39c5158Smillert
337b39c5158Smillert    ok( defined $ical,            "new(ical => '$ical_str')" );
338b39c5158Smillert
3396fb12b70Safresh1We've added more detail about what we're testing and the ICal string
340b39c5158Smillertitself we're trying out to the name.  So you get results like:
341b39c5158Smillert
342b39c5158Smillert    ok 25 - new(ical => '19971024T120000')
343b39c5158Smillert    ok 26 -   and it's the right class
344b39c5158Smillert    ok 27 -   year()
345b39c5158Smillert    ok 28 -   month()
346b39c5158Smillert    ok 29 -   day()
347b39c5158Smillert    ok 30 -   hour()
348b39c5158Smillert    ok 31 -   min()
349b39c5158Smillert    ok 32 -   sec()
350b39c5158Smillert
3516fb12b70Safresh1If something in there fails, you'll know which one it was and that
3526fb12b70Safresh1will make tracking down the problem easier.  Try to put a bit of
353b39c5158Smillertdebugging information into the test names.
354b39c5158Smillert
355b39c5158SmillertDescribe what the tests test, to make debugging a failed test easier
356b39c5158Smillertfor you or for the next person who runs your test.
357b39c5158Smillert
358b39c5158Smillert
359b39c5158Smillert=head2 Skipping tests
360b39c5158Smillert
361*9f11ffb7Safresh1Poking around in the existing L<Date::ICal> tests, I found this in
362b39c5158SmillertF<t/01sanity.t> [7]
363b39c5158Smillert
364b39c5158Smillert    #!/usr/bin/perl -w
365b39c5158Smillert
366b39c5158Smillert    use Test::More tests => 7;
367b39c5158Smillert    use Date::ICal;
368b39c5158Smillert
369b39c5158Smillert    # Make sure epoch time is being handled sanely.
370b39c5158Smillert    my $t1 = Date::ICal->new( epoch => 0 );
371b39c5158Smillert    is( $t1->epoch, 0,          "Epoch time of 0" );
372b39c5158Smillert
373b39c5158Smillert    # XXX This will only work on unix systems.
374b39c5158Smillert    is( $t1->ical, '19700101Z', "  epoch to ical" );
375b39c5158Smillert
376b39c5158Smillert    is( $t1->year,  1970,       "  year()"  );
377b39c5158Smillert    is( $t1->month, 1,          "  month()" );
378b39c5158Smillert    is( $t1->day,   1,          "  day()"   );
379b39c5158Smillert
380b39c5158Smillert    # like the tests above, but starting with ical instead of epoch
381b39c5158Smillert    my $t2 = Date::ICal->new( ical => '19700101Z' );
382b39c5158Smillert    is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );
383b39c5158Smillert
384b39c5158Smillert    is( $t2->epoch, 0,          "  and back to ICal" );
385b39c5158Smillert
3866fb12b70Safresh1The beginning of the epoch is different on most non-Unix operating systems [8].
3876fb12b70Safresh1Even though Perl smooths out the differences for the most part, certain ports
3886fb12b70Safresh1do it differently.  MacPerl is one off the top of my head. [9]  Rather than
3896fb12b70Safresh1putting a comment in the test and hoping someone will read the test while
3906fb12b70Safresh1debugging the failure, we can explicitly say it's never going to work and skip
3916fb12b70Safresh1the test.
392b39c5158Smillert
393b39c5158Smillert    use Test::More tests => 7;
394b39c5158Smillert    use Date::ICal;
395b39c5158Smillert
396b39c5158Smillert    # Make sure epoch time is being handled sanely.
397b39c5158Smillert    my $t1 = Date::ICal->new( epoch => 0 );
398b39c5158Smillert    is( $t1->epoch, 0,          "Epoch time of 0" );
399b39c5158Smillert
400b39c5158Smillert    SKIP: {
401b39c5158Smillert        skip('epoch to ICal not working on Mac OS', 6)
402b39c5158Smillert            if $^O eq 'MacOS';
403b39c5158Smillert
404b39c5158Smillert        is( $t1->ical, '19700101Z', "  epoch to ical" );
405b39c5158Smillert
406b39c5158Smillert        is( $t1->year,  1970,       "  year()"  );
407b39c5158Smillert        is( $t1->month, 1,          "  month()" );
408b39c5158Smillert        is( $t1->day,   1,          "  day()"   );
409b39c5158Smillert
410b39c5158Smillert        # like the tests above, but starting with ical instead of epoch
411b39c5158Smillert        my $t2 = Date::ICal->new( ical => '19700101Z' );
412b39c5158Smillert        is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );
413b39c5158Smillert
414b39c5158Smillert        is( $t2->epoch, 0,          "  and back to ICal" );
415b39c5158Smillert    }
416b39c5158Smillert
4176fb12b70Safresh1A little bit of magic happens here.  When running on anything but MacOS, all
4186fb12b70Safresh1the tests run normally.  But when on MacOS, C<skip()> causes the entire
4196fb12b70Safresh1contents of the SKIP block to be jumped over.  It never runs.  Instead,
420b8851fccSafresh1C<skip()> prints special output that tells L<Test::Harness> that the tests have
4216fb12b70Safresh1been skipped.
422b39c5158Smillert
423b39c5158Smillert    1..7
424b39c5158Smillert    ok 1 - Epoch time of 0
425b39c5158Smillert    ok 2 # skip epoch to ICal not working on MacOS
426b39c5158Smillert    ok 3 # skip epoch to ICal not working on MacOS
427b39c5158Smillert    ok 4 # skip epoch to ICal not working on MacOS
428b39c5158Smillert    ok 5 # skip epoch to ICal not working on MacOS
429b39c5158Smillert    ok 6 # skip epoch to ICal not working on MacOS
430b39c5158Smillert    ok 7 # skip epoch to ICal not working on MacOS
431b39c5158Smillert
4326fb12b70Safresh1This means your tests won't fail on MacOS.  This means fewer emails
433b39c5158Smillertfrom MacPerl users telling you about failing tests that you know will
434b39c5158Smillertnever work.  You've got to be careful with skip tests.  These are for
435b39c5158Smillerttests which don't work and I<never will>.  It is not for skipping
436b39c5158Smillertgenuine bugs (we'll get to that in a moment).
437b39c5158Smillert
438b39c5158SmillertThe tests are wholly and completely skipped. [10]  This will work.
439b39c5158Smillert
440b39c5158Smillert    SKIP: {
441b39c5158Smillert        skip("I don't wanna die!");
442b39c5158Smillert
443b39c5158Smillert        die, die, die, die, die;
444b39c5158Smillert    }
445b39c5158Smillert
446b39c5158Smillert
447b39c5158Smillert=head2 Todo tests
448b39c5158Smillert
449b8851fccSafresh1While thumbing through the L<Date::ICal> man page, I came across this:
450b39c5158Smillert
451b39c5158Smillert   ical
452b39c5158Smillert
453b39c5158Smillert       $ical_string = $ical->ical;
454b39c5158Smillert
455b39c5158Smillert   Retrieves, or sets, the date on the object, using any
456b39c5158Smillert   valid ICal date/time string.
457b39c5158Smillert
4586fb12b70Safresh1"Retrieves or sets".  Hmmm. I didn't see a test for using C<ical()> to set
4596fb12b70Safresh1the date in the Date::ICal test suite.  So I wrote one:
460b39c5158Smillert
461b39c5158Smillert    use Test::More tests => 1;
462b39c5158Smillert    use Date::ICal;
463b39c5158Smillert
464b39c5158Smillert    my $ical = Date::ICal->new;
465b39c5158Smillert    $ical->ical('20201231Z');
466b39c5158Smillert    is( $ical->ical, '20201231Z',   'Setting via ical()' );
467b39c5158Smillert
4686fb12b70Safresh1Run that. I saw:
469b39c5158Smillert
470b39c5158Smillert    1..1
471b39c5158Smillert    not ok 1 - Setting via ical()
472b39c5158Smillert    #     Failed test (- at line 6)
473b39c5158Smillert    #          got: '20010814T233649Z'
474b39c5158Smillert    #     expected: '20201231Z'
475b39c5158Smillert    # Looks like you failed 1 tests of 1.
476b39c5158Smillert
4776fb12b70Safresh1Whoops!  Looks like it's unimplemented.  Assume you don't have the time to fix
4786fb12b70Safresh1this. [11] Normally, you'd just comment out the test and put a note in a todo
4796fb12b70Safresh1list somewhere.  Instead, explicitly state "this test will fail" by wrapping it
4806fb12b70Safresh1in a C<TODO> block:
481b39c5158Smillert
482b39c5158Smillert    use Test::More tests => 1;
483b39c5158Smillert
484b39c5158Smillert    TODO: {
485b39c5158Smillert        local $TODO = 'ical($ical) not yet implemented';
486b39c5158Smillert
487b39c5158Smillert        my $ical = Date::ICal->new;
488b39c5158Smillert        $ical->ical('20201231Z');
489b39c5158Smillert
490b39c5158Smillert        is( $ical->ical, '20201231Z',   'Setting via ical()' );
491b39c5158Smillert    }
492b39c5158Smillert
493b39c5158SmillertNow when you run, it's a little different:
494b39c5158Smillert
495b39c5158Smillert    1..1
496b39c5158Smillert    not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented
497b39c5158Smillert    #          got: '20010822T201551Z'
498b39c5158Smillert    #     expected: '20201231Z'
499b39c5158Smillert
500b8851fccSafresh1L<Test::More> doesn't say "Looks like you failed 1 tests of 1".  That '#
501b8851fccSafresh1TODO' tells L<Test::Harness> "this is supposed to fail" and it treats a
5026fb12b70Safresh1failure as a successful test.  You can write tests even before
503b39c5158Smillertyou've fixed the underlying code.
504b39c5158Smillert
505b8851fccSafresh1If a TODO test passes, L<Test::Harness> will report it "UNEXPECTEDLY
5066fb12b70Safresh1SUCCEEDED".  When that happens, remove the TODO block with C<local $TODO> and
5076fb12b70Safresh1turn it into a real test.
508b39c5158Smillert
509b39c5158Smillert
510b39c5158Smillert=head2 Testing with taint mode.
511b39c5158Smillert
512b39c5158SmillertTaint mode is a funny thing.  It's the globalest of all global
513b39c5158Smillertfeatures.  Once you turn it on, it affects I<all> code in your program
514b39c5158Smillertand I<all> modules used (and all the modules they use).  If a single
515b39c5158Smillertpiece of code isn't taint clean, the whole thing explodes.  With that
516b39c5158Smillertin mind, it's very important to ensure your module works under taint
517b39c5158Smillertmode.
518b39c5158Smillert
519b39c5158SmillertIt's very simple to have your tests run under taint mode.  Just throw
520b8851fccSafresh1a C<-T> into the C<#!> line.  L<Test::Harness> will read the switches
521b39c5158Smillertin C<#!> and use them to run your tests.
522b39c5158Smillert
523b39c5158Smillert    #!/usr/bin/perl -Tw
524b39c5158Smillert
525b39c5158Smillert    ...test normally here...
526b39c5158Smillert
5276fb12b70Safresh1When you say C<make test> it will run with taint mode on.
528b39c5158Smillert
529b39c5158Smillert
530b39c5158Smillert=head1 FOOTNOTES
531b39c5158Smillert
532b39c5158Smillert=over 4
533b39c5158Smillert
534b39c5158Smillert=item 1
535b39c5158Smillert
536b39c5158SmillertThe first number doesn't really mean anything, but it has to be 1.
537b39c5158SmillertIt's the second number that's important.
538b39c5158Smillert
539b39c5158Smillert=item 2
540b39c5158Smillert
541b39c5158SmillertFor those following along at home, I'm using version 1.31.  It has
542b39c5158Smillertsome bugs, which is good -- we'll uncover them with our tests.
543b39c5158Smillert
544b39c5158Smillert=item 3
545b39c5158Smillert
546b39c5158SmillertYou can actually take this one step further and test the manual
5476fb12b70Safresh1itself.  Have a look at L<Test::Inline> (formerly L<Pod::Tests>).
548b39c5158Smillert
549b39c5158Smillert=item 4
550b39c5158Smillert
551b39c5158SmillertYes, there's a mistake in the test suite.  What!  Me, contrived?
552b39c5158Smillert
553b39c5158Smillert=item 5
554b39c5158Smillert
555b39c5158SmillertWe'll get to testing the contents of lists later.
556b39c5158Smillert
557b39c5158Smillert=item 6
558b39c5158Smillert
559b39c5158SmillertBut what happens if your test program dies halfway through?!  Since we
560b39c5158Smillertdidn't say how many tests we're going to run, how can we know it
561b8851fccSafresh1failed?  No problem, L<Test::More> employs some magic to catch that death
562b39c5158Smillertand turn the test into a failure, even if every test passed up to that
563b39c5158Smillertpoint.
564b39c5158Smillert
565b39c5158Smillert=item 7
566b39c5158Smillert
567b39c5158SmillertI cleaned it up a little.
568b39c5158Smillert
569b39c5158Smillert=item 8
570b39c5158Smillert
571b39c5158SmillertMost Operating Systems record time as the number of seconds since a
572b39c5158Smillertcertain date.  This date is the beginning of the epoch.  Unix's starts
573b39c5158Smillertat midnight January 1st, 1970 GMT.
574b39c5158Smillert
575b39c5158Smillert=item 9
576b39c5158Smillert
577b39c5158SmillertMacOS's epoch is midnight January 1st, 1904.  VMS's is midnight,
578b39c5158SmillertNovember 17th, 1858, but vmsperl emulates the Unix epoch so it's not a
579b39c5158Smillertproblem.
580b39c5158Smillert
581b39c5158Smillert=item 10
582b39c5158Smillert
583b39c5158SmillertAs long as the code inside the SKIP block at least compiles.  Please
584b39c5158Smillertdon't ask how.  No, it's not a filter.
585b39c5158Smillert
586b39c5158Smillert=item 11
587b39c5158Smillert
588b39c5158SmillertDo NOT be tempted to use TODO tests as a way to avoid fixing simple
589b39c5158Smillertbugs!
590b39c5158Smillert
591b39c5158Smillert=back
592b39c5158Smillert
593b39c5158Smillert=head1 AUTHORS
594b39c5158Smillert
595b39c5158SmillertMichael G Schwern E<lt>schwern@pobox.comE<gt> and the perl-qa dancers!
596b39c5158Smillert
597b8851fccSafresh1=head1 MAINTAINERS
598b8851fccSafresh1
599b8851fccSafresh1=over 4
600b8851fccSafresh1
601b8851fccSafresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt>
602b8851fccSafresh1
603b8851fccSafresh1=back
604b8851fccSafresh1
605b39c5158Smillert=head1 COPYRIGHT
606b39c5158Smillert
607b39c5158SmillertCopyright 2001 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
608b39c5158Smillert
609b39c5158SmillertThis documentation is free; you can redistribute it and/or modify it
610b39c5158Smillertunder the same terms as Perl itself.
611b39c5158Smillert
612b39c5158SmillertIrrespective of its distribution, all code examples in these files
613b39c5158Smillertare hereby placed into the public domain.  You are permitted and
614b39c5158Smillertencouraged to use this code in your own programs for fun
615b39c5158Smillertor for profit as you see fit.  A simple comment in the code giving
616b39c5158Smillertcredit would be courteous but is not required.
617b39c5158Smillert
618b39c5158Smillert=cut
619