Test::Harness - Run Perl standard test scripts with statistics |
Test::Harness - Run Perl standard test scripts with statistics
Version 2.46
use Test::Harness;
runtests(@test_files);
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 the Test::Harness::TAP manpage.
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.
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.
These variables can be used to configure the behavior of Test::Harness. They are exported on request.
$Test::Harness::Verbose
$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
$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
.
When tests fail, analyze 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 by default.
$verbose
, $switches
and $debug
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_DEBUG
HARNESS_VERBOSE
, which prints
the output from the test being run. Setting $Test::Harness::Debug
will
override this, or you can use the -d
switch in the prove utility.
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
$^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.
HARNESS_PERL_SWITCHES
HARNESS_PERL_SWITCHES
to -W
will
run all tests with all warnings enabled.
HARNESS_VERBOSE
$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 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 included prove utility for running test scripts from the command line, the Test manpage and the Test::Simple manpage for writing test scripts, the Benchmark manpage for the underlying timing routines, and the Devel::Cover manpage for test coverage analysis.
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.
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-harness
at rt.cpan.org>
.
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 <andy at petdance.com>
.
Copyright 2002-2005
by Michael G Schwern <schwern at pobox.com>
,
Andy Lester <andy at petdance.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.
Test::Harness - Run Perl standard test scripts with statistics |