Switch to SpeakEasy.net DSL

The Modular Manual Browser

Home Page
Manual: (OpenBSD-3.6)
Apropos / Subsearch:
optional field

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

       Test::Harness - Run Perl standard test scripts with

       Version 2.42

           $Header: /cvs/src/gnu/usr.bin/perl/lib/Test/Harness.pm,v 1.9 2004/08/09 18:09:48 millert Exp $

         use Test::Harness;


       STOP! If all you want to do is write a test script, con-
       sider using Test::Simple.  Otherwise, read on.

       (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 inte-
       gers. 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::Har-
       ness::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.

           This header tells how many tests there will be.  For
           example, 1..10 means you plan on running 10 tests.
           This is a safeguard in case your test dies quietly in
           the middle of its run.

           It should be the first non-comment line output by your
           test program.

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

perl v5.8.5                 2002-11-06                          1

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

           Under no circumstances should 1..M appear in the mid-
           dle 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 fol-
           lowed 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;
               not ok
               not ok

           will generate

               FAILED tests 1, 3, 6
               Failed 3/6 tests, 50.00% okay

       test names
           Anything after the test number but before the # is
           considered to be the name of the test.

             ok 42 this is the name of the test

           Currently, Test::Harness does nothing with this infor-

       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.

perl v5.8.5                 2002-11-06                          2

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

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

             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 suc-

             not ok 13 # TODO harness the power of the atom

           Note that the TODO must have a space after it.

           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 what-
           ever 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.

           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'

       Anything else
           Any other output Test::Harness sees it will silently
           ignore BUT WE PLAN TO CHANGE THIS! If you wish to
           place additional output in your test script, please
           use a comment.

perl v5.8.5                 2002-11-06                          3

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

       Taint mode

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

           #!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.

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

           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". It overrides "HAR-


       It will happen: your tests will fail.  After you mop up
       your ego, you can begin examining the summary report:

                 Test returned status 3 (wstat 768, 0x300)
         DIED. FAILED tests 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
                 Failed 10/20 tests, 50.00% okay
         Failed Test  Stat Wstat Total Fail  Failed  List of Failed
         t/waterloo.t    3   768    20   10  50.00%  1 3 5 7 9 11 13 15 17 19
         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.

perl v5.8.5                 2002-11-06                          4

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

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

           The wait status of the test.

           Total number of tests expected to run.

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

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


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

             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

           This is just _run_all_tests() plus _show_results()

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

       $verbose, $switches and $debug are exported upon request.

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

perl v5.8.5                 2002-11-06                          5

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

       "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

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

           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

           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.

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

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

           If true, Test::Harness will print debugging informa-
           tion about itself as it runs the tests.  This is dif-
           ferent from "HARNESS_VERBOSE", which prints the output
           from the test being run.  Setting $Test::Har-
           ness::Debug will override this, or you can use the
           "-d" switch in the prove utility.

           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 predictable results.

perl v5.8.5                 2002-11-06                          6

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

           Makes harness ignore the exit status of child pro-
           cesses when defined.

           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 con-
           soles may not handle carriage returns properly (which
           results in a somewhat messy output).

           If true, the "ok" messages are printed out only every
           second.  This reduces output and may help increase
           testing speed over slow connections, or with very
           large numbers of tests.

           Usually your tests will be run by $^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 prove utility, you can use the
           "--perl" switch.

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

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

       Here's how Test::Harness tests itself

         $ cd ~/src/devel/Test-Harness
         $ perl -Mblib -e 'use Test::Harness qw(&runtests $verbose);
           $verbose=0; runtests @ARGV;' t/*.t
         Using /home/schwern/src/devel/Test-Harness/blib
         All tests successful.
         Files=4, Tests=24, 2 wallclock secs ( 0.61 cusr + 0.41 csys = 1.02 CPU)

perl v5.8.5                 2002-11-06                          7

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

       The included prove utility for running test scripts from
       the command line, Test and Test::Simple for writing test
       scripts, Benchmark for the underlying timing routines,
       Devel::CoreStack to generate core dumps from failed tests
       and Devel::Cover for test coverage analysis.

       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 "<andyATpetdance.com>".

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

       See <http://www.perl.com/perl/misc/Artistic.html>;

       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 trun-
       cated as badly.  (Partially done with new skip test

       Deal with VMS's "not \nok 4\n" mistake.

       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

perl v5.8.5                 2002-11-06                          8

Test::Harness(3p)Perl Programmers Reference GuidTest::Harness(3p)

       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.

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

       Please use the CPAN bug ticketing system at
       <http://rt.cpan.org/>;.  You can also mail bugs, fixes and
       enhancements to "<bug-test-harnessATrt.org>".

       Original code by Michael G Schwern, maintained by Andy

       Copyright 2003 by Michael G Schwern "<schwernATpobox.com>",
                         Andy Lester "<andyATpetdance.com>".

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

       See <http://www.perl.com/perl/misc/Artistic.html>;.

perl v5.8.5                 2002-11-06                          9