8 use constant IS_WIN32 => ( $^O =~ /^(MS)?Win32$/ );
9 use constant IS_VMS => ( $^O eq 'VMS' );
12 use TAP::Parser::Aggregator ();
13 use TAP::Parser::Source ();
14 use TAP::Parser::SourceHandler::Perl ();
16 use Text::ParseWords qw(shellwords);
24 eval q{use Time::HiRes 'time'};
25 our $has_time_hires = !$@;
30 Test::Harness - Run Perl standard test scripts with statistics
38 our $VERSION = '3.42';
40 # Backwards compatibility for exportable variable names.
42 *switches = *Switches;
45 $ENV{HARNESS_ACTIVE} = 1;
46 $ENV{HARNESS_VERSION} = $VERSION;
51 delete $ENV{HARNESS_ACTIVE};
52 delete $ENV{HARNESS_VERSION};
55 our @EXPORT = qw(&runtests);
56 our @EXPORT_OK = qw(&execute_tests $verbose $switches);
58 our $Verbose = $ENV{HARNESS_VERBOSE} || 0;
59 our $Debug = $ENV{HARNESS_DEBUG} || 0;
61 our $Columns = $ENV{HARNESS_COLUMNS} || $ENV{COLUMNS} || 80;
62 $Columns--; # Some shells have trouble with a full line of text.
63 our $Timer = $ENV{HARNESS_TIMER} || 0;
64 our $Color = $ENV{HARNESS_COLOR} || 0;
65 our $IgnoreExit = $ENV{HARNESS_IGNORE_EXIT} || 0;
71 runtests(@test_files);
75 Although, for historical reasons, the L<Test::Harness> distribution
76 takes its name from this module it now exists only to provide
77 L<TAP::Harness> with an interface that is somewhat backwards compatible
78 with L<Test::Harness> 2.xx. If you're writing new code consider using
79 L<TAP::Harness> directly instead.
81 Emulation is provided for C<runtests> and C<execute_tests> but the
82 pluggable 'Straps' interface that previous versions of L<Test::Harness>
83 supported is not reproduced here. Straps is now available as a stand
84 alone module: L<Test::Harness::Straps>.
86 See L<TAP::Parser>, L<TAP::Harness> for the main documentation for this
91 The following functions are available.
93 =head2 runtests( @test_files )
95 This runs all the given I<@test_files> and divines whether they passed
96 or failed based on their output to STDOUT (details above). It prints
97 out each individual test which failed along with a summary report and
98 a how long it all took.
100 It returns true if everything was ok. Otherwise it will C<die()> with
101 one of the messages in the DIAGNOSTICS section.
107 return TAP::Parser::SourceHandler::Perl->get_taint(
108 TAP::Parser::Source->shebang($test) );
112 my ( $harness, $aggregate, @tests ) = @_;
114 # Don't propagate to our children
115 local $ENV{HARNESS_OPTIONS};
117 _apply_extra_INC($harness);
118 _aggregate_tests( $harness, $aggregate, @tests );
121 # Make sure the child sees all the extra junk in @INC
122 sub _apply_extra_INC {
127 my ( $args, $test ) = @_;
128 push @{ $args->{switches} }, map {"-I$_"} _filtered_inc();
133 sub _aggregate_tests {
134 my ( $harness, $aggregate, @tests ) = @_;
136 $harness->aggregate_tests( $aggregate, @tests );
147 my $harness = _new_harness();
148 my $aggregate = TAP::Parser::Aggregator->new();
150 local $ENV{PERL_USE_UNSAFE_INC} = 1 if not exists $ENV{PERL_USE_UNSAFE_INC};
151 _aggregate( $harness, $aggregate, @tests );
153 $harness->formatter->summary($aggregate);
155 my $total = $aggregate->total;
156 my $passed = $aggregate->passed;
157 my $failed = $aggregate->failed;
159 my @parsers = $aggregate->parsers;
162 for my $parser (@parsers) {
163 $num_bad++ if $parser->has_problems;
167 "Failed %d/%d test programs. %d/%d subtests failed.\n",
168 $num_bad, scalar @parsers, $failed, $total
172 return $total && $total == $passed;
176 my @list = sort { $a <=> $b } @_;
178 my $count = scalar @list;
181 while ( $pos < $count ) {
183 $end++ while $end < $count && $list[$end] <= $list[ $end - 1 ] + 1;
184 push @ranges, ( $end == $pos + 1 )
186 : join( '-', $list[$pos], $list[ $end - 1 ] );
190 return join( ' ', @ranges );
194 my $sub_args = shift || {};
196 my ( @lib, @switches );
197 my @opt = map { shellwords($_) } grep { defined } $Switches, $ENV{HARNESS_PERL_SWITCHES};
198 while ( my $opt = shift @opt ) {
199 if ( $opt =~ /^ -I (.*) $ /x ) {
200 push @lib, length($1) ? $1 : shift @opt;
203 push @switches, $opt;
207 # Do things the old way on VMS...
208 push @lib, _filtered_inc() if IS_VMS;
210 # If $Verbose isn't numeric default to 1. This helps core.
211 my $verbosity = ( $Verbose ? ( $Verbose !~ /\d/ ) ? 1 : $Verbose : 0 );
215 directives => our $Directives,
217 switches => \@switches,
219 verbosity => $verbosity,
220 ignore_exit => $IgnoreExit,
223 $args->{stdout} = $sub_args->{out}
224 if exists $sub_args->{out};
226 my $class = $ENV{HARNESS_SUBCLASS} || 'TAP::Harness';
227 if ( defined( my $env_opt = $ENV{HARNESS_OPTIONS} ) ) {
228 for my $opt ( split /:/, $env_opt ) {
229 if ( $opt =~ /^j(\d*)$/ ) {
230 $args->{jobs} = $1 || 9;
232 elsif ( $opt eq 'c' ) {
235 elsif ( $opt =~ m/^f(.*)$/ ) {
238 $args->{formatter_class} = $fmt;
240 elsif ( $opt =~ m/^a(.*)$/ ) {
242 $class = "TAP::Harness::Archive";
243 $args->{archive} = $archive;
246 die "Unknown HARNESS_OPTIONS item: $opt\n";
251 return TAP::Harness->_construct( $class, $args );
254 # Get the parts of @INC which are changed from the stock list AND
255 # preserve reordering of stock directories.
257 my @inc = grep { !ref } @INC; #28567
261 # VMS has a 255-byte limit on the length of %ENV entries, so
262 # toss the ones that involve perl_root, the install location
263 @inc = grep !/perl_root/i, @inc;
268 # Lose any trailing backslashes in the Win32 paths
269 s/[\\\/]+$// for @inc;
272 my @default_inc = _default_inc();
277 next if $seen{$dir}++;
279 if ( $dir eq ( $default_inc[0] || '' ) ) {
286 shift @default_inc while @default_inc and $seen{ $default_inc[0] };
294 # Cache this to avoid repeatedly shelling out to Perl.
300 local $ENV{PERL5LIB};
303 my $perl = $ENV{HARNESS_PERL} || $^X;
305 # Avoid using -l for the benefit of Perl 6
306 chomp( @inc = `"$perl" -e "print join qq[\\n], \@INC, q[]"` );
311 sub _check_sequence {
314 while ( my $next = shift @list ) {
315 return if defined $prev && $next <= $prev;
325 my $harness = _new_harness( \%args );
326 my $aggregate = TAP::Parser::Aggregator->new();
342 # Install a callback so we get to see any plans the
350 if ( $plan->directive eq 'SKIP' ) {
358 local $ENV{PERL_USE_UNSAFE_INC} = 1 if not exists $ENV{PERL_USE_UNSAFE_INC};
359 _aggregate( $harness, $aggregate, @{ $args{tests} } );
361 $tot{bench} = $aggregate->elapsed;
362 my @tests = $aggregate->descriptions;
364 # TODO: Work out the circumstances under which the files
365 # and tests totals can differ.
366 $tot{files} = $tot{tests} = scalar @tests;
368 my %failedtests = ();
369 my %todo_passed = ();
371 for my $test (@tests) {
372 my ($parser) = $aggregate->parsers($test);
374 my @failed = $parser->failed;
376 my $wstat = $parser->wait;
377 my $estat = $parser->exit;
378 my $planned = $parser->tests_planned;
379 my @errors = $parser->parse_errors;
380 my $passed = $parser->passed;
381 my $actual_passed = $parser->actual_passed;
383 my $ok_seq = _check_sequence( $parser->actual_passed );
385 # Duplicate exit, wait status semantics of old version
386 $estat ||= '' unless $wstat;
389 $tot{max} += ( $planned || 0 );
390 $tot{bonus} += $parser->todo_passed;
391 $tot{ok} += $passed > $actual_passed ? $passed : $actual_passed;
392 $tot{sub_skipped} += $parser->skipped;
393 $tot{todo} += $parser->todo;
395 if ( @failed || $estat || @errors ) {
398 my $huh_planned = $planned ? undef : '??';
399 my $huh_errors = $ok_seq ? undef : '??';
401 $failedtests{$test} = {
402 'canon' => $huh_planned
407 'failed' => $huh_planned
410 'max' => $huh_planned || $planned,
419 my @todo = $parser->todo_passed;
421 $todo_passed{$test} = {
422 'canon' => _canon(@todo),
424 'failed' => scalar @todo,
425 'max' => scalar $parser->todo,
432 return ( \%tot, \%failedtests, \%todo_passed );
435 =head2 execute_tests( tests => \@test_files, out => \*FH )
437 Runs all the given C<@test_files> (just like C<runtests()>) but
438 doesn't generate the final report. During testing, progress
439 information will be written to the currently selected output
440 filehandle (usually C<STDOUT>), or to the filehandle given by the
441 C<out> parameter. The I<out> is optional.
443 Returns a list of two values, C<$total> and C<$failed>, describing the
444 results. C<$total> is a hash ref summary of all the tests run. Its
445 keys and values are this:
447 bonus Number of individual todo tests unexpectedly passed
448 max Number of individual tests ran
449 ok Number of individual tests passed
450 sub_skipped Number of individual tests skipped
451 todo Number of individual todo tests
453 files Number of test files ran
454 good Number of test files passed
455 bad Number of test files failed
456 tests Number of test files originally given
457 skipped Number of test files skipped
459 If C<< $total->{bad} == 0 >> and C<< $total->{max} > 0 >>, you've
460 got a successful test.
462 C<$failed> is a hash ref of all the test scripts that failed. Each key
463 is the name of a test script, each value is another hash representing
464 how that script failed. Its keys are these:
466 name Name of the test which failed
467 estat Script's exit value
468 wstat Script's wait status
469 max Number of individual tests
470 failed Number which failed
471 canon List of tests which failed (as string).
473 C<$failed> should be empty if everything passed.
482 C<&runtests> is exported by C<Test::Harness> by default.
484 C<&execute_tests>, C<$verbose>, C<$switches> and C<$debug> are
485 exported upon request.
487 =head1 ENVIRONMENT VARIABLES THAT TAP::HARNESS::COMPATIBLE SETS
489 C<Test::Harness> sets these before executing the individual tests.
493 =item C<HARNESS_ACTIVE>
495 This is set to a true value. It allows the tests to determine if they
496 are being executed through the harness or by any other means.
498 =item C<HARNESS_VERSION>
500 This is the version of C<Test::Harness>.
504 =head1 ENVIRONMENT VARIABLES THAT AFFECT TEST::HARNESS
508 =item C<HARNESS_PERL_SWITCHES>
510 Setting this adds perl command line switches to each test file run.
512 For example, C<HARNESS_PERL_SWITCHES=-T> will turn on taint mode.
513 C<HARNESS_PERL_SWITCHES=-MDevel::Cover> will run C<Devel::Cover> for
516 C<-w> is always set. You can turn this off in the test with C<BEGIN {
519 =item C<HARNESS_TIMER>
521 Setting this to true will make the harness display the number of
522 milliseconds each test took. You can also use F<prove>'s C<--timer>
525 =item C<HARNESS_VERBOSE>
527 If true, C<Test::Harness> will output the verbose results of running
528 its tests. Setting C<$Test::Harness::verbose> will override this,
529 or you can use the C<-v> switch in the F<prove> utility.
531 =item C<HARNESS_OPTIONS>
533 Provide additional options to the harness. Currently supported options are:
539 Run <n> (default 9) parallel jobs.
543 Try to color output. See L<TAP::Formatter::Base/"new">.
545 =item C<< a<file.tgz> >>
547 Will use L<TAP::Harness::Archive> as the harness class, and save the TAP to
550 =item C<< fPackage-With-Dashes >>
552 Set the formatter_class of the harness being run. Since the C<HARNESS_OPTIONS>
553 is seperated by C<:>, we use C<-> instead.
557 Multiple options may be separated by colons:
559 HARNESS_OPTIONS=j9:c make test
561 =item C<HARNESS_SUBCLASS>
563 Specifies a TAP::Harness subclass to be used in place of TAP::Harness.
565 =item C<HARNESS_SUMMARY_COLOR_SUCCESS>
567 Determines the L<Term::ANSIColor> for the summary in case it is successful.
568 This color defaults to C<'green'>.
570 =item C<HARNESS_SUMMARY_COLOR_FAIL>
572 Determines the L<Term::ANSIColor> for the failure in case it is successful.
573 This color defaults to C<'red'>.
579 Normally when a Perl program is run in taint mode the contents of the
580 C<PERL5LIB> environment variable do not appear in C<@INC>.
582 Because C<PERL5LIB> is often used during testing to add build
583 directories to C<@INC> C<Test::Harness> passes the names of any
584 directories found in C<PERL5LIB> as -I switches. The net effect of this
585 is that C<PERL5LIB> is honoured even in taint mode.
593 Please report any bugs or feature requests to
594 C<bug-test-harness at rt.cpan.org>, or through the web interface at
595 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Harness>. I will be
596 notified, and then you'll automatically be notified of progress on your bug
601 Andy Armstrong C<< <andy@hexten.net> >>
603 L<Test::Harness> 2.64 (maintained by Andy Lester and on which this
604 module is based) has this attribution:
606 Either Tim Bunce or Andreas Koenig, we don't know. What we know for
607 sure is, that it was inspired by Larry Wall's F<TEST> script that came
608 with perl distributions for ages. Numerous anonymous contributors
609 exist. Andreas Koenig held the torch for many years, and then
612 =head1 LICENCE AND COPYRIGHT
614 Copyright (c) 2007-2011, Andy Armstrong C<< <andy@hexten.net> >>. All rights reserved.
616 This module is free software; you can redistribute it and/or
617 modify it under the same terms as Perl itself. See L<perlartistic>.