NAME
Test::Builder - Backend for building test libraries
SYNOPSIS
package My::Test::Module;use Test::Builder;
require Exporter; @ISA = qw(Exporter); @EXPORT = qw(ok);my $Test = Test::Builder->new;
$Test->output('mylogfile');
sub import {my($self) = shift;
my $pack = caller;
$Test->exportedto($pack);
$Test->plan(@);
$self->exporttolevel(1, $self, 'ok');
} sub ok {my($test, $name) = @;
$Test->ok($test, $name);
}DESCRIPTION
Test::Simple and Test::More have proven to be popular testing modules,but they're not always flexible enough. Test::Builder provides the a
building block upon which to write your own test libraries which can work together. CCoonnssttrruuccttiioonn nneewwmy $Test = Test::Builder->new;
Returns a Test::Builder object representing the current state of
the test. Since you only run one test per program "new" always returns thesame Test::Builder object. No matter how many times you call
new(), you're getting the same object. This is called a singleton. This is done so that multiple modules share such global information as the test counter and where test output is going.If you want a completely new Test::Builder object different from
the singleton, use "create". ccrreeaatteemy $Test = Test::Builder->create;
Ok, so there can be more than one Test::Builder object and this is
how you get it. You might use this instead of "new()" if you'retesting a Test::Builder based module, but otherwise you probably
want "new". NNOOTTEE: the implementation is not complete. "level", for example, isstill shared amongst aallll Test::Builder objects, even ones created
using this method. Also, the method name may change in the future. rreesseett$Test->reset;
Reinitializes the Test::Builder singleton to its original state.
Mostly useful for tests run in persistent environments where the same test might be run multiple times in the same process. SSeettttiinngg uupp tteessttss These methods are for setting up tests and declaring how many there are. You usually only want to call one of these methods. eexxppoorrtteeddttoomy $pack = $Test->exportedto;
$Test->exportedto($pack);
Tells Test::Builder what package you exported your functions to.
This is important for getting TODO tests right. ppllaann$Test->plan('noplan');
$Test->plan( skipall => $reason );
$Test->plan( tests => $numtests );
A convenient way to set up your tests. Call this and Test::Builder
will print the appropriate headers and take the appropriate actions. If you call plan(), don't call any of the other methods below. eexxppeecctteeddtteessttssmy $max = $Test->expectedtests;
$Test->expectedtests($max);
Gets/sets the # of tests we expect this test to run and prints out
the appropriate headers. nnooppllaann$Test->noplan;
Declares that this test will run an indeterminate # of tests.
hhaassppllaann$plan = $Test->hasplan
Find out whether a plan has been defined. $plan is either "undef"
(no plan has been set), "noplan" (indeterminate # of tests) or an
integer (the number of expected tests). sskkiippaallll$Test->skipall;
$Test->skipall($reason);
Skips all the tests, using the given $reason. Exits immediately
with 0. RRuunnnniinngg tteessttss These actually run the tests, analogous to the functions in Test::More.$name is always optional.
ookk$Test->ok($test, $name);
Your basic test. Pass if $test is true, fail if $test is false.
Just like Test::Simple's ok(). iisseeqq$Test->iseq($got, $expected, $name);
Like Test::More's is(). Checks if $got eq $expected. This is the
string version. iissnnuumm$Test->isnum($got, $expected, $name);
Like Test::More's is(). Checks if $got == $expected. This is the
numeric version. iissnntteeqq$Test->isnteq($got, $dontexpect, $name);
Like Test::More's isnt(). Checks if $got ne $dontexpect. This is
the string version. iissnnttnnuumm$Test->isnum($got, $dontexpect, $name);
Like Test::More's isnt(). Checks if $got ne $dontexpect. This is
the numeric version. lliikkee$Test->like($this, qr/$regex/, $name);
$Test->like($this, '/$regex/', $name);
Like Test::More's like(). Checks if $this matches the given
$regex.
You'll want to avoid qr// if you want your tests to work before 5.005. uunnlliikkee$Test->unlike($this, qr/$regex/, $name);
$Test->unlike($this, '/$regex/', $name);
Like Test::More's unlike(). Checks if $this ddooeess nnoott mmaattcchh the
given $regex.
mmaayybbeerreeggeexx$Test->mayberegex(qr/$regex/);
$Test->mayberegex('/$regex/');
Convenience method for building testing functions that take regular expressions as arguments, but need to work before perl 5.005. Takes a quoted regular expression produced by qr//, or a string representing a regular expression. Returns a Perl value which may be used instead of the corresponding regular expression, or undef if it's argument is not recognised.For example, a version of like(), sans the useful diagnostic mes-
sages, could be written as: sub laconiclike {my ($self, $this, $regex, $name) = @;
my $usableregex = $self->mayberegex($regex);
die "expecting regex, found '$regex'\n"
unless $usableregex;
$self->ok($this =~ m/$usableregex/, $name);
} ccmmppookk$Test->cmpok($this, $type, $that, $name);
Works just like Test::More's cmpok().$Test->cmpok($bignum, '!=', $otherbignum);
BBAAIILLOOUUTT$Test->BAILOUT($reason);
Indicates to the Test::Harness that things are going so badly all testing should terminate. This includes running any additional test scripts. It will exit with 255. sskkiipp$Test->skip;
$Test->skip($why);
Skips the current test, reporting $why.
ttooddoosskkiipp$Test->todoskip;
$Test->todoskip($why);
Like skip(), only it will declare the test as failing and TODO. Similar toprint "not ok $tnum # TODO $why\n";
TTeesstt ssttyyllee lleevveell$Test->level($howhigh);
How far up the call stack should $Test look when reporting where
the test failed. Defaults to 1.Setting $Test::Builder::Level overrides. This is typically useful
localized: {local $Test::Builder::Level = 2;
$Test->ok($test);
} uusseennuummbbeerrss$Test->usenumbers($onoroff);
Whether or not the test should output numbers. That is, this if true: ok 1 ok 2 ok 3 or this if false ok ok ok Most useful when you can't depend on the test output order, such as when threads or forking is involved. Test::Harness will accept either, but avoid mixing the two styles. Defaults to on. nnooddiiaagg$Test->nodiag($nodiag);
If set true no diagnostics will be printed. This includes calls to diag(). nnooeennddiinngg$Test->noending($noending);
Normally, Test::Builder does some extra diagnostics when the test
ends. It also changes the exit code as described below. If this is true, none of that will be done. nnoohheeaaddeerr$Test->noheader($noheader);
If set to true, no "1..N" header will be printed. OOuuttppuutt Controlling where the test output goes. It's ok for your test to change where STDOUT and STDERR point to,Test::Builder's default output settings will not be affected.
ddiiaagg$Test->diag(@msgs);
Prints out the given @msgs. Like "print", arguments are simply appended together. Normally, it uses the failureoutput() handle, but if this is for a TODO test, the todooutput() handle is used.Output will be indented and marked with a # so as not to interfere
with test output. A newline will be put on the end if there isn't one already. We encourage using this rather than calling print directly. Returns false. Why? Because diag() is often used in conjunction with a failing test ("ok() || diag()") it "passes through" the failure. return ok(...) || diag(...); pprriinnttddiiaagg$Test->printdiag(@msg);
Like print, but prints to the current diagnostic filehandle. oouuttppuutt$Test->output($fh);
$Test->output($file);
Where normal "ok/not ok" test output should go. Defaults to STDOUT. ffaaiilluurreeoouuttppuutt$Test->failureoutput($fh);
$Test->failureoutput($file);
Where diagnostic output on test failures and diag() should go. Defaults to STDERR. ttooddoooouuttppuutt$Test->todooutput($fh);
$Test->todooutput($file);
Where diagnostics about todo test failures and diag() should go. Defaults to STDOUT. TTeesstt SSttaattuuss aanndd IInnffoo ccuurrrreenntttteessttmy $currtest = $Test->currenttest;
$Test->currenttest($num);
Gets/sets the current test number we're on. You usually shouldn't have to set this. If set forward, the details of the missing tests are filled in as 'unknown'. if set backward, the details of the intervening tests are deleted. You can erase history if you really want to. ssuummmmaarryymy @tests = $Test->summary;
A simple summary of the tests so far. True for pass, false for fail. This is a logical pass/fail, so todos are passes.Of course, test #1 is $tests[0], etc...
ddeettaaiillssmy @tests = $Test->details;
Like summary(), but with a lot more detail.$tests[$testnum - 1] =
{ 'ok' => is the test considered a pass? actualok => did it literally say 'ok'? name => name of the test (if any) type => type of test (if any, see below). reason => reason for the above (if any) }; 'ok' is true if Test::Harness will consider the test to be a pass. 'actualok' is a reflection of whether or not the test literally printed 'ok' or 'not ok'. This is for examining the result of 'todo' tests. 'name' is the name of the test. 'type' indicates if it was a special test. Normal tests have a type of ''. Type can be one of the following: skip see skip() todo see todo() todoskip see todoskip() unknown see belowSometimes the Test::Builder test counter is incremented without it
printing any test output, for example, when currenttest() ischanged. In these cases, Test::Builder doesn't know the result of
the test, so it's type is 'unkown'. These details for these tests are filled in. They are considered ok, but the name and actualok is left undef.For example "not ok 23 - hole count # TODO insufficient donuts"
would result in this structure:$tests[22] = # 23 - 1, since arrays start from 0.
{ ok => 1, # logically, the test passed since it's todo
actualok => 0, # in absolute terms, it failed
name => 'hole count', type => 'todo', reason => 'insufficient donuts' }; ttooddoomy $todoreason = $Test->todo;
my $todoreason = $Test->todo($pack);
todo() looks for a $TODO variable in your tests. If set, all tests
will be considered 'todo' (see Test::More and Test::Harness fordetails). Returns the reason (ie. the value of $TODO) if running
as todo tests, false otherwise.todo() is about finding the right package to look for $TODO in. It
uses the exportedto() package to find it. If that's not set, it's pretty good at guessing the right package to look at based on$Level.
Sometimes there is some confusion about where todo() should belooking for the $TODO variable. If you want to be sure, tell it
explicitly what $pack to use.
ccaalllleerrmy $package = $Test->caller;
my($pack, $file, $line) = $Test->caller;
my($pack, $file, $line) = $Test->caller($height);
Like the normal caller(), except it reports according to your level(). EEXXIITT CCOODDEESSIf all your tests passed, Test::Builder will exit with zero (which is
normal). If anything failed it will exit with how many failed. If you run less (or more) tests than you planned, the missing (or extras) willbe considered failures. If no tests were ever run Test::Builder will
throw a warning and exit with 255. If the test died, even after having successfully completed all its tests, it will still be considered a failure and will exit with 255. So the exit codes are... 0 all tests successful255 test died or all passed but wrong # of tests run
any other number how many failed (including missing or extras) If you fail more than 254 tests, it will be reported as 254. TTHHRREEAADDSSIn perl 5.8.0 and later, Test::Builder is thread-safe. The test number
is shared amongst all threads. This means if one thread sets the test number using currenttest() they will all be effected.Test::Builder is only thread-aware if threads.pm is loaded before
Test::Builder.
EEXXAAMMPPLLEESS CPAN can provide the best examples. Test::Simple, Test::More,Test::Exception and Test::Differences all use Test::Builder.
SEE ALSO
Test::Simple, Test::More, Test::Harness AUTHORSOriginal code by chromatic, maintained by Michael G Schwern
ern@pobox.com> COPYRIGHT Copyright 2002, 2004 by chromatic and Michael G Schwern . This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.8.8 2001-09-21 Test::Builder(3pm)