The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Harness - run perl standard test scripts with statistics

SYNOPSIS

  use Test::Harness;

  runtests(@test_files);

DESCRIPTION

(By using the Test module, you can write test scripts without knowing the exact output this module expects. However, if you need to know the specifics, read on!)

Perl test scripts print to standard output "ok N" for each single test, where N is an increasing sequence of integers. The first line output by a standard test script is "1..M" with M being the number of tests that should be run within the test script. Test::Harness::runtests(@tests) runs all the testscripts named as arguments and checks standard output for the expected "ok N" strings.

After all tests have been performed, runtests() prints some performance statistics that are computed by the Benchmark module.

The test script output

The following explains how Test::Harness interprets the output of your test program.

'1..M'

This header tells how many tests there will be. It should be the first line output by your test program (but its okay if its preceded by comments).

In certain instanced, you may not know how many tests you will ultimately be running. In this case, it is permitted (but not encouraged) for the 1..M header to appear as the last line output by your test (again, it can be followed by further comments). But we strongly encourage you to put it first.

Under no circumstances should 1..M appear in the middle of your output or more than once.

'ok', 'not ok'. Ok?

Any output from the testscript to standard error is ignored and bypassed, thus will be seen by the user. Lines written to standard output containing /^(not\s+)?ok\b/ are interpreted as feedback for runtests(). All other lines are discarded.

/^not ok/ indicates a failed test. /^ok/ is a successful test.

test numbers

Perl normally expects the 'ok' or 'not ok' to be followed by a test number. It is tolerated if the test numbers after 'ok' are omitted. In this case Test::Harness maintains temporarily its own counter until the script supplies test numbers again. So the following test script

    print <<END;
    1..6
    not ok
    ok
    not ok
    ok
    ok
    END

will generate

    FAILED tests 1, 3, 6
    Failed 3/6 tests, 50.00% okay
$Test::Harness::verbose

The global variable $Test::Harness::verbose is exportable and can be used to let runtests() display the standard output of the script without altering the behavior otherwise.

$Test::Harness::switches

The global variable $Test::Harness::switches is exportable and can be used to set perl command line options used for running the test script(s). The default value is -w.

Skipping tests

If the standard output line contains the substring # Skip (with variations in spacing and case) after ok or ok NUMBER, it is counted as a skipped test. If the whole testscript succeeds, the count of skipped tests is included in the generated output. Test::Harness reports the text after # Skip\S*\s+ as a reason for skipping.

  ok 23 # skip Insufficient flogiston pressure.

Similarly, one can include a similar explanation in a 1..0 line emitted if the test script is skipped completely:

  1..0 # Skipped: no leverage found
Todo tests

If the standard output line contains the substring # TODO after not ok or not ok NUMBER, it is counted as a todo test. The text afterwards is the thing that has to be done before this test will succeed.

  not ok 13 # TODO harness the power of the atom

These tests represent a feature to be implemented or a bug to be fixed and act as something of an executable "thing to do" list. They are not expected to succeed. Should a todo test begin succeeding, Test::Harness will report it as a bonus. This indicates that whatever you were supposed to do has been done and you should promote this to a normal test.

Bail out!

As an emergency measure, a test script can decide that further tests are useless (e.g. missing dependencies) and testing should stop immediately. In that case the test script prints the magic words

  Bail out!

to standard output. Any message after these words will be displayed by Test::Harness as the reason why testing is stopped.

Comments

Additional comments may be put into the testing output on their own lines. Comment lines should begin with a '#', Test::Harness will ignore them.

  ok 1
  # Life is good, the sun is shining, RAM is cheap.
  not ok 2
  # got 'Bush' expected 'Gore'

EXPORT

&runtests is exported by Test::Harness per default.

$verbose and $switches are exported upon request.

DIAGNOSTICS

All tests successful.\nFiles=%d, Tests=%d, %s

If all tests are successful some statistics about the performance are printed.

FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.

For any single script that has failing subtests statistics like the above are printed.

Test returned status %d (wstat %d)

Scripts that return a non-zero exit status, both $? >> 8 and $? are printed in a message similar to the above.

Failed 1 test, %.2f%% okay. %s
Failed %d/%d tests, %.2f%% okay. %s

If not all tests were successful, the script dies with one of the above messages.

FAILED--Further testing stopped%s

If a single subtest decides that further testing will not make sense, the script dies with this message.

ENVIRONMENT

HARNESS_IGNORE_EXITCODE

Makes harness ignore the exit status of child processes when defined.

HARNESS_NOTTY

When set to a true value, forces it to behave as though STDOUT were not a console. You may need to set this if you don't want harness to output more frequent progress messages using carriage returns. Some consoles may not handle carriage returns properly (which results in a somewhat messy output).

HARNESS_COMPILE_TEST

When true it will make harness attempt to compile the test using perlcc before running it.

HARNESS_FILELEAK_IN_DIR

When set to the name of a directory, harness will check after each test whether new files appeared in that directory, and report them as

  LEAKED FILES: scr.tmp 0 my.db

If relative, directory name is with respect to the current directory at the moment runtests() was called. Putting absolute path into HARNESS_FILELEAK_IN_DIR may give more predicatable results.

HARNESS_PERL_SWITCHES

Its value will be prepended to the switches used to invoke perl on each test. For example, setting HARNESS_PERL_SWITCHES to "-W" will run all tests with all warnings enabled.

HARNESS_COLUMNS

This value will be used for the width of the terminal. If it is not set then it will default to COLUMNS. If this is not set, it will default to 80. Note that users of Bourne-sh based shells will need to export COLUMNS for this module to use that variable.

HARNESS_ACTIVE

Harness sets this before executing the individual tests. This allows the tests to determine if they are being executed through the harness or by any other means.

SEE ALSO

Test for writing test scripts, Benchmark for the underlying timing routines and Devel::Coverage for test coverage analysis.

AUTHORS

Either Tim Bunce or Andreas Koenig, we don't know. What we know for sure is, that it was inspired by Larry Wall's TEST script that came with perl distributions for ages. Numerous anonymous contributors exist.

Current maintainers are Andreas Koenig <andreas.koenig@anima.de> and Michael G Schwern <schwern@pobox.com>

BUGS

Test::Harness uses $^X to determine the perl binary to run the tests with. Test scripts running via the shebang (#!) line may not be portable because $^X is not consistent for shebang scripts across platforms. This is no problem when Test::Harness is run with an absolute path to the perl binary or when $^X can be found in the path.