Chapter 8. Testing

Every programmer likes writing code, but only a brave and masochistic few actually like writing tests for their code. However, with the rise of XP, Agile programming, and other programming methodologies, it has become more important for programmers to write complete test suites for the code they produce.

Not only that, but thanks to the efforts of the Perl Kwalitee Assurance team, headed by Michael Schwern, there’s a good deal of social pressure for CPAN module writers to come up with thorough automated test plans for their modules.

Thankfully, Schwern and others have also produced a bunch of modules that make producing such test plans relatively painless. We’ll take a look at the more popular and useful modules in this chapter.

Test::Simple

Back in the mists of time, around the late 1990s, test plans were very simple indeed; you had a program that spat out “ok” or “not ok” followed by a test number, and an automated testing harness would go through, run your tests, and pick out the tests that failed.

So, programmers would write test scripts that looked something like this:

    print "1..10\n";

    print (( 1 + 1 =  = 2  ? "": "not "), "ok 1\n");
    print (( 2 + 2 != 7  ? "": "not "), "ok 2\n");
    if (foobar() ) {
        print "ok 3\n";
    } else {
        print "not ok 3\n";
    }
    ...

Then some programmers realized they didn’t want to keep score of the test numbers themselves, so they used a variable instead:

 print "1..10\n"; my $i = 1; print (( 1 + 1 = = 2 ? "": "not "), "ok ", $i++, "\n"); print (( 2 ...

Get Advanced Perl Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.