Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

Test::Harness

Perl 5 version 8.8 documentation
Recently read

Test::Harness

NAME

Test::Harness - Run Perl standard test scripts with statistics

VERSION

Version 2.56

SYNOPSIS

  1. use Test::Harness;
  2. runtests(@test_files);

DESCRIPTION

STOP! If all you want to do is write a test script, consider using Test::Simple. Test::Harness is the module that reads the output from Test::Simple, Test::More and other modules based on Test::Builder. You don't need to know about Test::Harness to use those modules.

Test::Harness runs tests and expects output from the test in a certain format. That format is called TAP, the Test Anything Protocol. It is defined in Test::Harness::TAP.

Test::Harness::runtests(@tests) runs all the testscripts named as arguments and checks standard output for the expected strings in TAP format.

The prove utility is a thin wrapper around Test::Harness.

Taint mode

Test::Harness will honor the -T or -t in the #! line on your test files. So if you begin a test with:

  1. #!perl -T

the test will be run with taint mode on.

Configuration variables.

These variables can be used to configure the behavior of Test::Harness. They are exported on request.

  • $Test::Harness::Verbose

    The package 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. The prove utility's -v flag will set this.

  • $Test::Harness::switches

    The package 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 . It overrides HARNESS_SWITCHES .

  • $Test::Harness::Timer

    If set to true, and Time::HiRes is available, print elapsed seconds after each test file.

Failure

When tests fail, analyze the summary report:

  1. t/base..............ok
  2. t/nonumbers.........ok
  3. t/ok................ok
  4. t/test-harness......ok
  5. t/waterloo..........dubious
  6. Test returned status 3 (wstat 768, 0x300)
  7. DIED. FAILED tests 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
  8. Failed 10/20 tests, 50.00% okay
  9. Failed Test Stat Wstat Total Fail Failed List of Failed
  10. -----------------------------------------------------------------------
  11. t/waterloo.t 3 768 20 10 50.00% 1 3 5 7 9 11 13 15 17 19
  12. Failed 1/5 test scripts, 80.00% okay. 10/44 subtests failed, 77.27% okay.

Everything passed but t/waterloo.t. It failed 10 of 20 tests and exited with non-zero status indicating something dubious happened.

The columns in the summary report mean:

  • Failed Test

    The test file which failed.

  • Stat

    If the test exited with non-zero, this is its exit status.

  • Wstat

    The wait status of the test.

  • Total

    Total number of tests expected to run.

  • Fail

    Number which failed, either from "not ok" or because they never ran.

  • Failed

    Percentage of the total tests which failed.

  • List of Failed

    A list of the tests which failed. Successive failures may be abbreviated (ie. 15-20 to indicate that tests 15, 16, 17, 18, 19 and 20 failed).

Functions

Test::Harness currently only has one function, here it is.

  • runtests
    1. my $allok = runtests(@test_files);

    This runs all the given @test_files and divines whether they passed or failed based on their output to STDOUT (details above). It prints out each individual test which failed along with a summary report and a how long it all took.

    It returns true if everything was ok. Otherwise it will die() with one of the messages in the DIAGNOSTICS section.

  • _all_ok
    1. my $ok = _all_ok(\%tot);

    Tells you if this test run is overall successful or not.

  • _globdir
    1. my @files = _globdir $dir;

    Returns all the files in a directory. This is shorthand for backwards compatibility on systems where C doesn't work right.

  • _run_all_tests
    1. my($total, $failed) = _run_all_tests(@test_files);

    Runs all the given C<@test_files> (as C) but does it quietly (no report). $total is a hash ref summary of all the tests run. Its keys and values are this:

    1. bonus Number of individual todo tests unexpectedly passed
    2. max Number of individual tests ran
    3. ok Number of individual tests passed
    4. sub_skipped Number of individual tests skipped
    5. todo Number of individual todo tests
    6. files Number of test files ran
    7. good Number of test files passed
    8. bad Number of test files failed
    9. tests Number of test files originally given
    10. skipped Number of test files skipped

    If C<< $total->{bad} == 0 >> and C<< $total->{max} > 0 >>, you've got a successful test.

    $failed is a hash ref of all the test scripts which failed. Each key is the name of a test script, each value is another hash representing how that script failed. Its keys are these:

    1. name Name of the test which failed
    2. estat Script's exit value
    3. wstat Script's wait status
    4. max Number of individual tests
    5. failed Number which failed
    6. percent Percentage of tests which failed
    7. canon List of tests which failed (as string).

    C<$failed> should be empty if everything passed.

    B Currently this function is still noisy. I'm working on it.

  • _mk_leader
    1. my($leader, $ml) = _mk_leader($test_file, $width);

    Generates the 't/foo........' leader for the given C<$test_file> as well as a similar version which will overwrite the current line (by use of \r and such). C<$ml> may be empty if Test::Harness doesn't think you're on TTY.

    The C<$width> is the width of the "yada/blah.." string.

  • _leader_width
    1. my($width) = _leader_width(@test_files);

    Calculates how wide the leader should be based on the length of the longest test name.

EXPORT

C<&runtests> is exported by Test::Harness by default.

C<$verbose>, C<$switches> and C<$debug> 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 C<$? EE 8> and C<$?> 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 VARIABLES THAT TEST::HARNESS SETS

Test::Harness sets these before executing the individual tests.

  • HARNESS_ACTIVE

    This is set to a true value. It allows the tests to determine if they are being executed through the harness or by any other means.

  • HARNESS_VERSION

    This is the version of Test::Harness.

ENVIRONMENT VARIABLES THAT AFFECT TEST::HARNESS

  • HARNESS_COLUMNS

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

  • HARNESS_COMPILE_TEST

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

    B This currently only works when sitting in the perl source directory!

  • HARNESS_DEBUG

    If true, Test::Harness will print debugging information about itself as it runs the tests. This is different from C, which prints the output from the test being run. Setting C<$Test::Harness::Debug> will override this, or you can use the C<-d> switch in the F utility.

  • 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

    1. 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 C may give more predictable results.

  • 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_PERL

    Usually your tests will be run by C<$^X>, the currently-executing Perl. However, you may want to have it run by a different executable, such as a threading perl, or a different version.

    If you're using the F utility, you can use the C<--perl> switch.

  • HARNESS_PERL_SWITCHES

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

  • HARNESS_VERBOSE

    If true, Test::Harness will output the verbose results of running its tests. Setting C<$Test::Harness::verbose> will override this, or you can use the C<-v> switch in the F utility.

EXAMPLE

Here's how Test::Harness tests itself

  1. $ cd ~/src/devel/Test-Harness
  2. $ perl -Mblib -e 'use Test::Harness qw(&runtests $verbose);
  3. $verbose=0; runtests @ARGV;' t/*.t
  4. Using /home/schwern/src/devel/Test-Harness/blib
  5. t/base..............ok
  6. t/nonumbers.........ok
  7. t/ok................ok
  8. t/test-harness......ok
  9. All tests successful.
  10. Files=4, Tests=24, 2 wallclock secs ( 0.61 cusr + 0.41 csys = 1.02 CPU)

SEE ALSO

The included F utility for running test scripts from the command line, L and L for writing test scripts, L for the underlying timing routines, and L for test coverage analysis.

TODO

Provide a way of running tests quietly (ie. no printing) for automated validation of tests. This will probably take the form of a version of runtests() which rather than printing its output returns raw data on the state of the tests. (Partially done in Test::Harness::Straps)

Document the format.

Fix HARNESS_COMPILE_TEST without breaking its core usage.

Figure a way to report test names in the failure summary.

Rework the test summary so long test names are not truncated as badly. (Partially done with new skip test styles)

Add option for coverage analysis.

Trap STDERR.

Implement Straps total_results()

Remember exit code

Completely redo the print summary code.

Implement Straps callbacks. (experimentally implemented)

Straps->analyze_file() not taint clean, don't know if it can be

Fix that damned VMS nit.

HARNESS_TODOFAIL to display TODO failures

Add a test for verbose.

Change internal list of test results to a hash.

Fix stats display when there's an overrun.

Fix so perls with spaces in the filename work.

Keeping whittling away at _run_all_tests()

Clean up how the summary is printed. Get rid of those damned formats.

BUGS

HARNESS_COMPILE_TEST currently assumes it's run from the Perl source directory.

Please use the CPAN bug ticketing system at L. You can also mail bugs, fixes and enhancements to C<< > at C<< rt.cpan.org> >>.

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. Andreas Koenig held the torch for many years, and then Michael G Schwern.

Current maintainer is Andy Lester C<< >>.

COPYRIGHT

Copyright 2002-2005 by Michael G Schwern C<< >>, Andy Lester C<< >>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See L.