Commit | Line | Data |
a0d0e21e |
1 | package Test::Harness; |
2 | |
3 | use Exporter; |
4 | use Benchmark; |
4633a7c4 |
5 | use Config; |
6 | |
7 | $Is_OS2 = $Config{'osname'} =~ m|^os/?2$|i ; |
8 | |
9 | $ENV{EMXSHELL} = 'sh' if $Is_OS2; # to run commands |
10 | $path_s = $Is_OS2 ? ';' : ':' ; |
11 | |
a0d0e21e |
12 | @ISA=(Exporter); |
cb1a09d0 |
13 | @EXPORT= qw(&runtests); |
a0d0e21e |
14 | @EXPORT_OK= qw($verbose $switches); |
15 | |
16 | $verbose = 0; |
17 | $switches = "-w"; |
18 | |
19 | sub runtests { |
20 | my(@tests) = @_; |
21 | local($|) = 1; |
22 | my($test,$te,$ok,$next,$max,$totmax, $files,$pct); |
23 | my $bad = 0; |
24 | my $good = 0; |
25 | my $total = @tests; |
4633a7c4 |
26 | local($ENV{'PERL5LIB'}) = join($path_s, @INC); # pass -I flags to children |
a0d0e21e |
27 | |
28 | my $t_start = new Benchmark; |
29 | while ($test = shift(@tests)) { |
30 | $te = $test; |
31 | chop($te); |
32 | print "$te" . '.' x (20 - length($te)); |
33 | my $fh = "RESULTS"; |
34 | open($fh,"$^X $switches $test|") || (print "can't run. $!\n"); |
35 | $ok = 0; |
36 | $next = 0; |
37 | while (<$fh>) { |
38 | if( $verbose ){ |
39 | print $_; |
40 | } |
41 | unless (/^#/) { |
42 | if (/^1\.\.([0-9]+)/) { |
43 | $max = $1; |
44 | $totmax += $max; |
45 | $files += 1; |
46 | $next = 1; |
47 | $ok = 1; |
48 | } else { |
49 | $next = $1, $ok = 0, last if /^not ok ([0-9]*)/; |
50 | if (/^ok (.*)/ && $1 == $next) { |
51 | $next = $next + 1; |
52 | } |
53 | } |
54 | } |
55 | } |
56 | close($fh); # must close to reap child resource values |
57 | $next -= 1; |
58 | if ($ok && $next == $max) { |
59 | print "ok\n"; |
60 | $good += 1; |
61 | } else { |
62 | $next += 1; |
63 | print "FAILED on test $next\n"; |
64 | $bad += 1; |
65 | $_ = $test; |
66 | } |
67 | } |
68 | my $t_total = timediff(new Benchmark, $t_start); |
69 | |
70 | if ($bad == 0) { |
71 | if ($ok) { |
72 | print "All tests successful.\n"; |
73 | } else { |
74 | die "FAILED--no tests were run for some reason.\n"; |
75 | } |
76 | } else { |
77 | $pct = sprintf("%.2f", $good / $total * 100); |
78 | if ($bad == 1) { |
4633a7c4 |
79 | die "Failed 1 test, $pct% okay.\n"; |
a0d0e21e |
80 | } else { |
81 | die "Failed $bad/$total tests, $pct% okay.\n"; |
82 | } |
83 | } |
84 | printf("Files=%d, Tests=%d, %s\n", $files,$totmax, timestr($t_total, 'nop')); |
85 | } |
86 | |
87 | 1; |
cb1a09d0 |
88 | __END__ |
89 | |
90 | =head1 NAME |
91 | |
92 | Test::Harness - run perl standard test scripts with statistics |
93 | |
94 | =head1 SYNOPSIS |
95 | |
96 | use Test::Harness; |
97 | |
98 | runtests(@tests); |
99 | |
100 | =head1 DESCRIPTION |
101 | |
102 | Perl test scripts print to standard output C<"ok N"> for each single |
103 | test, where C<N> is an increasing sequence of integers. The first line |
104 | output by a standard test scxript is C<"1..M"> with C<M> being the |
105 | number of tests that should be run within the test |
106 | script. Test::Harness::runscripts(@tests) runs all the testscripts |
107 | named as arguments and checks standard output for the expected |
108 | C<"ok N"> strings. |
109 | |
110 | After all tests have been performed, runscripts() prints some |
111 | performance statistics that are computed by the Benchmark module. |
112 | |
113 | =head1 EXPORT |
114 | |
115 | C<&runscripts> is exported by Test::Harness per default. |
116 | |
117 | =head1 DIAGNOSTICS |
118 | |
119 | =over 4 |
120 | |
121 | =item C<All tests successful.\nFiles=%d, Tests=%d, %s> |
122 | |
123 | If all tests are successful some statistics about the performance are |
124 | printed. |
125 | |
126 | =item C<Failed 1 test, $pct% okay.> |
127 | |
128 | =item C<Failed %d/%d tests, %.2f%% okay.> |
129 | |
130 | If not all tests were successful, the script dies with one of the |
131 | above messages. |
132 | |
133 | =back |
134 | |
135 | =head1 SEE ALSO |
136 | |
137 | See L<Benchmerk> for the underlying timing routines. |
138 | |
139 | =head1 BUGS |
140 | |
141 | Test::Harness uses $^X to determine the perl binary to run the tests |
142 | with. Test scripts running via the shebang (C<#!>) line may not be portable |
143 | because $^X is not consistent for shebang scripts across |
144 | platforms. This is no problem when Test::Harness is run with an |
145 | absolute path to the perl binary. |
146 | |
147 | =cut |