3 Test::Tutorial - A tutorial about writing really basic tests
7 AHHHHHHH!!!! NOT B<TESTING>! Anything but testing!
8 Beat me, whip me, send me to I<Detroit>, but don't make
13 Besides, I don't know how to write the damned things.
15 Is this you? Is writing tests right up there with writing
16 documentation and having your fingernails pulled out?
19 =head2 Nuts and bolts of testing.
21 Here's the most basic test program.
27 print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";
29 since 1 + 1 is 2, it prints:
34 What this says is: C<1..1> "I'm going to run one test." [1] C<ok 1>
35 "The first test passed". And that's about all magic there is to
36 testing. Your basic unit of testing is the 'ok'. For each thing you
37 test, an 'ok' is printed. Simple. Test::Harness interprets your test
38 results to determine if you succeeded or failed (more on that later).
40 Writing all these print statements rapidly gets tedious. Fortunately,
41 there's Test::Simple. It has one function, ok().
45 use Test::Simple tests => 1;
49 and that does the same thing as the code above. ok() is the backbone
50 of Perl testing, and we'll be using it instead of roll-your-own from
51 here on. If ok() gets a true value, the test passes. False, it
56 use Test::Simple tests => 2;
65 # Failed test (test.pl at line 5)
66 # Looks like you failed 1 tests of 2.
68 C<1..2> "I'm going to run two tests." This number is used to ensure
69 your test program ran all the way through and didn't die or skip some
70 tests. C<ok 1> "The first test passed." C<not ok 2> "The second test
71 failed". Test::Simple helpfuly prints out some extra commentary about
74 It's not scary. Come, hold my hand. We're going to give an example
75 of testing a module. For our example, we'll be testing a date
76 library, Date::ICal. It's on CPAN, so download a copy and follow
80 =head2 Where to start?
82 This is the hardest part of testing, where do you start? People often
83 get overwhelmed at the apparent enormity of the task of testing a
84 whole module. Best place to start is at the beginning. Date::ICal is
85 an object-oriented module, and that means you start by making an
86 object. So we test new().
90 use Test::Simple tests => 2;
94 my $ical = Date::ICal->new; # create an object
95 ok( defined $ical ); # check that we got something
96 ok( $ical->isa('Date::ICal') ); # and it's the right class
98 run that and you should get:
104 congratulations, you've written your first useful test.
109 That output isn't terribly descriptive, is it? When you have two
110 tests you can figure out which one is #2, but what if you have 102?
112 Each test can be given a little descriptive name as the second
115 use Test::Simple tests => 2;
117 ok( defined $ical, 'new() returned something' );
118 ok( $ical->isa('Date::ICal'), " and it's the right class" );
123 ok 1 - new() returned something
124 ok 2 - and it's the right class
127 =head2 Test the manual
129 Simplest way to build up a decent testing suite is to just test what
130 the manual says it does. [3] Let's pull something out of of the
131 Date::ICal SYNOPSIS and test that all it's bits work.
135 use Test::Simple tests => 8;
139 $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
140 hour => 16, min => 12, sec => 47,
143 ok( defined $ical, 'new() returned something' );
144 ok( $ical->isa('Date::ICal'), " and it's the right class" );
145 ok( $ical->sec == 47, ' sec()' );
146 ok( $ical->min == 12, ' min()' );
147 ok( $ical->hour == 16, ' hour()' );
148 ok( $ical->day == 17, ' day()' );
149 ok( $ical->month == 10, ' month()' );
150 ok( $ical->year == 1964, ' year()' );
152 run that and you get:
155 ok 1 - new() returned something
156 ok 2 - and it's the right class
161 # Failed test (- at line 16)
164 # Looks like you failed 1 tests of 8.
166 Whoops, a failure! [4] Test::Simple helpfully lets us know on what line
167 the failure occured, but not much else. We were supposed to get 17,
168 but we didn't. What did we get?? Dunno. We'll have to re-run the
169 test in the debugger or throw in some print statements to find out.
171 Instead, we'll switch from Test::Simple to Test::More. Test::More
172 does everything Test::Simple does, and more! In fact, Test::More does
173 things I<exactly> the way Test::Simple does. You can literally swap
174 Test::Simple out and put Test::More in its place. That's just what
177 Test::More provides more informative ways to say 'ok'. ok() is nice
178 and generic, you can write almost any test with it, but it can't tell
179 you what went wrong. For that, we use the is() function.
183 use Test::More tests => 8;
187 $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
188 hour => 16, min => 12, sec => 47,
191 ok( defined $ical, 'new() returned something' );
192 ok( $ical->isa('Date::ICal'), " and it's the right class" );
193 is( $ical->sec, 47, ' sec()' );
194 is( $ical->min, 12, ' min()' );
195 is( $ical->hour, 16, ' hour()' );
196 is( $ical->day, 17, ' day()' );
197 is( $ical->month, 10, ' month()' );
198 is( $ical->year, 1964, ' year()' );
200 "Is C<$ical->sec> 47?" "Is C<$ical->min> 12?" With is() in place,
201 you get some more information
204 ok 1 - new() returned something
205 ok 2 - and it's the right class
210 # Failed test (- at line 16)
215 # Looks like you failed 1 tests of 8.
217 letting us know that $ical->day returned 16, but we expected 17. A
218 quick check shows that the code is working fine, we made a mistake
219 when writing up the tests. Just change it to:
221 is( $ical->day, 16, ' day()' );
223 and everything works.
225 So any time you're doing a "this equals that" sort of test, use is().
226 It even works on arrays. The test is always in scalar context, so you
227 can test how many elements are in a list this way. [5]
229 is( @foo, 5, 'foo has 5 elements' );
232 =head2 Sometimes the tests are wrong
234 Which brings us to a very important lesson. Code has bugs. Tests are
235 code. Ergo, tests have bugs. A failing test could mean a bug in the
236 code, but don't discount the possibility that the test is wrong.
238 On the flip side, don't be tempted to prematurely declare a test
239 incorrect just because you're having trouble finding the bug.
240 Invalidating a test isn't something to be taken lightly, and don't use
241 it as a cop out to avoid work.
244 =head2 Testing lots of values
246 We're going to be wanting to test a lot of dates here, trying to trick
247 the code with lots of different edge cases. Does it work before 1970?
248 After 2038? Before 1904? Do years after 10,000 give it trouble?
249 Does it get leap years right? We could keep repeating the code above,
250 or we could set up a little try/expect loop.
252 use Test::More tests => 32;
256 # An ICal string And the year, month, date
257 # hour, minute and second we expect.
258 '19971024T120000' => # from the docs.
259 [ 1997, 10, 24, 12, 0, 0 ],
260 '20390123T232832' => # after the Unix epoch
261 [ 2039, 1, 23, 23, 28, 32 ],
262 '19671225T000000' => # before the Unix epoch
263 [ 1967, 12, 25, 0, 0, 0 ],
264 '18990505T232323' => # before the MacOS epoch
265 [ 1899, 5, 5, 23, 23, 23 ],
269 while( my($ical_str, $expect) = each %ICal_Dates ) {
270 my $ical = Date::ICal->new( ical => $ical_str );
272 ok( defined $ical, "new(ical => '$ical_str')" );
273 ok( $ical->isa('Date::ICal'), " and it's the right class" );
275 is( $ical->year, $expect->[0], ' year()' );
276 is( $ical->month, $expect->[1], ' month()' );
277 is( $ical->day, $expect->[2], ' day()' );
278 is( $ical->hour, $expect->[3], ' hour()' );
279 is( $ical->min, $expect->[4], ' min()' );
280 is( $ical->sec, $expect->[5], ' sec()' );
283 So now we can test bunches of dates by just adding them to
284 %ICal_Dates. Now that it's less work to test with more dates, you'll
285 be inclined to just throw more in as you think of them.
286 Only problem is, every time we add to that we have to keep adjusting
287 the C<use Test::More tests => ##> line. That can rapidly get
288 annoying. Instead we use 'no_plan'. This means we're just running
289 some tests, don't know how many. [6]
291 use Test::More 'no_plan'; # instead of tests => 32
293 now we can just add tests and not have to do all sorts of math to
294 figure out how many we're running.
297 =head2 Informative names
299 Take a look at this line here
301 ok( defined $ical, "new(ical => '$ical_str')" );
303 we've added more detail about what we're testing and the ICal string
304 itself we're trying out to the name. So you get results like:
306 ok 25 - new(ical => '19971024T120000')
307 ok 26 - and it's the right class
315 if something in there fails, you'll know which one it was and that
316 will make tracking down the problem easier. So try to put a bit of
317 debugging information into the test names.
320 =head2 Skipping tests
322 Poking around in the existing Date::ICal tests, I found this in
327 use Test::More tests => 7;
330 # Make sure epoch time is being handled sanely.
331 my $t1 = Date::ICal->new( epoch => 0 );
332 is( $t1->epoch, 0, "Epoch time of 0" );
334 # XXX This will only work on unix systems.
335 is( $t1->ical, '19700101Z', " epoch to ical" );
337 is( $t1->year, 1970, " year()" );
338 is( $t1->month, 1, " month()" );
339 is( $t1->day, 1, " day()" );
341 # like the tests above, but starting with ical instead of epoch
342 my $t2 = Date::ICal->new( ical => '19700101Z' );
343 is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );
345 is( $t2->epoch, 0, " and back to ICal" );
347 The beginning of the epoch is different on most non-Unix operating
348 systems [8]. Even though Perl smooths out the differences for the most
349 part, certain ports do it differently. MacPerl is one off the top of
350 my head. [9] We I<know> this will never work on MacOS. So rather than
351 just putting a comment in the test, we can explicitly say it's never
352 going to work and skip the test.
354 use Test::More tests => 7;
357 # Make sure epoch time is being handled sanely.
358 my $t1 = Date::ICal->new( epoch => 0 );
359 is( $t1->epoch, 0, "Epoch time of 0" );
362 skip('epoch to ICal not working on MacOS', 6)
365 is( $t1->ical, '19700101Z', " epoch to ical" );
367 is( $t1->year, 1970, " year()" );
368 is( $t1->month, 1, " month()" );
369 is( $t1->day, 1, " day()" );
371 # like the tests above, but starting with ical instead of epoch
372 my $t2 = Date::ICal->new( ical => '19700101Z' );
373 is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );
375 is( $t2->epoch, 0, " and back to ICal" );
378 A little bit of magic happens here. When running on anything but
379 MacOS, all the tests run normally. But when on MacOS, skip() causes
380 the entire contents of the SKIP block to be jumped over. It's never
381 run. Instead, it prints special output that tells Test::Harness that
382 the tests have been skipped.
385 ok 1 - Epoch time of 0
386 ok 2 # skip epoch to ICal not working on MacOS
387 ok 3 # skip epoch to ICal not working on MacOS
388 ok 4 # skip epoch to ICal not working on MacOS
389 ok 5 # skip epoch to ICal not working on MacOS
390 ok 6 # skip epoch to ICal not working on MacOS
391 ok 7 # skip epoch to ICal not working on MacOS
393 This means your tests won't fail on MacOS. This means less emails
394 from MacPerl users telling you about failing tests that you know will
395 never work. You've got to be careful with skip tests. These are for
396 tests which don't work and B<never will>. It is not for skipping
397 genuine bugs (we'll get to that in a moment).
399 The tests are wholely and completely skipped. [10] This will work.
402 skip("I don't wanna die!");
404 die, die, die, die, die;
410 Thumbing through the Date::ICal man page, I came across this:
414 $ical_string = $ical->ical;
416 Retrieves, or sets, the date on the object, using any
417 valid ICal date/time string.
419 "Retrieves or sets". Hmmm, didn't see a test for using ical() to set
420 the date in the Date::ICal test suite. So I'll write one.
422 use Test::More tests => 1;
424 my $ical = Date::ICal->new;
425 $ical->ical('20201231Z');
426 is( $ical->ical, '20201231Z', 'Setting via ical()' );
431 not ok 1 - Setting via ical()
432 # Failed test (- at line 6)
433 # got: '20010814T233649Z'
434 # expected: '20201231Z'
435 # Looks like you failed 1 tests of 1.
437 Whoops! Looks like it's unimplemented. Let's assume we don't have
438 the time to fix this. [11] Normally, you'd just comment out the test
439 and put a note in a todo list somewhere. Instead, we're going to
440 explicitly state "this test will fail" by wraping it in a TODO block.
442 use Test::More tests => 1;
445 local $TODO = 'ical($ical) not yet implemented';
447 my $ical = Date::ICal->new;
448 $ical->ical('20201231Z');
450 is( $ical->ical, '20201231Z', 'Setting via ical()' );
453 Now when you run, it's a little different:
456 not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented
457 # got: '20010822T201551Z'
458 # expected: '20201231Z'
460 Test::More doesn't say "Looks like you failed 1 tests of 1". That '#
461 TODO' tells Test::Harness "this is supposed to fail" and it treats a
462 failure as a successful test. So you can write tests even before
463 you've fixed the underlying code.
465 If a TODO test passes, Test::Harness will report it "UNEXPECTEDLY
466 SUCCEEDED". When that happens, you simply remove the TODO block and
467 C<local $TODO> and turn it into a real test.
470 =head2 Testing with taint mode.
472 Taint mode is a funny thing. It's the globalest of all global
473 features. Once you turn it on it effects B<all> code in your program
474 and B<all> modules used (and all the modules they use). If a single
475 piece of code isn't taint clean, the whole thing explodes. With that
476 in mind, it's very important to ensure your module works under taint
479 It's very simple to have your tests run under taint mode. Just throw
480 a -T into the #! line. Test::Harness will read the switches in #! and
481 use them to run your tests.
485 use Test::More 'no_plan';
487 ...test normally here...
489 So when you say "make test" it will be run with taint mode and
499 The first number doesn't really mean anything, but it has to be 1.
500 It's the second number that's important.
504 For those following along at home, I'm using version 1.31. It has
505 some bugs, which is good -- we'll uncover them with our tests.
509 You can actually take this one step further and test the manual
510 itself. Have a look at Pod::Tests (soon to be Test::Inline).
514 Yes, there's a mistake in the test suite. What! Me, contrived?
518 We'll get to testing the contents of lists later.
522 But what happens if your test program dies halfway through?! Since we
523 didn't say how many tests we're going to run, how can we know it
524 failed? No problem, Test::More employs some magic to catch that death
525 and turn the test into a failure, even if every test passed up to that
530 I cleaned it up a little.
534 Most Operating Systems record time as the number of seconds since a
535 certain date. This date is the beginning of the epoch. Unix's starts
536 at midnight January 1st, 1970 GMT.
540 MacOS's epoch is midnight January 1st, 1904. VMS's is midnight,
541 November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a
546 As long as the code inside the SKIP block at least compiles. Please
547 don't ask how. No, it's not a filter.
551 Do NOT be tempted to use TODO tests as a way to avoid fixing simple