Home > Articles > Web Development > Perl

  • Print
  • + Share This
This chapter is from the book

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:

  1. Laziness: "Hard work" sounds, well, hard. If you're faced with a mindless, repetitive task—such as running for public office—then 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.)

  2. 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.

  3. 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:

  1. Laziness: "Hard work" sounds, well, hard. If you're faced with a mindless, repetitive task—such as running for public office—then 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.)

  2. 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.

  3. 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 50–53 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 56–70 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 here—imagine 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.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020