5
# $^C was only introduced in 5.005-ish. We do this to prevent
6
# use of uninitialized value warnings in older perls.
10
use vars qw($VERSION $CLASS);
14
my $IsVMS = $^O eq 'VMS';
17
my @Test_Results = ();
18
my @Test_Details = ();
23
# Make Test::Builder thread-safe for ithreads.
26
if( $] >= 5.008 && $Config{useithreads} ) {
28
require threads::shared;
29
threads::shared->import;
31
share(\@Test_Details);
32
share(\@Test_Results);
42
Test::Builder - Backend for building test libraries
46
package My::Test::Module;
52
my $Test = Test::Builder->new;
53
$Test->output('my_logfile');
59
$Test->exported_to($pack);
62
$self->export_to_level(1, $self, 'ok');
66
my($test, $name) = @_;
68
$Test->ok($test, $name);
74
Test::Simple and Test::More have proven to be popular testing modules,
75
but they're not always flexible enough. Test::Builder provides the a
76
building block upon which to write your own test libraries I<which can
85
my $Test = Test::Builder->new;
87
Returns a Test::Builder object representing the current state of the
90
Since you only run one test per program, there is B<one and only one>
91
Test::Builder object. No matter how many times you call new(), you're
92
getting the same object. (This is called a singleton).
99
$Test ||= bless ['Move along, nothing to see here'], $class;
105
=head2 Setting up tests
107
These methods are for setting up tests and declaring how many there
108
are. You usually only want to call one of these methods.
114
my $pack = $Test->exported_to;
115
$Test->exported_to($pack);
117
Tells Test::Builder what package you exported your functions to.
118
This is important for getting TODO tests right.
124
my($self, $pack) = @_;
126
if( defined $pack ) {
127
$Exported_To = $pack;
134
$Test->plan('no_plan');
135
$Test->plan( skip_all => $reason );
136
$Test->plan( tests => $num_tests );
138
A convenient way to set up your tests. Call this and Test::Builder
139
will print the appropriate headers and take the appropriate actions.
141
If you call plan(), don't call any of the other methods below.
146
my($self, $cmd, $arg) = @_;
151
die sprintf "You tried to plan twice! Second plan at %s line %d\n",
152
($self->caller)[1,2];
155
if( $cmd eq 'no_plan' ) {
158
elsif( $cmd eq 'skip_all' ) {
159
return $self->skip_all($arg);
161
elsif( $cmd eq 'tests' ) {
163
return $self->expected_tests($arg);
165
elsif( !defined $arg ) {
166
die "Got an undefined number of tests. Looks like you tried to ".
167
"say how many tests you plan to run but made a mistake.\n";
170
die "You said to run 0 tests! You've got to run something.\n";
175
my @args = grep { defined } ($cmd, $arg);
176
Carp::croak("plan() doesn't understand @args");
182
=item B<expected_tests>
184
my $max = $Test->expected_tests;
185
$Test->expected_tests($max);
187
Gets/sets the # of tests we expect this test to run and prints out
188
the appropriate headers.
192
my $Expected_Tests = 0;
194
my($self, $max) = @_;
197
$Expected_Tests = $max;
200
$self->_print("1..$max\n") unless $self->no_header;
202
return $Expected_Tests;
210
Declares that this test will run an indeterminate # of tests.
223
$Test->skip_all($reason);
225
Skips all the tests, using the given $reason. Exits immediately with 0.
231
my($self, $reason) = @_;
234
$out .= " # Skip $reason" if $reason;
239
$self->_print($out) unless $self->no_header;
247
These actually run the tests, analogous to the functions in
250
$name is always optional.
256
$Test->ok($test, $name);
258
Your basic test. Pass if $test is true, fail if $test is false. Just
259
like Test::Simple's ok().
264
my($self, $test, $name) = @_;
266
unless( $Have_Plan ) {
268
Carp::croak("You tried to run a test without a plan! Gotta have a plan.");
274
$self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/;
275
You named your test '$name'. You shouldn't use numbers for your test names.
279
my($pack, $file, $line) = $self->caller;
281
my $todo = $self->todo($pack);
286
$Test_Results[$Curr_Test-1] = $todo ? 1 : 0;
289
$Test_Results[$Curr_Test-1] = 1;
293
$out .= " $Curr_Test" if $self->use_numbers;
295
if( defined $name ) {
296
$name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
301
my $what_todo = $todo;
302
$out .= " # TODO $what_todo";
310
my $msg = $todo ? "Failed (TODO)" : "Failed";
311
$self->diag(" $msg test ($file at line $line)\n");
314
return $test ? 1 : 0;
319
$Test->is_eq($got, $expected, $name);
321
Like Test::More's is(). Checks if $got eq $expected. This is the
326
$Test->is_num($got, $expected, $name);
328
Like Test::More's is(). Checks if $got == $expected. This is the
334
my($self, $got, $expect, $name) = @_;
335
local $Level = $Level + 1;
337
if( !defined $got || !defined $expect ) {
338
# undef only matches undef and nothing else
339
my $test = !defined $got && !defined $expect;
341
$self->ok($test, $name);
342
$self->_is_diag($got, 'eq', $expect) unless $test;
346
return $self->cmp_ok($got, 'eq', $expect, $name);
350
my($self, $got, $expect, $name) = @_;
351
local $Level = $Level + 1;
353
if( !defined $got || !defined $expect ) {
354
# undef only matches undef and nothing else
355
my $test = !defined $got && !defined $expect;
357
$self->ok($test, $name);
358
$self->_is_diag($got, '==', $expect) unless $test;
362
return $self->cmp_ok($got, '==', $expect, $name);
366
my($self, $got, $type, $expect) = @_;
368
foreach my $val (\$got, \$expect) {
369
if( defined $$val ) {
370
if( $type eq 'eq' ) {
371
# quote and force string context
375
# force numeric context
384
return $self->diag(sprintf <<DIAGNOSTIC, $got, $expect);
393
$Test->isnt_eq($got, $dont_expect, $name);
395
Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
400
$Test->is_num($got, $dont_expect, $name);
402
Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
408
my($self, $got, $dont_expect, $name) = @_;
409
local $Level = $Level + 1;
411
if( !defined $got || !defined $dont_expect ) {
412
# undef only matches undef and nothing else
413
my $test = defined $got || defined $dont_expect;
415
$self->ok($test, $name);
416
$self->_cmp_diag('ne', $got, $dont_expect) unless $test;
420
return $self->cmp_ok($got, 'ne', $dont_expect, $name);
424
my($self, $got, $dont_expect, $name) = @_;
425
local $Level = $Level + 1;
427
if( !defined $got || !defined $dont_expect ) {
428
# undef only matches undef and nothing else
429
my $test = defined $got || defined $dont_expect;
431
$self->ok($test, $name);
432
$self->_cmp_diag('!=', $got, $dont_expect) unless $test;
436
return $self->cmp_ok($got, '!=', $dont_expect, $name);
442
$Test->like($this, qr/$regex/, $name);
443
$Test->like($this, '/$regex/', $name);
445
Like Test::More's like(). Checks if $this matches the given $regex.
447
You'll want to avoid qr// if you want your tests to work before 5.005.
451
$Test->unlike($this, qr/$regex/, $name);
452
$Test->unlike($this, '/$regex/', $name);
454
Like Test::More's unlike(). Checks if $this B<does not match> the
460
my($self, $this, $regex, $name) = @_;
462
local $Level = $Level + 1;
463
$self->_regex_ok($this, $regex, '=~', $name);
467
my($self, $this, $regex, $name) = @_;
469
local $Level = $Level + 1;
470
$self->_regex_ok($this, $regex, '!~', $name);
475
$Test->maybe_regex(qr/$regex/);
476
$Test->maybe_regex('/$regex/');
478
Convenience method for building testing functions that take regular
479
expressions as arguments, but need to work before perl 5.005.
481
Takes a quoted regular expression produced by qr//, or a string
482
representing a regular expression.
484
Returns a Perl value which may be used instead of the corresponding
485
regular expression, or undef if it's argument is not recognised.
487
For example, a version of like(), sans the useful diagnostic messages,
491
my ($self, $this, $regex, $name) = @_;
492
my $usable_regex = $self->maybe_regex($regex);
493
die "expecting regex, found '$regex'\n"
494
unless $usable_regex;
495
$self->ok($this =~ m/$usable_regex/, $name);
502
my ($self, $regex) = @_;
503
my $usable_regex = undef;
504
if( ref $regex eq 'Regexp' ) {
505
$usable_regex = $regex;
507
# Check if it looks like '/foo/'
508
elsif( my($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx ) {
509
$usable_regex = length $opts ? "(?$opts)$re" : $re;
511
return($usable_regex)
515
my($self, $this, $regex, $cmp, $name) = @_;
517
local $Level = $Level + 1;
520
my $usable_regex = $self->maybe_regex($regex);
521
unless (defined $usable_regex) {
522
$ok = $self->ok( 0, $name );
523
$self->diag(" '$regex' doesn't look much like a regex to me.");
529
my $test = $this =~ /$usable_regex/ ? 1 : 0;
530
$test = !$test if $cmp eq '!~';
531
$ok = $self->ok( $test, $name );
535
$this = defined $this ? "'$this'" : 'undef';
536
my $match = $cmp eq '=~' ? "doesn't match" : "matches";
537
$self->diag(sprintf <<DIAGNOSTIC, $this, $match, $regex);
549
$Test->cmp_ok($this, $type, $that, $name);
551
Works just like Test::More's cmp_ok().
553
$Test->cmp_ok($big_num, '!=', $other_big_num);
558
my($self, $got, $type, $expect, $name) = @_;
563
local($@,$!); # don't interfere with $@
564
# eval() sometimes resets $!
565
$test = eval "\$got $type \$expect";
567
local $Level = $Level + 1;
568
my $ok = $self->ok($test, $name);
571
if( $type =~ /^(eq|==)$/ ) {
572
$self->_is_diag($got, $type, $expect);
575
$self->_cmp_diag($got, $type, $expect);
582
my($self, $got, $type, $expect) = @_;
584
$got = defined $got ? "'$got'" : 'undef';
585
$expect = defined $expect ? "'$expect'" : 'undef';
586
return $self->diag(sprintf <<DIAGNOSTIC, $got, $type, $expect);
595
$Test->BAILOUT($reason);
597
Indicates to the Test::Harness that things are going so badly all
598
testing should terminate. This includes running any additional test
601
It will exit with 255.
606
my($self, $reason) = @_;
608
$self->_print("Bail out! $reason");
617
Skips the current test, reporting $why.
622
my($self, $why) = @_;
625
unless( $Have_Plan ) {
627
Carp::croak("You tried to run tests without a plan! Gotta have a plan.");
633
$Test_Results[$Curr_Test-1] = 1;
636
$out .= " $Curr_Test" if $self->use_numbers;
637
$out .= " # skip $why\n";
648
$Test->todo_skip($why);
650
Like skip(), only it will declare the test as failing and TODO. Similar
653
print "not ok $tnum # TODO $why\n";
658
my($self, $why) = @_;
661
unless( $Have_Plan ) {
663
Carp::croak("You tried to run tests without a plan! Gotta have a plan.");
669
$Test_Results[$Curr_Test-1] = 1;
672
$out .= " $Curr_Test" if $self->use_numbers;
673
$out .= " # TODO & SKIP $why\n";
681
=begin _unimplemented
686
$Test->skip_rest($reason);
688
Like skip(), only it skips all the rest of the tests you plan to run
689
and terminates the test.
691
If you're running under no_plan, it skips once and terminates the
705
$Test->level($how_high);
707
How far up the call stack should $Test look when reporting where the
712
Setting $Test::Builder::Level overrides. This is typically useful
716
local $Test::Builder::Level = 2;
723
my($self, $level) = @_;
725
if( defined $level ) {
736
$Test->use_numbers($on_or_off);
738
Whether or not the test should output numbers. That is, this if true:
750
Most useful when you can't depend on the test output order, such as
751
when threads or forking is involved.
753
Test::Harness will accept either, but avoid mixing the two styles.
761
my($self, $use_nums) = @_;
763
if( defined $use_nums ) {
764
$Use_Nums = $use_nums;
771
$Test->no_header($no_header);
773
If set to true, no "1..N" header will be printed.
777
$Test->no_ending($no_ending);
779
Normally, Test::Builder does some extra diagnostics when the test
780
ends. It also changes the exit code as described in Test::Simple.
782
If this is true, none of that will be done.
786
my($No_Header, $No_Ending) = (0,0);
788
my($self, $no_header) = @_;
790
if( defined $no_header ) {
791
$No_Header = $no_header;
797
my($self, $no_ending) = @_;
799
if( defined $no_ending ) {
800
$No_Ending = $no_ending;
810
Controlling where the test output goes.
812
It's ok for your test to change where STDOUT and STDERR point to,
813
Test::Builder's default output settings will not be affected.
821
Prints out the given $message. Normally, it uses the failure_output()
822
handle, but if this is for a TODO test, the todo_output() handle is
825
Output will be indented and marked with a # so as not to interfere
826
with test output. A newline will be put on the end if there isn't one
829
We encourage using this rather than calling print directly.
831
Returns false. Why? Because diag() is often used in conjunction with
832
a failing test (C<ok() || diag()>) it "passes through" the failure.
834
return ok(...) || diag(...);
837
Mark Fowler <mark@twoshortplanks.com>
842
my($self, @msgs) = @_;
845
# Prevent printing headers when compiling (i.e. -c)
848
# Escape each line with a #.
850
$_ = 'undef' unless defined;
854
push @msgs, "\n" unless $msgs[-1] =~ /\n\Z/;
856
local $Level = $Level + 1;
857
my $fh = $self->todo ? $self->todo_output : $self->failure_output;
858
local($\, $", $,) = (undef, ' ', '');
868
$Test->_print(@msgs);
870
Prints to the output() filehandle.
877
my($self, @msgs) = @_;
879
# Prevent printing headers when only compiling. Mostly for when
880
# tests are deparsed with B::Deparse
883
local($\, $", $,) = (undef, ' ', '');
884
my $fh = $self->output;
886
# Escape each line after the first with a # so we don't
887
# confuse Test::Harness.
892
push @msgs, "\n" unless $msgs[-1] =~ /\n\Z/;
901
$Test->output($file);
903
Where normal "ok/not ok" test output should go.
907
=item B<failure_output>
909
$Test->failure_output($fh);
910
$Test->failure_output($file);
912
Where diagnostic output on test failures and diag() should go.
918
$Test->todo_output($fh);
919
$Test->todo_output($file);
921
Where diagnostics about todo test failures and diag() should go.
927
my($Out_FH, $Fail_FH, $Todo_FH);
932
$Out_FH = _new_fh($fh);
941
$Fail_FH = _new_fh($fh);
950
$Todo_FH = _new_fh($fh);
956
my($file_or_fh) = shift;
959
unless( UNIVERSAL::isa($file_or_fh, 'GLOB') ) {
960
$fh = do { local *FH };
961
open $fh, ">$file_or_fh" or
962
die "Can't open test output log $file_or_fh: $!";
972
# We dup STDOUT and STDERR so people can change them in their
973
# test suites while still getting normal test output.
974
open(TESTOUT, ">&STDOUT") or die "Can't dup STDOUT: $!";
975
open(TESTERR, ">&STDERR") or die "Can't dup STDERR: $!";
977
# Set everything to unbuffered else plain prints to STDOUT will
978
# come out in the wrong order from our own prints.
979
_autoflush(\*TESTOUT);
980
_autoflush(\*STDOUT);
981
_autoflush(\*TESTERR);
982
_autoflush(\*STDERR);
984
$CLASS->output(\*TESTOUT);
985
$CLASS->failure_output(\*TESTERR);
986
$CLASS->todo_output(\*TESTOUT);
991
my $old_fh = select $fh;
1000
=head2 Test Status and Info
1004
=item B<current_test>
1006
my $curr_test = $Test->current_test;
1007
$Test->current_test($num);
1009
Gets/sets the current test # we're on.
1011
You usually shouldn't have to set this.
1016
my($self, $num) = @_;
1019
if( defined $num ) {
1020
unless( $Have_Plan ) {
1022
Carp::croak("Can't change the current test number without a plan!");
1026
if( $num > @Test_Results ) {
1027
my $start = @Test_Results ? $#Test_Results : 0;
1028
for ($start..$num-1) {
1029
$Test_Results[$_] = 1;
1039
my @tests = $Test->summary;
1041
A simple summary of the tests so far. True for pass, false for fail.
1042
This is a logical pass/fail, so todos are passes.
1044
Of course, test #1 is $tests[0], etc...
1051
return @Test_Results;
1054
=item B<details> I<UNIMPLEMENTED>
1056
my @tests = $Test->details;
1058
Like summary(), but with a lot more detail.
1060
$tests[$test_num - 1] =
1061
{ ok => is the test considered ok?
1062
actual_ok => did it literally say 'ok'?
1063
name => name of the test (if any)
1064
type => 'skip' or 'todo' (if any)
1065
reason => reason for the above (if any)
1070
my $todo_reason = $Test->todo;
1071
my $todo_reason = $Test->todo($pack);
1073
todo() looks for a $TODO variable in your tests. If set, all tests
1074
will be considered 'todo' (see Test::More and Test::Harness for
1075
details). Returns the reason (ie. the value of $TODO) if running as
1076
todo tests, false otherwise.
1078
todo() is pretty part about finding the right package to look for
1079
$TODO in. It uses the exported_to() package to find it. If that's
1080
not set, it's pretty good at guessing the right package to look at.
1082
Sometimes there is some confusion about where todo() should be looking
1083
for the $TODO variable. If you want to be sure, tell it explicitly
1089
my($self, $pack) = @_;
1091
$pack = $pack || $self->exported_to || $self->caller(1);
1094
return defined ${$pack.'::TODO'} ? ${$pack.'::TODO'}
1100
my $package = $Test->caller;
1101
my($pack, $file, $line) = $Test->caller;
1102
my($pack, $file, $line) = $Test->caller($height);
1104
Like the normal caller(), except it reports according to your level().
1109
my($self, $height) = @_;
1112
my @caller = CORE::caller($self->level + $height + 1);
1113
return wantarray ? @caller : $caller[0];
1124
=item B<_sanity_check>
1128
Runs a bunch of end of test sanity checks to make sure reality came
1129
through ok. If anything is wrong it will die with a fairly friendly
1136
_whoa($Curr_Test < 0, 'Says here you ran a negative number of tests!');
1137
_whoa(!$Have_Plan and $Curr_Test,
1138
'Somehow your tests ran without a plan!');
1139
_whoa($Curr_Test != @Test_Results,
1140
'Somehow you got a different number of results than tests ran!');
1145
_whoa($check, $description);
1147
A sanity check, similar to assert(). If the $check is true, something
1148
has gone horribly wrong. It will die with the given $description and
1149
a note to contact the author.
1154
my($check, $desc) = @_;
1158
This should never happen! Please contact the author immediately!
1165
_my_exit($exit_num);
1167
Perl seems to have some trouble with exiting inside an END block. 5.005_03
1168
and 5.6.1 both seem to do odd things. Instead, this function edits $?
1169
directly. It should ONLY be called from inside an END block. It
1170
doesn't actually exit, that's your job.
1187
$SIG{__DIE__} = sub {
1188
# We don't want to muck with death in an eval, but $^S isn't
1189
# totally reliable. 5.005_03 and 5.6.1 both do the wrong thing
1190
# with it. Instead, we use caller. This also means it runs under
1193
for( my $stack = 1; my $sub = (CORE::caller($stack))[3]; $stack++ ) {
1194
$in_eval = 1 if $sub =~ /^\(eval\)/;
1196
$Test_Died = 1 unless $in_eval;
1204
# Bailout if plan() was never called. This is so
1205
# "require Test::Simple" doesn't puke.
1206
do{ _my_exit(0) && return } if !$Have_Plan;
1208
# Figure out if we passed or failed and print helpful messages.
1209
if( @Test_Results ) {
1210
# The plan? We have no plan.
1212
$self->_print("1..$Curr_Test\n") unless $self->no_header;
1213
$Expected_Tests = $Curr_Test;
1216
# 5.8.0 threads bug. Shared arrays will not be auto-extended
1218
$Test_Results[$Expected_Tests-1] = undef
1219
unless defined $Test_Results[$Expected_Tests-1];
1221
my $num_failed = grep !$_, @Test_Results[0..$Expected_Tests-1];
1222
$num_failed += abs($Expected_Tests - @Test_Results);
1224
if( $Curr_Test < $Expected_Tests ) {
1225
$self->diag(<<"FAIL");
1226
Looks like you planned $Expected_Tests tests but only ran $Curr_Test.
1229
elsif( $Curr_Test > $Expected_Tests ) {
1230
my $num_extra = $Curr_Test - $Expected_Tests;
1231
$self->diag(<<"FAIL");
1232
Looks like you planned $Expected_Tests tests but ran $num_extra extra.
1235
elsif ( $num_failed ) {
1236
$self->diag(<<"FAIL");
1237
Looks like you failed $num_failed tests of $Expected_Tests.
1242
$self->diag(<<"FAIL");
1243
Looks like your test died just after $Curr_Test.
1246
_my_exit( 255 ) && return;
1249
_my_exit( $num_failed <= 254 ? $num_failed : 254 ) && return;
1251
elsif ( $Skip_All ) {
1252
_my_exit( 0 ) && return;
1255
$self->diag("No tests run!\n");
1256
_my_exit( 255 ) && return;
1261
$Test->_ending if defined $Test and !$Test->no_ending;
1266
In perl 5.8.0 and later, Test::Builder is thread-safe. The test
1267
number is shared amongst all threads. This means if one thread sets
1268
the test number using current_test() they will all be effected.
1272
CPAN can provide the best examples. Test::Simple, Test::More,
1273
Test::Exception and Test::Differences all use Test::Builder.
1277
Test::Simple, Test::More, Test::Harness
1281
Original code by chromatic, maintained by Michael G Schwern
1282
E<lt>schwern@pobox.comE<gt>
1286
Copyright 2001 by chromatic E<lt>chromatic@wgz.orgE<gt>,
1287
Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1289
This program is free software; you can redistribute it and/or
1290
modify it under the same terms as Perl itself.
1292
See F<http://www.perl.com/perl/misc/Artistic.html>