Section 8.1.  Test::Simple

Table of Contents

8.1. 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 + 2 != 7  ? "": "not "), "ok ", $i++, "\n");

The next logical advance would be to put the test into a subroutine that spat out the appropriate string. Some people came up with their own idiosyncratic way of skipping tests, marking known failures, and providing names for their tests so that they wouldn't have to go through and count to find the failing test.

Eventually, we ended up in the situation where every test suite looked more or less the same but somehow subtly different from the others. It was out of this chaos that the original Test module was born. Test provided an ok subroutine that compared one thing with another, and reported the result and an automated test number.

However, Test wasn't very flexible, and along came its modern replacement, Test::Simple. It works on exactly the same principle: you have an ok function that runs a test and prints out the appropriate output. Here's a simple test plan with Test::Simple.

    use Test::Simple tests => 3;

    ok( 1 + 1 =  = 2 );
    ok( 2 + 2 != 7, "Two and two are not seven" );
    ok( foobar(  ) );

The first line states how many tests are going to run, so that the automated test harness will know if the test script completed successfully or died halfway through. Then come the three tests. Test::Simple provides the ok subroutine to emit a test result. If the first parameter to ok is true, then the test was successful. The second parameter is an optional description to display along with the test result. When you're viewing the output, the description makes it much easier to understand what the test is for and also helps to locate which tests are failing. Running the preceding three tests on the command line has the following result:

    ok 1
    ok 2 - Two and two are not seven
    ok 3

If the first parameter to ok is false, then the test failed. When you test a false value:

    ok( 1 =  = 2, "One is two" );

The test output includes your description, the name of the test file, and the line number where the test failed to help you locate which test is failing:

    not ok 1 - One is two
    #     Failed test (simple.t at line 5)

These results are interpreted by the test harness to total up successes and failures. And this, basically, is all there is to Test::Simple, and all that most people need to know about testing. Test::Simple was, as its name implies, deliberately made really, really easy, so that there'd be no excuse[*] for not writing a decent test plan.

[*] Well, other than laziness, impatience, or hubris.

    Table of Contents
    © 2000- NIV