10 use vars qw($VERSION $verbose $switches $have_devel_corestack $curtest
11 @ISA @EXPORT @EXPORT_OK);
12 $have_devel_corestack = 0;
17 @EXPORT= qw(&runtests);
18 @EXPORT_OK= qw($verbose $switches);
21 Failed Test Status Wstat Total Fail Failed List of failed
22 -------------------------------------------------------------------------------
26 @<<<<<<<<<<<<<< @>> @>>>> @>>>> @>>> ^##.##% ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
35 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
46 my($test,$te,$ok,$next,$max,$pct,$totok,@failed,%failedtests);
53 # pass -I flags to children
54 my $old5lib = $ENV{PERL5LIB};
55 local($ENV{'PERL5LIB'}) = join($Config{path_sep}, @INC);
57 if ($^O eq 'VMS') { $switches =~ s/-(\S*[A-Z]\S*)/"-$1"/g }
59 my $t_start = new Benchmark;
60 while ($test = shift(@tests)) {
63 if ($^O eq 'VMS') { $te =~ s/^.*\.t\./[.t./; }
64 print "$te" . '.' x (20 - length($te));
65 my $fh = new FileHandle;
66 $fh->open($test) or print "can't open $test. $!\n";
69 $s .= q[ "-T"] if $first =~ /^#!.*\bperl.*-\w*T/;
70 $fh->close or print "can't close $test. $!\n";
71 my $cmd = "$^X $s $test|";
72 $cmd = "MCR $cmd" if $^O eq 'VMS';
73 $fh->open($cmd) or print "can't run $test. $!\n";
74 $ok = $next = $max = 0;
80 if (/^1\.\.([0-9]+)/) {
85 } elsif ($max && /^(not\s+)?ok\b/) {
87 if (/^not ok\s*(\d*)/){
90 } elsif (/^ok\s*(\d*)/) {
96 # warn "Test output counter mismatch [test $this]\n";
97 # no need to warn probably
98 push @failed, $next..$this-1;
99 } elsif ($this < $next) {
100 #we have seen more "ok" lines than the number suggests
101 warn "Confused test output: test $this answered after test ", $next-1, "\n";
107 $fh->close; # must close to reap child resource values
110 $estatus = ($^O eq 'VMS'
111 ? eval 'use vmsish "status"; $estatus = $?'
114 my ($failed, $canon, $percent) = ('??', '??');
115 printf "dubious\n\tTest returned status $estatus (wstat %d, 0x%x)\n",
117 print "\t\t(VMS status is $estatus)\n" if $^O eq 'VMS';
118 if (corestatus($wstatus)) { # until we have a wait module
119 if ($have_devel_corestack) {
120 Devel::CoreStack::stack($^X);
122 print "\ttest program seems to have generated a core\n";
127 if ($next == $max + 1 and not @failed) {
128 print "\tafter all the subtests completed successfully\n";
130 $failed = 0; # But we do not set $canon!
132 push @failed, $next..$max;
134 (my $txt, $canon) = canonfailed($max,@failed);
135 $percent = 100*(scalar @failed)/$max;
139 $failedtests{$test} = { canon => $canon, max => $max || '??',
141 name => $test, percent => $percent,
142 estat => $estatus, wstat => $wstatus,
144 } elsif ($ok == $max && $next == $max+1) {
148 print "skipping test on this platform\n";
153 push @failed, $next..$max;
156 my ($txt, $canon) = canonfailed($max,@failed);
158 $failedtests{$test} = { canon => $canon, max => $max,
159 failed => scalar @failed,
160 name => $test, percent => 100*(scalar @failed)/$max,
161 estat => '', wstat => '',
164 print "Don't know which tests failed: got $ok ok, expected $max\n";
165 $failedtests{$test} = { canon => '??', max => $max,
167 name => $test, percent => undef,
168 estat => '', wstat => '',
172 } elsif ($next == 0) {
173 print "FAILED before any test output arrived\n";
175 $failedtests{$test} = { canon => '??', max => '??',
177 name => $test, percent => undef,
178 estat => '', wstat => '',
182 my $t_total = timediff(new Benchmark, $t_start);
185 if (defined $old5lib) {
186 $ENV{PERL5LIB} = $old5lib;
188 delete $ENV{PERL5LIB};
191 if ($bad == 0 && $totmax) {
192 print "All tests successful.\n";
194 die "FAILED--no tests were run for some reason.\n";
195 } elsif ($totmax==0) {
196 my $blurb = $total==1 ? "script" : "scripts";
197 die "FAILED--$total test $blurb could be run, alas--no output ever seen\n";
199 $pct = sprintf("%.2f", $good / $total * 100);
200 my $subpct = sprintf " %d/%d subtests failed, %.2f%% okay.",
201 $totmax - $totok, $totmax, 100*$totok/$totmax;
203 for $script (sort keys %failedtests) {
204 $curtest = $failedtests{$script};
208 die "Failed $bad/$total test scripts, $pct% okay.$subpct\n";
211 printf("Files=%d, Tests=%d, %s\n", $files, $totmax, timestr($t_total, 'nop'));
213 return ($bad == 0 && $totmax) ;
216 my $tried_devel_corestack;
221 eval {require 'wait.ph'};
224 $ret = ($st & 0200); # Tim says, this is for 90%
227 $ret = WCOREDUMP($st);
230 eval { require Devel::CoreStack; $have_devel_corestack++ }
231 unless $tried_devel_corestack++;
236 sub canonfailed ($@) {
237 my($max,@failed) = @_;
239 @failed = sort {$a <=> $b} grep !$seen{$_}++, @failed;
240 my $failed = @failed;
244 my $last = $min = shift @failed;
247 for (@failed, $failed[-1]) { # don't forget the last one
248 if ($_ > $last+1 || $_ == $last) {
252 push @canon, "$min-$last";
259 push @result, "FAILED tests @canon\n";
262 push @result, "FAILED test $last\n";
266 push @result, "\tFailed $failed/$max tests, ";
267 push @result, sprintf("%.2f",100*(1-$failed/$max)), "% okay\n";
268 my $txt = join "", @result;
277 Test::Harness - run perl standard test scripts with statistics
287 Perl test scripts print to standard output C<"ok N"> for each single
288 test, where C<N> is an increasing sequence of integers. The first line
289 output by a standard test script is C<"1..M"> with C<M> being the
290 number of tests that should be run within the test
291 script. Test::Harness::runtests(@tests) runs all the testscripts
292 named as arguments and checks standard output for the expected
295 After all tests have been performed, runtests() prints some
296 performance statistics that are computed by the Benchmark module.
298 =head2 The test script output
300 Any output from the testscript to standard error is ignored and
301 bypassed, thus will be seen by the user. Lines written to standard
302 output containing C</^(not\s+)?ok\b/> are interpreted as feedback for
303 runtests(). All other lines are discarded.
305 It is tolerated if the test numbers after C<ok> are omitted. In this
306 case Test::Harness maintains temporarily its own counter until the
307 script supplies test numbers again. So the following test script
321 Failed 3/6 tests, 50.00% okay
323 The global variable $Test::Harness::verbose is exportable and can be
324 used to let runtests() display the standard output of the script
325 without altering the behavior otherwise.
327 The global variable $Test::Harness::switches is exportable and can be
328 used to set perl command line options used for running the test
329 script(s). The default value is C<-w>.
333 C<&runtests> is exported by Test::Harness per default.
339 =item C<All tests successful.\nFiles=%d, Tests=%d, %s>
341 If all tests are successful some statistics about the performance are
344 =item C<FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.>
346 For any single script that has failing subtests statistics like the
349 =item C<Test returned status %d (wstat %d)>
351 Scripts that return a non-zero exit status, both C<$? E<gt>E<gt> 8> and C<$?> are
352 printed in a message similar to the above.
354 =item C<Failed 1 test, %.2f%% okay. %s>
356 =item C<Failed %d/%d tests, %.2f%% okay. %s>
358 If not all tests were successful, the script dies with one of the
365 See L<Benchmark> for the underlying timing routines.
369 Either Tim Bunce or Andreas Koenig, we don't know. What we know for
370 sure is, that it was inspired by Larry Wall's TEST script that came
371 with perl distributions for ages. Numerous anonymous contributors
372 exist. Current maintainer is Andreas Koenig.
376 Test::Harness uses $^X to determine the perl binary to run the tests
377 with. Test scripts running via the shebang (C<#!>) line may not be
378 portable because $^X is not consistent for shebang scripts across
379 platforms. This is no problem when Test::Harness is run with an
380 absolute path to the perl binary or when $^X can be found in the path.