📄 harness.pm
字号:
my $subpct = sprintf " %d/%d subtests failed, %.2f%% okay.", $totmax - $totok, $totmax, 100*$totok/$totmax; # Create formats # First, figure out max length of test names my $failed_str = "Failed Test"; my $middle_str = " Status Wstat Total Fail Failed "; my $list_str = "List of Failed"; my $max_namelen = length($failed_str); my $script; foreach $script (keys %failedtests) { $max_namelen = (length $failedtests{$script}->{name} > $max_namelen) ? length $failedtests{$script}->{name} : $max_namelen; } my $list_len = $columns - length($middle_str) - $max_namelen; if ($list_len < length($list_str)) { $list_len = length($list_str); $max_namelen = $columns - length($middle_str) - $list_len; if ($max_namelen < length($failed_str)) { $max_namelen = length($failed_str); $columns = $max_namelen + length($middle_str) + $list_len; } } my $fmt_top = "format STDOUT_TOP =\n" . sprintf("%-${max_namelen}s", $failed_str) . $middle_str . $list_str . "\n" . "-" x $columns . "\n.\n"; my $fmt = "format STDOUT =\n" . "@" . "<" x ($max_namelen - 1) . " @>> @>>>> @>>>> @>>> ^##.##% " . "^" . "<" x ($list_len - 1) . "\n" . '{ $curtest->{name}, $curtest->{estat},' . ' $curtest->{wstat}, $curtest->{max},' . ' $curtest->{failed}, $curtest->{percent},' . ' $curtest->{canon}' . "\n}\n" . "~~" . " " x ($columns - $list_len - 2) . "^" . "<" x ($list_len - 1) . "\n" . '$curtest->{canon}' . "\n.\n"; eval $fmt_top; die $@ if $@; eval $fmt; die $@ if $@; # Now write to formats for $script (sort keys %failedtests) { $curtest = $failedtests{$script}; write; } if ($bad) { $bonusmsg =~ s/^,\s*//; print "$bonusmsg.\n" if $bonusmsg; die "Failed $bad/$total test scripts, $pct% okay.$subpct\n"; } } printf("Files=%d, Tests=%d, %s\n", $files, $totmax, timestr($t_total, 'nop')); return ($bad == 0 && $totmax) ;}my $tried_devel_corestack;sub corestatus { my($st) = @_; eval {require 'wait.ph'}; my $ret = defined &WCOREDUMP ? WCOREDUMP($st) : $st & 0200; eval { require Devel::CoreStack; $have_devel_corestack++ } unless $tried_devel_corestack++; $ret;}sub canonfailed ($@) { my($max,$skipped,@failed) = @_; my %seen; @failed = sort {$a <=> $b} grep !$seen{$_}++, @failed; my $failed = @failed; my @result = (); my @canon = (); my $min; my $last = $min = shift @failed; my $canon; if (@failed) { for (@failed, $failed[-1]) { # don't forget the last one if ($_ > $last+1 || $_ == $last) { if ($min == $last) { push @canon, $last; } else { push @canon, "$min-$last"; } $min = $_; } $last = $_; } local $" = ", "; push @result, "FAILED tests @canon\n"; $canon = "@canon"; } else { push @result, "FAILED test $last\n"; $canon = $last; } push @result, "\tFailed $failed/$max tests, "; push @result, sprintf("%.2f",100*(1-$failed/$max)), "% okay"; my $ender = 's' x ($skipped > 1); my $good = $max - $failed - $skipped; my $goodper = sprintf("%.2f",100*($good/$max)); push @result, " (-$skipped skipped test$ender: $good okay, $goodper%)" if $skipped; push @result, "\n"; my $txt = join "", @result; ($txt, $canon);}1;__END__=head1 NAMETest::Harness - run perl standard test scripts with statistics=head1 SYNOPSISuse Test::Harness;runtests(@tests);=head1 DESCRIPTION(By using the L<Test> module, you can write test scripts withoutknowing the exact output this module expects. However, if you need toknow the specifics, read on!)Perl test scripts print to standard output C<"ok N"> for each singletest, where C<N> is an increasing sequence of integers. The first lineoutput by a standard test script is C<"1..M"> with C<M> being thenumber of tests that should be run within the testscript. Test::Harness::runtests(@tests) runs all the testscriptsnamed as arguments and checks standard output for the expectedC<"ok N"> strings.After all tests have been performed, runtests() prints someperformance statistics that are computed by the Benchmark module.=head2 The test script outputAny output from the testscript to standard error is ignored andbypassed, thus will be seen by the user. Lines written to standardoutput containing C</^(not\s+)?ok\b/> are interpreted as feedback forruntests(). All other lines are discarded.It is tolerated if the test numbers after C<ok> are omitted. In thiscase Test::Harness maintains temporarily its own counter until thescript supplies test numbers again. So the following test script print <<END; 1..6 not ok ok not ok ok ok ENDwill generate FAILED tests 1, 3, 6 Failed 3/6 tests, 50.00% okayThe global variable $Test::Harness::verbose is exportable and can beused to let runtests() display the standard output of the scriptwithout altering the behavior otherwise.The global variable $Test::Harness::switches is exportable and can beused to set perl command line options used for running the testscript(s). The default value is C<-w>.If the standard output line contains substring C< # Skip> (withvariations in spacing and case) after C<ok> or C<ok NUMBER>, it iscounted as a skipped test. If the whole testscript succeeds, thecount of skipped tests is included in the generated output.C<Test::Harness> reports the text after C< # Skip(whatever)> as areason for skipping. Similarly, one can include a similar explanationin a C<1..0> line emitted if the test is skipped completely: 1..0 # Skipped: no leverage found=head1 EXPORTC<&runtests> is exported by Test::Harness per default.=head1 DIAGNOSTICS=over 4=item C<All tests successful.\nFiles=%d, Tests=%d, %s>If all tests are successful some statistics about the performance areprinted.=item C<FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.>For any single script that has failing subtests statistics like theabove are printed.=item C<Test returned status %d (wstat %d)>Scripts that return a non-zero exit status, both C<$? E<gt>E<gt> 8> and C<$?> areprinted in a message similar to the above.=item C<Failed 1 test, %.2f%% okay. %s>=item C<Failed %d/%d tests, %.2f%% okay. %s>If not all tests were successful, the script dies with one of theabove messages.=back=head1 ENVIRONMENTSetting C<HARNESS_IGNORE_EXITCODE> makes harness ignore the exit statusof child processes.Setting C<HARNESS_NOTTY> to a true value forces it to behave as thoughSTDOUT were not a console. You may need to set this if you don't wantharness to output more frequent progress messages using carriage returns.Some consoles may not handle carriage returns properly (which resultsin a somewhat messy output).Setting C<HARNESS_COMPILE_TEST> to a true value will make harness attemptto compile the test using C<perlcc> before running it.If C<HARNESS_FILELEAK_IN_DIR> is set to the name of a directory, harnesswill check after each test whether new files appeared in that directory,and report them as LEAKED FILES: scr.tmp 0 my.dbIf relative, directory name is with respect to the current directory atthe moment runtests() was called. Putting absolute path into C<HARNESS_FILELEAK_IN_DIR> may give more predicatable results.The value of C<HARNESS_PERL_SWITCHES> will be prepended to theswitches used to invoke perl on each test. For example, settingC<HARNESS_PERL_SWITCHES> to "-W" will run all tests with allwarnings enabled.If C<HARNESS_COLUMNS> is set, then this value will be used for thewidth of the terminal. If it is not set then it will default toC<COLUMNS>. If this is not set, it will default to 80. Note that usersof Bourne-sh based shells will need to C<export COLUMNS> for thismodule to use that variable.Harness sets C<HARNESS_ACTIVE> before executing the individual tests.This allows the tests to determine if they are being executed through theharness or by any other means.=head1 SEE ALSOL<Test> for writing test scripts and also L<Benchmark> for theunderlying timing routines.=head1 AUTHORSEither Tim Bunce or Andreas Koenig, we don't know. What we know forsure is, that it was inspired by Larry Wall's TEST script that camewith perl distributions for ages. Numerous anonymous contributorsexist. Current maintainer is Andreas Koenig.=head1 BUGSTest::Harness uses $^X to determine the perl binary to run the testswith. Test scripts running via the shebang (C<#!>) line may not beportable because $^X is not consistent for shebang scripts acrossplatforms. This is no problem when Test::Harness is run with anabsolute path to the perl binary or when $^X can be found in the path.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -