3 Test::Tutorial - A tutorial about writing really basic tests
8 B<AHHHHHHH!!!! NOT TESTING! Anything but testing!
9 Beat me, whip me, send me to Detroit, but don't make
14 B<Besides, I don't know how to write the damned things.>
17 Is this you? Is writing tests right up there with writing
18 documentation and having your fingernails pulled out? Did you open up
21 ######## We start with some black magic
23 and decide that's quite enough for you?
25 It's ok. That's all gone now. We've done all the black magic for
26 you. And here are the tricks...
29 =head2 Nuts and bolts of testing.
31 Here's the most basic test program.
37 print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";
39 since 1 + 1 is 2, it prints:
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 'ok'. For each thing you
47 test, an 'ok' is printed. Simple. Test::Harness interprets your test
48 results to determine if you succeeded or failed (more on that later).
50 Writing all these print statements rapidly gets tedious. Fortunately,
51 there's Test::Simple. It has one function, ok().
55 use Test::Simple tests => 1;
59 and that does the same thing as the code above. ok() is the backbone
60 of Perl testing, and we'll be using it instead of roll-your-own from
61 here on. If ok() gets a true value, the test passes. False, it
66 use Test::Simple tests => 2;
75 # Failed test (test.pl at line 5)
76 # Looks like you failed 1 tests of 2.
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 helpfuly prints out some extra commentary about
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, Date::ICal. It's on CPAN, so download a copy and follow
90 =head2 Where to start?
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 new().
100 use Test::Simple tests => 2;
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
108 run that and you should get:
114 congratulations, you've written your first useful test.
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?
122 Each test can be given a little descriptive name as the second
125 use Test::Simple tests => 2;
127 ok( defined $ical, 'new() returned something' );
128 ok( $ical->isa('Date::ICal'), " and it's the right class" );
133 ok 1 - new() returned something
134 ok 2 - and it's the right class
137 =head2 Test the manual
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 of the
141 Date::ICal SYNOPSIS and test that all it's bits work.
145 use Test::Simple tests => 8;
149 $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
150 hour => 16, min => 12, sec => 47,
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()' );
162 run that and you get:
165 ok 1 - new() returned something
166 ok 2 - and it's the right class
171 # Failed test (- at line 16)
174 # Looks like you failed 1 tests of 8.
176 Whoops, a failure! [4] Test::Simple helpfully lets us know on what line
177 the failure occured, 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.
181 Instead, we'll switch from Test::Simple to Test::More. Test::More
182 does everything 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
187 Test::More provides more informative ways to say 'ok'. ok() is nice
188 and generic, you can write almost any test with it, but it can't tell
189 you what went wrong. For that, we use the is() function.
193 use Test::More tests => 8;
197 $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
198 hour => 16, min => 12, sec => 47,
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()' );
210 "Is C<$ical->sec> 47?" "Is C<$ical->min> 12?" With is() in place,
211 you get some more information
214 ok 1 - new() returned something
215 ok 2 - and it's the right class
220 # Failed test (- at line 16)
225 # Looks like you failed 1 tests of 8.
227 letting us know that $ical->day returned 16, but we expected 17. A
228 quick check shows that the code is working fine, we made a mistake
229 when writing up the tests. Just change it to:
231 is( $ical->day, 16, ' day()' );
233 and everything works.
235 So any time you're doing a "this equals that" sort of test, use is().
236 It even works on arrays. The test is always in scalar context, so you
237 can test how many elements are in a list this way. [5]
239 is( @foo, 5, 'foo has 5 elements' );
242 =head2 Sometimes the tests are wrong
244 Which brings us to a very important lesson. Code has bugs. Tests are
245 code. Ergo, tests have bugs. A failing test could mean a bug in the
246 code, but don't discount the possibility that the test is wrong.
248 On the flip side, don't be tempted to prematurely declare a test
249 incorrect just because you're having trouble finding the bug.
250 Invalidating a test isn't something to be taken lightly, and don't use
251 it as a cop out to avoid work.
254 =head2 Testing lots of values
256 We're going to be wanting to test a lot of dates here, trying to trick
257 the code with lots of different edge cases. Does it work before 1970?
258 After 2038? Before 1904? Do years after 10,000 give it trouble?
259 Does it get leap years right? We could keep repeating the code above,
260 or we could set up a little try/expect loop.
262 use Test::More tests => 32;
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 ],
279 while( my($ical_str, $expect) = each %ICal_Dates ) {
280 my $ical = Date::ICal->new( ical => $ical_str );
282 ok( defined $ical, "new(ical => '$ical_str')" );
283 ok( $ical->isa('Date::ICal'), " and it's the right class" );
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()' );
293 So 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
295 be inclined to just throw more in as you think of them.
296 Only problem is, every time we add to that we have to keep adjusting
297 the C<use Test::More tests => ##> line. That can rapidly get
298 annoying. Instead we use 'no_plan'. This means we're just running
299 some tests, don't know how many. [6]
301 use Test::More 'no_plan'; # instead of tests => 32
303 now we can just add tests and not have to do all sorts of math to
304 figure out how many we're running.
307 =head2 Informative names
309 Take a look at this line here
311 ok( defined $ical, "new(ical => '$ical_str')" );
313 we've added more detail about what we're testing and the ICal string
314 itself we're trying out to the name. So you get results like:
316 ok 25 - new(ical => '19971024T120000')
317 ok 26 - and it's the right class
325 if something in there fails, you'll know which one it was and that
326 will make tracking down the problem easier. So try to put a bit of
327 debugging information into the test names.
330 =head2 Skipping tests
332 Poking around in the existing Date::ICal tests, I found this in
337 use Test::More tests => 7;
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" );
344 # XXX This will only work on unix systems.
345 is( $t1->ical, '19700101Z', " epoch to ical" );
347 is( $t1->year, 1970, " year()" );
348 is( $t1->month, 1, " month()" );
349 is( $t1->day, 1, " day()" );
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" );
355 is( $t2->epoch, 0, " and back to ICal" );
357 The beginning of the epoch is different on most non-Unix operating
358 systems [8]. Even though Perl smooths out the differences for the most
359 part, certain ports do it differently. MacPerl is one off the top of
360 my head. [9] We I<know> this will never work on MacOS. So rather than
361 just putting a comment in the test, we can explicitly say it's never
362 going to work and skip the test.
364 use Test::More tests => 7;
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" );
372 skip('epoch to ICal not working on MacOS', 6)
375 is( $t1->ical, '19700101Z', " epoch to ical" );
377 is( $t1->year, 1970, " year()" );
378 is( $t1->month, 1, " month()" );
379 is( $t1->day, 1, " day()" );
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" );
385 is( $t2->epoch, 0, " and back to ICal" );
388 A little bit of magic happens here. When running on anything but
389 MacOS, all the tests run normally. But when on MacOS, skip() causes
390 the entire contents of the SKIP block to be jumped over. It's never
391 run. Instead, it prints special output that tells Test::Harness that
392 the tests have been skipped.
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
403 This means your tests won't fail on MacOS. This means less emails
404 from MacPerl users telling you about failing tests that you know will
405 never work. You've got to be careful with skip tests. These are for
406 tests which don't work and B<never will>. It is not for skipping
407 genuine bugs (we'll get to that in a moment).
409 The tests are wholely and completely skipped. [10] This will work.
412 skip("I don't wanna die!");
414 die, die, die, die, die;
420 Thumbing through the Date::ICal man page, I came across this:
424 $ical_string = $ical->ical;
426 Retrieves, or sets, the date on the object, using any
427 valid ICal date/time string.
429 "Retrieves or sets". Hmmm, didn't see a test for using ical() to set
430 the date in the Date::ICal test suite. So I'll write one.
432 use Test::More tests => 1;
434 my $ical = Date::ICal->new;
435 $ical->ical('20201231Z');
436 is( $ical->ical, '20201231Z', 'Setting via ical()' );
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.
447 Whoops! Looks like it's unimplemented. Let's assume we don't have
448 the time to fix this. [11] Normally, you'd just comment out the test
449 and put a note in a todo list somewhere. Instead, we're going to
450 explicitly state "this test will fail" by wraping it in a TODO block.
452 use Test::More tests => 1;
455 local $TODO = 'ical($ical) not yet implemented';
457 my $ical = Date::ICal->new;
458 $ical->ical('20201231Z');
460 is( $ical->ical, '20201231Z', 'Setting via ical()' );
463 Now when you run, it's a little different:
466 not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented
467 # got: '20010822T201551Z'
468 # expected: '20201231Z'
470 Test::More doesn't say "Looks like you failed 1 tests of 1". That '#
471 TODO' tells Test::Harness "this is supposed to fail" and it treats a
472 failure as a successful test. So you can write tests even before
473 you've fixed the underlying code.
475 If a TODO test passes, Test::Harness will report it "UNEXPECTEDLY
476 SUCCEEDED". When that happens, you simply remove the TODO block and
477 C<local $TODO> and turn it into a real test.
480 =head2 Testing with taint mode.
482 Taint mode is a funny thing. It's the globalest of all global
483 features. Once you turn it on it effects B<all> code in your program
484 and B<all> modules used (and all the modules they use). If a single
485 piece of code isn't taint clean, the whole thing explodes. With that
486 in mind, it's very important to ensure your module works under taint
489 It's very simple to have your tests run under taint mode. Just throw
490 a -T into the #! line. Test::Harness will read the switches in #! and
491 use them to run your tests.
495 use Test::More 'no_plan';
497 ...test normally here...
499 So when you say "make test" it will be run with taint mode and
509 The first number doesn't really mean anything, but it has to be 1.
510 It's the second number that's important.
514 For those following along at home, I'm using version 1.31. It has
515 some bugs, which is good -- we'll uncover them with our tests.
519 You can actually take this one step further and test the manual
520 itself. Have a look at Pod::Tests (soon to be Test::Inline).
524 Yes, there's a mistake in the test suite. What! Me, contrived?
528 We'll get to testing the contents of lists later.
532 But what happens if your test program dies halfway through?! Since we
533 didn't say how many tests we're going to run, how can we know it
534 failed? No problem, Test::More employs some magic to catch that death
535 and turn the test into a failure, even if every test passed up to that
540 I cleaned it up a little.
544 Most Operating Systems record time as the number of seconds since a
545 certain date. This date is the beginning of the epoch. Unix's starts
546 at midnight January 1st, 1970 GMT.
550 MacOS's epoch is midnight January 1st, 1904. VMS's is midnight,
551 November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a
556 As long as the code inside the SKIP block at least compiles. Please
557 don't ask how. No, it's not a filter.
561 Do NOT be tempted to use TODO tests as a way to avoid fixing simple