Nuts and bolts of testing.

What this says is: 1..1 "I'm going to run one test." [1] ok 1 "The first test passed". And that's about all magic there is to testing. Your basic unit of testing is the ok. For each thing you test, an ok is printed. Simple. Test::Harness interprets your test results to determine if you succeeded or failed (more on that later).

Writing all these print statements rapidly gets tedious. Fortunately, there's Test::Simple. It has one function, ok().

#!/usr/bin/perl -w
use Test::Simple tests => 1;
ok( 1 + 1 == 2 );

and that does the same thing as the code above. ok() is the backbone of Perl testing, and we'll be using it instead of roll-your-own from here on. If ok() gets a true value, the test passes. False, it fails.

1..2 "I'm going to run two tests." This number is used to ensure your test program ran all the way through and didn't die or skip some tests. ok 1 "The first test passed." not ok 2 "The second test failed". Test::Simple helpfully prints out some extra commentary about your tests.

It's not scary. Come, hold my hand. We're going to give an example of testing a module. For our example, we'll be testing a date library, Date::ICal. It's on CPAN, so download a copy and follow along. [2]

Where to start?

This is the hardest part of testing, where do you start? People often get overwhelmed at the apparent enormity of the task of testing a whole module. Best place to start is at the beginning. Date::ICal is an object-oriented module, and that means you start by making an object. So we test new().

Whoops, a failure! [4] Test::Simple helpfully lets us know on what line the failure occured, but not much else. We were supposed to get 17, but we didn't. What did we get?? Dunno. We'll have to re-run the test in the debugger or throw in some print statements to find out.

Instead, we'll switch from Test::Simple to Test::More. Test::More does everything Test::Simple does, and more! In fact, Test::More does things exactly the way Test::Simple does. You can literally swap Test::Simple out and put Test::More in its place. That's just what we're going to do.

Test::More does more than Test::Simple. The most important difference at this point is it provides more informative ways to say "ok". Although you can write almost any test with a generic ok(), it can't tell you what went wrong. Instead, we'll use the is() function, which lets us declare that something is supposed to be the same as something else:

letting us know that $ical->day returned 16, but we expected 17. A quick check shows that the code is working fine, we made a mistake when writing up the tests. Just change it to:

is( $ical->day, 16, ' day()' );

and everything works.

So any time you're doing a "this equals that" sort of test, use is(). It even works on arrays. The test is always in scalar context, so you can test how many elements are in a list this way. [5]

is( @foo, 5, 'foo has 5 elements' );

Sometimes the tests are wrong

Which brings us to a very important lesson. Code has bugs. Tests are code. Ergo, tests have bugs. A failing test could mean a bug in the code, but don't discount the possibility that the test is wrong.

On the flip side, don't be tempted to prematurely declare a test incorrect just because you're having trouble finding the bug. Invalidating a test isn't something to be taken lightly, and don't use it as a cop out to avoid work.

Testing lots of values

We're going to be wanting to test a lot of dates here, trying to trick the code with lots of different edge cases. Does it work before 1970? After 2038? Before 1904? Do years after 10,000 give it trouble? Does it get leap years right? We could keep repeating the code above, or we could set up a little try/expect loop.

So now we can test bunches of dates by just adding them to %ICal_Dates. Now that it's less work to test with more dates, you'll be inclined to just throw more in as you think of them. Only problem is, every time we add to that we have to keep adjusting the use Test::More tests => ## line. That can rapidly get annoying. There's two ways to make this work better.

First, we can calculate the plan dynamically using the plan() function.

The beginning of the epoch is different on most non-Unix operating systems [8]. Even though Perl smooths out the differences for the most part, certain ports do it differently. MacPerl is one off the top of my head. [9] We know this will never work on MacOS. So rather than just putting a comment in the test, we can explicitly say it's never going to work and skip the test.

A little bit of magic happens here. When running on anything but MacOS, all the tests run normally. But when on MacOS, skip() causes the entire contents of the SKIP block to be jumped over. It's never run. Instead, it prints special output that tells Test::Harness that the tests have been skipped.

1..7
ok 1 - Epoch time of 0
ok 2 # skip epoch to ICal not working on MacOS
ok 3 # skip epoch to ICal not working on MacOS
ok 4 # skip epoch to ICal not working on MacOS
ok 5 # skip epoch to ICal not working on MacOS
ok 6 # skip epoch to ICal not working on MacOS
ok 7 # skip epoch to ICal not working on MacOS

This means your tests won't fail on MacOS. This means less emails from MacPerl users telling you about failing tests that you know will never work. You've got to be careful with skip tests. These are for tests which don't work and never will. It is not for skipping genuine bugs (we'll get to that in a moment).

The tests are wholly and completely skipped. [10] This will work.

SKIP: {
skip("I don't wanna die!");
die, die, die, die, die;
}

Todo tests

Thumbing through the Date::ICal man page, I came across this:

ical
$ical_string = $ical->ical;
Retrieves, or sets, the date on the object, using any
valid ICal date/time string.

"Retrieves or sets". Hmmm, didn't see a test for using ical() to set the date in the Date::ICal test suite. So I'll write one.

Whoops! Looks like it's unimplemented. Let's assume we don't have the time to fix this. [11] Normally, you'd just comment out the test and put a note in a todo list somewhere. Instead, we're going to explicitly state "this test will fail" by wrapping it in a TODO block.

Test::More doesn't say "Looks like you failed 1 tests of 1". That '# TODO' tells Test::Harness "this is supposed to fail" and it treats a failure as a successful test. So you can write tests even before you've fixed the underlying code.

If a TODO test passes, Test::Harness will report it "UNEXPECTEDLY SUCCEEDED". When that happens, you simply remove the TODO block with local $TODO and turn it into a real test.

Testing with taint mode.

Taint mode is a funny thing. It's the globalest of all global features. Once you turn it on it effects all code in your program and all modules used (and all the modules they use). If a single piece of code isn't taint clean, the whole thing explodes. With that in mind, it's very important to ensure your module works under taint mode.

It's very simple to have your tests run under taint mode. Just throw a -T into the #! line. Test::Harness will read the switches in #! and use them to run your tests.

So when you say make test it will be run with taint mode and warnings on.

FOOTNOTES

The first number doesn't really mean anything, but it has to be 1. It's the second number that's important.

For those following along at home, I'm using version 1.31. It has some bugs, which is good -- we'll uncover them with our tests.

You can actually take this one step further and test the manual itself. Have a look at Test::Inline (formerly Pod::Tests).

Yes, there's a mistake in the test suite. What! Me, contrived?

We'll get to testing the contents of lists later.

But what happens if your test program dies halfway through?! Since we didn't say how many tests we're going to run, how can we know it failed? No problem, Test::More employs some magic to catch that death and turn the test into a failure, even if every test passed up to that point.

I cleaned it up a little.

Most Operating Systems record time as the number of seconds since a certain date. This date is the beginning of the epoch. Unix's starts at midnight January 1st, 1970 GMT.

MacOS's epoch is midnight January 1st, 1904. VMS's is midnight, November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a problem.

As long as the code inside the SKIP block at least compiles. Please don't ask how. No, it's not a filter.

Do NOT be tempted to use TODO tests as a way to avoid fixing simple bugs!

AUTHORS

Michael G Schwern <schwern@pobox.com> and the perl-qa dancers!

COPYRIGHT

Copyright 2001 by Michael G Schwern <schwern@pobox.com>.

This documentation is free; you can redistribute it and/or modify it under the same terms as Perl itself.

Irrespective of its distribution, all code examples in these files are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.