3.2 Extreme Testing
This testing philosophy is best articulated by the Extreme Programming (XP) methodology, wherein it is fundamental (see [BECK00]). On the subject of testing, XP says:
-
Development of tests should precede development of code.
-
All requirements should be turned into tests.
-
All tests should be automated.
-
The software should pass all its tests at the end of every day.
-
All bugs should get turned into tests.
If you've not yet applied these principles to the development of a new project, you're in for a life-altering experience when you first give them an honest try. Because your development speed will take off like a termite in a lumberyard.
Perl wholeheartedly embraces this philosophy, thanks largely to the efforts in recent years of a group of people including Michael Schwern, chromatic, and others. Because of their enthusiasm and commitment to the testing process, the number of tests that are run when you build Perl from the source and type "make test" has increased from 5,000 in Perl 5.004_04 (1997) to 70,000 in the current development version of Perl 5.9.0 (2004). That's right, a 14-fold increase.
True to the Perl philosophy, these developers exercised extreme laziness in adding those thousands of tests (see sidebar). To make it easier to create tests for Perl, they created a number of modules that can in fact be used to test anything. We'll take a look at them shortly.
What is it about this technology that brings such joy to the developer's heart? It provides a safety net, that's what. Instead of perennially wondering whether you've accidentally broken some code while working on an unrelated piece, you can make certain at any time. If you want to make a radical change to some interface, you can be sure that you've fixed all the dependencies because every scenario that you care about will have been captured in a test case, and running all the tests is as simple as typing "make test". One month into creating a new system that comprised more than a dozen modules and as many programs, I had built up a test suite that ran nearly 600 tests with that one command, all by adding the tests as I created the code they tested. When I made a radical change to convert one interface from functional to object-oriented, it took only a couple of hours because the tests told me when I was done.
This technique has been around for many years, but under the label regression testing, which sounds boring to anyone who can even figure out what it means.1 However, using that label can be your entrance ticket to respectability when trying to convince managers of large projects that you know what you're talking about.
What's this about laziness? Isn't that a pejorative way to describe luminaries of the Perl universe?
Actually, no; they'd take it as a compliment. Larry Wall enumerated three principal virtues of Perl programmers:
-
Laziness: "Hard work" sounds, well, hard. If you're faced with a mindless, repetitive tasksuch as running for public officethen laziness will make you balk at doing the same thing over and over again. Instead of stifling your creative spirit, you'll cultivate it by inventing a process that automates the repetitive task. If the Karate Kid had been a Perl programmer, he'd have abstracted the common factor from "wax on" and "wax off" shortly before fetching an orbital buffer. (Only to get, er, waxed, in the tournament from being out of shape. But I digress.)
-
Impatience: There's more than enough work to do in this business. By being impatient to get to the next thing quickly, you'll not spend unnecessary time on the task you're doing; you'll find ways to make it as efficient as possible.
-
Hubris: It's not good enough to be lazy and impatient if you're going to take them as an excuse to do lousy work. You need an unreasonable amount of pride in your abilities to carry you past the many causes for discouragement. If you didn't, and you thought about all the things that could go wrong with your code, you'd either never get out of bed in the morning, or just quit and take up potato farming.
What's this about laziness? Isn't that a pejorative way to describe luminaries of the Perl universe?
Actually, no; they'd take it as a compliment. Larry Wall enumerated three principal virtues of Perl programmers:
-
Laziness: "Hard work" sounds, well, hard. If you're faced with a mindless, repetitive tasksuch as running for public officethen laziness will make you balk at doing the same thing over and over again. Instead of stifling your creative spirit, you'll cultivate it by inventing a process that automates the repetitive task. If the Karate Kid had been a Perl programmer, he'd have abstracted the common factor from "wax on" and "wax off" shortly before fetching an orbital buffer. (Only to get, er, waxed, in the tournament from being out of shape. But I digress.)
-
Impatience: There's more than enough work to do in this business. By being impatient to get to the next thing quickly, you'll not spend unnecessary time on the task you're doing; you'll find ways to make it as efficient as possible.
-
Hubris: It's not good enough to be lazy and impatient if you're going to take them as an excuse to do lousy work. You need an unreasonable amount of pride in your abilities to carry you past the many causes for discouragement. If you didn't, and you thought about all the things that could go wrong with your code, you'd either never get out of bed in the morning, or just quit and take up potato farming.
So what are these magic modules that facilitate testing?
3.2.1 The Test Module
Test.pm was added in version 5.004 of Perl. By the time Perl 5.6.1 was released it was superseded by the Test::Simple module, which was published to CPAN and included in the Perl 5.8.0 core. Use Test::Simple instead.
If you inherit regression tests written to use Test.pm, it is still included in the Perl core for backward compatibility. You should be able to replace its use with Test::Simple if you want to start modernizing the tests.
3.2.2 The Test::Simple Module
When I say "simple," I mean simple. Test::Simple exports precisely one function, ok(). It takes one mandatory argument, and one optional argument. If its first argument evaluates to true, it prints "ok"; otherwise it prints "not ok". In each case it adds a number that starts at one and increases by one for each call to ok(). If a second argument is given, ok() then prints a dash and that argument, which is just a way of annotating a test.
Doesn't exactly sound like rocket science, does it? But on such a humble foundation is the entire Perl regression test suite built. The only other requirement is that we know how many tests we expected to run so we can tell if something caused them to terminate prematurely. That is done by an argument to the use statement:
use Test::Simple tests => 5;
The output from a test run therefore looks like:
1..5 ok 1 - Can make a frobnitz ok 2 - Can fliggle the frobnitz not ok 3 - Can grikkle the frobnitz ok 4 - Can delete the frobnitz ok 5 - Can't use a deleted frobnitz
Note that the first line says how many tests are expected to follow. That makes life easier for code like Test::Harness (see Section 3.2.9) that reads this output in order to summarize it.
3.2.3 The Test::More Module
You knew there couldn't be a module called Test::Simple unless there was something more complicated, right? Here it is. This is the module you'll use for virtually all your testing. It exports many useful functions aside from the same ok() as Test::Simple. Some of the most useful ones are:
is($expression, $value, $description)
-
Same as ok($expression eq $value, $description). So why bother? Because is() can give you better diagnostics when it fails.
like($attribute, qr/regex/, $description)
-
Tests whether $attribute matches the given regular expression.
is_deeply($struct1, $struct2, $description)
-
Tests whether data structures match. Follows references in each and prints out the first discrepancy it finds, if any. Note that it does not compare the packages that any components may be blessed into.
isa_ok($object, $class)
-
Tests whether an object is a member of, or inherits from, a particular class.
can_ok($object_or_class, @methods)
-
Tests whether an object or a class can perform each of the methods listed.
use_ok($module, @imports)
-
Tests whether a module can be loaded (if it contains a syntax error, for instance, this will fail). Wrap this test in a BEGIN block to ensure it is run at compile time, viz: BEGIN {use_ok("My::Module")}
There's much more. See the Test::More documentation. I won't be using any other functions in this chapter, though.
Caveat: I don't know why you might do this, but if you fork() inside the test script, don't run tests from child processes. They won't be recognized by the parent process where the test analyzer is running.
3.2.4 The Test::Exception Module
No, there's no module called Test::EvenMore.2 But there is a module you'll have to get from CPAN that can test for whether code lives or dies: Test::Exception. It exports these handy functions:
lives_ok()
-
Passes if code does not die. The first argument is the block of code, the second is an optional tag string. Note there is no comma between those arguments (this is a feature of Perl's prototyping mechanism when a code block is the first argument to a subroutine). For example:
lives_ok { risky_function() } "risky_function lives!"; dies_ok()
-
Passes if the code does die. Use this to check that error-checking code is operating properly. For example:
dies_ok { $] / 0 } "division by zero dies!"; throws_ok()
-
For when you want to check the actual text of the exception. For example:
throws_ok { some_web_function() } qr/URL not found/, "Nonexistent page get fails";
The second argument is a regular expression that the exception thrown by the code block in the first argument is tested against. If the match succeeds, so does the test. The optional third argument is the comment tag for the test. Note that there is a comma between the second and third arguments.
3.2.5 The Test::Builder Module
Did you spot that all these modules have a lot in common? Did you wonder how you'd add a Test:: module of your own, if you wanted to write one?
Then you're already thinking lazily, and the testing guys are ahead of you. That common functionality lives in a superclass module called Test::Builder, seldom seen, but used to take the drudgery out of creating new test modules.
Suppose we want to write a module that checks whether mail messages conform to RFC 822 syntax.3 We'll call it Test::MailMessage, and it will export a basic function, msg_ok(), that determines whether a message consists of an optional set of header lines, optionally followed by a blank line and any number of lines of text. (Yes, an empty message is legal according to this syntax. Unfortunately, too few people who have nothing to say avail themselves of this option.) Here's the module:
Example 3.1. Using Test::Builder to Create Test::MailMessage
1 package Test::MailMessage; 2 use strict; 3 use warnings; 4 use Carp; 5 use Test::Builder; 6 use base qw(Exporter); 7 our @EXPORT = qw(msg_ok); 8 9 my $test = Test::Builder->new; 10 11 sub import 12 { 13 my $self = shift; 14 my $pack = caller; 15 16 $test->exported_to($pack); 17 $test->plan(@_); 18 19 $self->export_to_level(1, $self, 'msg_ok'); 20 } 21 22 sub msg_ok 23 { 24 my $arg = shift; 25 my $tester = _new(); 26 eval 27 { 28 if (defined(fileno($arg))) 29 { 30 while (<$arg>) 31 { 32 $tester->_validate($_); 33 } 34 } 35 elsif (ref $arg) 36 { 37 $tester->_validate($_) for @$arg; 38 } 39 else 40 { 41 for ($arg =~ /(.*\n)/g) 42 { 43 $tester->_validate($_); 44 } 45 } 46 }; 47 $test->ok(!$@, shift); 48 $test->diag($@) if $@; 49 } 50 51 sub _new 52 { 53 return bless { expect => "header" }; 54 } 55 56 sub _validate 57 { 58 my ($self, $line) = @_; 59 return if $self->{expect} eq "body"; 60 if ($self->{expect} eq "header/continuation") 61 { 62 /^\s+\S/ and return; 63 } 64 $self->{expect} = "body", return if /^$/; 65 /^\S+:/ or croak "Invalid header"; 66 $self->{expect} = "header/continuation"; 67 } 68 69 1;
In line 1 we put this module into its own package, and in lines 2 and 3 we set warnings and strictness to help development go smoothly. In lines 4 and 5 we load the Carp module so we can call croak(), and the Test::Builder module so we can create an instance of it. In lines 6 and 7 we declare this to be a subclass of the Exporter module, exporting to the caller the subroutine msg_ok(). (Note that this is not a subclass of Test::Builder.)
In line 9 we create a Test::Builder object that will do the boring part of testing for us. Lines 11 through 20 are copied right out of the Test::Builder documentation; the import()routine is what allows us to say how many tests we're going to run when we use the module.
Lines 22 through 49 define the msg_ok() function itself. Its single argument specifies the mail message, either via a scalar containing the message, a reference to an array of lines in the message, or a filehandle from which the message can be read. Rather than read all of the lines from that filehandle into memory, we're going to operate on them one at a time because it's not necessary to have the whole message in memory. That's why we create the object $tester in line 25 to handle each line: it will contain a memory of its current state.
Then we call the _validate() method of $tester with each line of the message. Because that method will croak() if the message is in error, we wrap those loops in an eval block. This allows us easily to skip superfluous scanning of a message after detecting an error.
Finally, we see whether an error occurred; if an exception was thrown by croak()inside the eval block, $@ will contain its text; otherwise $@ will be empty. The ok() method of the Test::Builder object we created is the same function we're used to using in Test::Simple; it takes a true or false value, and an optional tag string, which we pass from our caller. If we had an exception, we pass its text to Test::Builder's diag() method, which causes it to be output as a comment during testing.
The _new() method in lines 5053 is not called new() because it's not really a proper constructor; it's really just creating a state object, which is why we didn't bother to make it inheritable. It starts out in life expecting to see a mail header.
Lines 5670 validate a line of a message. Because anything goes in a message body, if that's what we're expecting we have nothing to do. Otherwise, if we're expecting a header or header continuation line, then first we check for a continuation line (which starts with white space; this is how a long message header "overflows"). If we have a blank line (line 67), that separates the header from the body, so we switch to expecting body text.
Finally, we must at this point be expecting a header line, and one of those starts with non-white-space characters followed by a colon. If we don't have that, the message is bogus; but if we do, the next line could be either a header line or a continuation of the current header (or the blank line separating headers from the body).
Here's a simple test of the Test::MailMessage module:
1 #!/usr/bin/perl 2 use strict; 3 use warnings; 4 5 use lib qw(..); 6 use Test::MailMessage tests => 2; 7 8 msg_ok(<<EOM, "okay"); 9 from: ok 10 subject: whatever 11 12 body 13 EOM 14 msg_ok(\*DATA, "bogus"); 15 16 __END__ 17 bogus mail 18 message
The result of running this is:
1..2 ok 1 - okay not ok 2 - bogus # Failed test (./test at line 14) # Invalid header at ./test line 14 # Looks like you failed 1 tests of 2.
Although we only used one Test:: module, we could have used others, for example:
use Test::MailMessage tests z=> 2; use Test::Exception; use Test::More;
Only one of the use statements for Test::Modules should give the number of tests to be run. Do not think that each use statement is supposed to number the tests run by functions of that module; instead, one use statement gives the total number of tests to be run.
brian d foy4 used Test::Builder to create Test::Pod,5 which is also worth covering.
3.2.6 The Test::Pod Module
Documentation in Perl need not be entirely unstructured. The Plain Old Documentation (POD) format for storing documentation in the Perl source code (see the perlpod manual page) is a markup language and therefore it is possible to commit syntax errors. So rather than wait until your users try to look at your documentation (okay, play along with me hereimagine that you have users who want to read your documentation), and get errors from their POD viewer, you can make sure in advance that the POD is good.
Test::Pod exports a single function, pod_ok(), which checks the POD in the file named by its argument. I'll show an example of its use later in this chapter.
3.2.7 Test::Inline
If you're thinking that tests deserve to be inside the code they're testing just as much as documentation does, then you want Test::Inline. This module by Michael Schwern enables you to embed tests in code just like POD, because, in fact, it uses POD for that embedding.
3.2.8 Test::NoWarnings
Fergal Daly's Test::NoWarnings (formerly Test::Warn::None) lets you verify that your code is free of warnings. In its simplest usage, you just use the module, and increment the number of tests you're running, because Test::NoWarnings adds one more. So if your test starts:
use Test::More tests => 17;
then change it to:
use Test::NoWarnings; use Test::More tests => 18;
and the final test will be that no warnings were generated in the running of the other tests.
3.2.9 The Test::Harness Module
Test::Harness is how you combine multiple tests. It predates every other Test:: module, and you'll find it in every version of Perl 5. Test::Harness exports a function, runtests(), which runs all the test files whose names are passed to it as arguments and summarizes their results. You won't see one line printed per test; runtests() intercepts those lines of output. Rather you'll see one line printed per test file, followed by a summary of the results of the tests in that file. Then it prints a global summary line. Here's an example of the output:
t/01load....ok t/02tie.....ok t/03use.....ok t/04pod.....ok All tests successful. Files=4, Tests=24, 2 wallclock secs ( 1.51 cusr + 0.31 csys = 1.82 CPU)
As it runs, before printing "ok" on each line, you'll see a count of the tests being run updating in place, finally to be overwritten by "ok". If any fail, you'll see something appropriate instead of "ok".
You can use Test::Harness quite easily, for instance:
% perl -MTest::Harness -e 'runtests(glob "*.t")'
but it's seldom necessary even to do that, because a standard Perl module makefile will do it for you. I'll show you how shortly.
Test::Harness turns your regression tests into a full-fledged deliverable. Managers just love to watch the numbers whizzing around.