Test::Harness - run perl standard test scripts with statistics |
Test::Harness - run perl standard test scripts with statistics
use Test::Harness;
runtests(@test_files);
STOP! If all you want to do is write a test script, consider 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 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 following explains how Test::Harness interprets the output of your test program.
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).
Under no circumstances should 1..M appear in the middle of your output or more than once.
/^(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.
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
ok 42 this is the name of the test
Currently, Test::Harness does nothing with this information.
# 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
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!
to standard output. Any message after these words will be displayed by
Test::Harness
as the reason why testing is stopped.
ok 1 # Life is good, the sun is shining, RAM is cheap. not ok 2 # got 'Bush' expected 'Gore'
Test::Harness will honor the -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.
These variables can be used to configure the behavior of Test::Harness. They are exported on request.
runtests()
display the standard output of the script
without altering the behavior otherwise.
-w
.
It will happen, your tests will fail. After you mop up your ego, you can begin examining the summary report:
t/base..............ok t/nonumbers.........ok t/ok................ok t/test-harness......ok t/waterloo..........dubious 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:
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 section.
&runtests
is exported by Test::Harness per default.
$verbose
and $switches
are exported upon request.
All tests successful.\nFiles=%d, Tests=%d, %s
FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.
Test returned status %d (wstat %d)
$? >> 8
and $?
are printed in a message similar to the above.
Failed 1 test, %.2f%% okay. %s
Failed %d/%d tests, %.2f%% okay. %s
FAILED--Further testing stopped: %s
HARNESS_ACTIVE
HARNESS_COLUMNS
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_COMPILE_TEST
perlcc
before running it.
NOTE This currently only works when sitting in the perl source directory!
HARNESS_FILELEAK_IN_DIR
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.
HARNESS_IGNORE_EXITCODE
HARNESS_NOTTY
HARNESS_PERL_SWITCHES
HARNESS_PERL_SWITCHES
to -W
will
run all tests with all warnings enabled.
HARNESS_VERBOSE
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 t/base..............ok t/nonumbers.........ok t/ok................ok t/test-harness......ok All tests successful. Files=4, Tests=24, 2 wallclock secs ( 0.61 cusr + 0.41 csys = 1.02 CPU)
the Test manpage and the Test::Simple manpage for writing test scripts, the Benchmark manpage for the underlying timing routines, the Devel::CoreStack manpage to generate core dumps from failed tests and the Devel::Cover manpage 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.
Current maintainer is Michael G Schwern <schwern@pobox.com>
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)
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)
Deal with VMS's ``not \nok 4\n'' mistake.
Add option for coverage analysis.
HARNESS_COMPILE_TEST currently assumes it's run from the Perl source directory.
Test::Harness - run perl standard test scripts with statistics |