11 use vars qw($VERSION $verbose $switches $have_devel_corestack $curtest
12 @ISA @EXPORT @EXPORT_OK);
13 $have_devel_corestack = 0;
18 @EXPORT= qw(&runtests);
19 @EXPORT_OK= qw($verbose $switches);
22 Failed Test Status Wstat Total Fail Failed List of failed
23 ------------------------------------------------------------------------------
27 @<<<<<<<<<<<<<< @>> @>>>> @>>>> @>>> ^##.##% @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
45 my($test,$te,$ok,$next,$max,$pct,$totok,@failed,%failedtests);
52 # pass -I flags to children
53 my $old5lib = $ENV{PERL5LIB};
54 local($ENV{'PERL5LIB'}) = join($Config{path_sep}, @INC);
56 if ($^O eq 'VMS') { $switches =~ s/-(\S*[A-Z]\S*)/"-$1"/g }
58 my $t_start = new Benchmark;
59 while ($test = shift(@tests)) {
62 print "$te" . '.' x (20 - length($te));
63 my $fh = new FileHandle;
64 my $cmd = "$^X $switches $test|";
65 $cmd = "MCR $cmd" if $^O eq 'VMS';
66 $fh->open($cmd) or print "can't run. $!\n";
67 $ok = $next = $max = 0;
73 if (/^1\.\.([0-9]+)/) {
78 } elsif ($max && /^(not\s+)?ok\b/) {
80 if (/^not ok\s*(\d*)/){
83 } elsif (/^ok\s*(\d*)/) {
89 # warn "Test output counter mismatch [test $this]\n";
90 # no need to warn probably
91 push @failed, $next..$this-1;
92 } elsif ($this < $next) {
93 #we have seen more "ok" lines than the number suggests
94 warn "Confused test output: test $this answered after test ", $next-1, "\n";
100 $fh->close; # must close to reap child resource values
102 my $estatus = $^O eq 'VMS' ? $wstatus : $wstatus >> 8;
103 if ($^O eq 'VMS' ? !($wstatus & 1) : $wstatus) {
104 print "dubious\n\tTest returned status $estatus (wstat $wstatus)\n";
105 if (corestatus($wstatus)) { # until we have a wait module
106 if ($have_devel_corestack) {
107 Devel::CoreStack::stack($^X);
109 print "\ttest program seems to have generated a core\n";
113 $failedtests{$test} = { canon => '??', max => $max || '??',
115 name => $test, percent => undef,
116 estat => $estatus, wstat => $wstatus,
118 } elsif ($ok == $max && $next == $max+1) {
122 print "skipping test on this platform\n";
127 push @failed, $next..$max;
130 my ($txt, $canon) = canonfailed($max,@failed);
132 $failedtests{$test} = { canon => $canon, max => $max,
133 failed => scalar @failed,
134 name => $test, percent => 100*(scalar @failed)/$max,
135 estat => '', wstat => '',
138 print "Don't know which tests failed: got $ok ok, expected $max\n";
139 $failedtests{$test} = { canon => '??', max => $max,
141 name => $test, percent => undef,
142 estat => '', wstat => '',
146 } elsif ($next == 0) {
147 print "FAILED before any test output arrived\n";
149 $failedtests{$test} = { canon => '??', max => '??',
151 name => $test, percent => undef,
152 estat => '', wstat => '',
156 my $t_total = timediff(new Benchmark, $t_start);
159 if (defined $old5lib) {
160 $ENV{PERL5LIB} = $old5lib;
163 delete $ENV{PERL5LIB};
166 if ($bad == 0 && $totmax) {
167 print "All tests successful.\n";
169 die "FAILED--no tests were run for some reason.\n";
170 } elsif ($totmax==0) {
171 my $blurb = $total==1 ? "script" : "scripts";
172 die "FAILED--$total test $blurb could be run, alas--no output ever seen\n";
174 $pct = sprintf("%.2f", $good / $total * 100);
175 my $subpct = sprintf " %d/%d subtests failed, %.2f%% okay.",
176 $totmax - $totok, $totmax, 100*$totok/$totmax;
178 for $script (sort keys %failedtests) {
179 $curtest = $failedtests{$script};
183 die "Failed $bad/$total test scripts, $pct% okay.$subpct\n";
186 printf("Files=%d, Tests=%d, %s\n", $files, $totmax, timestr($t_total, 'nop'));
188 return ($bad == 0 && $totmax) ;
195 eval {require 'wait.ph'};
198 $ret = ($st & 0200); # Tim says, this is for 90%
201 $ret = WCOREDUMP($st);
204 eval {require Devel::CoreStack};
205 $have_devel_corestack++ unless $@;
210 sub canonfailed ($@) {
211 my($max,@failed) = @_;
213 @failed = sort {$a <=> $b} grep !$seen{$_}++, @failed;
214 my $failed = @failed;
218 my $last = $min = shift @failed;
221 for (@failed, $failed[-1]) { # don't forget the last one
222 if ($_ > $last+1 || $_ == $last) {
226 push @canon, "$min-$last";
233 push @result, "FAILED tests @canon\n";
236 push @result, "FAILED test $last\n";
240 push @result, "\tFailed $failed/$max tests, ";
241 push @result, sprintf("%.2f",100*(1-$failed/$max)), "% okay\n";
242 my $txt = join "", @result;
251 Test::Harness - run perl standard test scripts with statistics
261 Perl test scripts print to standard output C<"ok N"> for each single
262 test, where C<N> is an increasing sequence of integers. The first line
263 output by a standard test script is C<"1..M"> with C<M> being the
264 number of tests that should be run within the test
265 script. Test::Harness::runtests(@tests) runs all the testscripts
266 named as arguments and checks standard output for the expected
269 After all tests have been performed, runtests() prints some
270 performance statistics that are computed by the Benchmark module.
272 =head2 The test script output
274 Any output from the testscript to standard error is ignored and
275 bypassed, thus will be seen by the user. Lines written to standard
276 output containing C</^(not\s+)?ok\b/> are interpreted as feedback for
277 runtests(). All other lines are discarded.
279 It is tolerated if the test numbers after C<ok> are omitted. In this
280 case Test::Harness maintains temporarily its own counter until the
281 script supplies test numbers again. So the following test script
295 Failed 3/6 tests, 50.00% okay
297 The global variable $Test::Harness::verbose is exportable and can be
298 used to let runtests() display the standard output of the script
299 without altering the behavior otherwise.
303 C<&runtests> is exported by Test::Harness per default.
309 =item C<All tests successful.\nFiles=%d, Tests=%d, %s>
311 If all tests are successful some statistics about the performance are
314 =item C<FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.>
316 For any single script that has failing subtests statistics like the
319 =item C<Test returned status %d (wstat %d)>
321 Scripts that return a non-zero exit status, both C<$? E<gt>E<gt> 8> and C<$?> are
322 printed in a message similar to the above.
324 =item C<Failed 1 test, %.2f%% okay. %s>
326 =item C<Failed %d/%d tests, %.2f%% okay. %s>
328 If not all tests were successful, the script dies with one of the
335 See L<Benchmark> for the underlying timing routines.
339 Either Tim Bunce or Andreas Koenig, we don't know. What we know for
340 sure is, that it was inspired by Larry Wall's TEST script that came
341 with perl distributions for ages. Current maintainer is Andreas
346 Test::Harness uses $^X to determine the perl binary to run the tests
347 with. Test scripts running via the shebang (C<#!>) line may not be
348 portable because $^X is not consistent for shebang scripts across
349 platforms. This is no problem when Test::Harness is run with an
350 absolute path to the perl binary or when $^X can be found in the path.