Seth Woolley's Man Viewer

Test::Builder(3) - Test::Builder - Backend for building test libraries - man 3 Test::Builder

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

Test::Builder(3)       Perl Programmers Reference Guide       Test::Builder(3)

       Test::Builder - Backend for building test libraries

         package My::Test::Module;
         use Test::Builder;
         require Exporter;
         @ISA = qw(Exporter);
         @EXPORT = qw(ok);

         my $Test = Test::Builder->new;

         sub import {
             my($self) = shift;
             my $pack(3,n,n pack-old) = caller;

             $Test->exported_to($pack(3,n,n pack-old));

             $self->export_to_level(1, $self, 'ok');

         sub ok {
             my($test, $name) = @_;

             $Test->ok($test, $name);

       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(1,2) your own test libraries which can
       work together.


             my $Test = Test::Builder->new;

           Returns a Test::Builder object representing the current state of
           the test.

           Since you only run one test per program, there is one and only one
           Test::Builder object.  No matter how many times you call new(),
           you're getting the same object.  (This is called a singleton).

       Setting up tests

       These methods are for setting up tests and declaring how many there
       are.  You usually only want to call one of these methods.

             my $pack(3,n,n pack-old) = $Test->exported_to;
             $Test->exported_to($pack(3,n,n pack-old));

           Tells Test::Builder what package you exported your functions to.
           This is important for getting TODO tests right.

             $Test->plan( skip_all => $reason );
             $Test->plan( tests => $num_tests );

           A convenient way to set(7,n,1 builtins) up your tests.  Call this and Test::Builder
           will print the appropriate headers and take the appropriate

           If you call plan(), don't call any of the other methods below.

               my $max = $Test->expected_tests;

           Gets/sets the # of tests we expect this test to run and prints out
           the appropriate headers.


           Declares that this test will run an indeterminate # of tests.

             $plan = $Test->has_plan

           Find out whether a plan has been defined. $plan is either "undef"
           (no plan has been set(7,n,1 builtins)), "no_plan" (indeterminate # of tests) or an
           integer (the number of expected tests).


           Skips all the tests, using the given $reason.  Exits immediately
           with 0.

       Running tests

       These actually run the tests, analogous to the functions in(1,8) Test::More.

       $name is always optional.

             $Test->ok($test, $name);

           Your basic test.  Pass if(3,n) $test is true, fail if(3,n) $test is false.
           Just like Test::Simple's ok().

             $Test->is_eq($got, $expected, $name);

           Like Test::More's is().  Checks if(3,n) $got eq $expected.  This is the
           string(3,n) version.

             $Test->is_num($got, $expected, $name);

           Like Test::More's is().  Checks if(3,n) $got == $expected.  This is the
           numeric version.

             $Test->isnt_eq($got, $dont_expect, $name);

           Like Test::More's isnt().  Checks if(3,n) $got ne $dont_expect.  This is
           the string(3,n) version.

             $Test->is_num($got, $dont_expect, $name);

           Like Test::More's isnt().  Checks if(3,n) $got ne $dont_expect.  This is
           the numeric version.

             $Test->like($this, qr/$regex(3,7)/, $name);
             $Test->like($this, '/$regex(3,7)/', $name);

           Like Test::More's like().  Checks if(3,n) $this matches the given

           You'll want to avoid qr// if(3,n) you want your tests to work before

             $Test->unlike($this, qr/$regex(3,7)/, $name);
             $Test->unlike($this, '/$regex(3,7)/', $name);

           Like Test::More's unlike().  Checks if(3,n) $this does not match the
           given $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(3,n)
           representing a regular expression.

           Returns a Perl value which may be used instead of the corresponding
           regular expression, or undef if(3,n) it's argument is not recognised.

           For example, a version(1,3,5) of like(), sans the useful diagnostic mes-
           sages, could be written as:

             sub laconic_like {
                 my ($self, $this, $regex(3,7), $name) = @_;
                 my $usable_regex = $self->maybe_regex($regex(3,7));
                 die "expecting regex(3,7), found '$regex(3,7)'\n"
                     unless $usable_regex;
                 $self->ok($this =~ m/$usable_regex/, $name);

             $Test->cmp_ok($this, $type, $that, $name);

           Works just like Test::More's cmp_ok().

               $Test->cmp_ok($big_num, '!=', $other_big_num);


           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(3,n,1 builtins) with 255.


           Skips the current test, reporting $why.


           Like skip(), only it will declare the test as failing and TODO.
           Similar to

               print "not ok $tnum # TODO $why\n";

       Test style


           How far up the call stack should $Test look(1,8,3 Search::Dict) when reporting where
           the test failed.

           Defaults to 1.

           Setting $Test::Builder::Level overrides.  This is typically useful

                   local $Test::Builder::Level = 2;


           Whether or not the test should output numbers.  That is, this if(3,n)

             ok 1
             ok 2
             ok 3

           or this if(3,n) false


           Most useful when you can't depend on the test output order, such as
           when threads or forking is involved.

           Test::Harness will accept(2,8) either, but avoid mixing the two styles.

           Defaults to on.


           If set(7,n,1 builtins) to true, no "1..N" header will be printed.


           Normally, Test::Builder does some extra diagnostics when the test
           ends.  It also changes the exit(3,n,1 builtins) code as described in(1,8) Test::Simple.

           If this is true, none of that will be done.


       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.


           Prints out the given $message.  Normally, it uses the failure_out-
           put() handle, but if(3,n) this is for a TODO test, the todo_output()
           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(3,n) there isn't
           one already.

           We encourage using this rather than calling print directly.

           Returns false.  Why?  Because diag() is often used in(1,8) conjunction
           with a failing test ("ok() || diag()") it "passes through" the

               return ok(...) || diag(...);


           Where normal "ok/not ok" test output should go.

           Defaults to STDOUT.


           Where diagnostic output on test failures and diag() should go.

           Defaults to STDERR.


           Where diagnostics about todo test failures and diag() should go.

           Defaults to STDOUT.

       Test Status and Info

               my $curr_test = $Test->current_test;

           Gets/sets the current test # we're on.

           You usually shouldn't have to set(7,n,1 builtins) this.

               my @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...

               my @tests = $Test->details;

           Like summary(), but with a lot more detail.

               $tests[$test_num - 1] =
                       { 'ok'       => is the test considered a pass?
                         actual_ok  => did it literally say 'ok'?
                         name       => name of the test (if(3,n) any)
                         type       => type of test (if(3,n) any, see below).
                         reason     => reason for the above (if(3,n) any)

           'ok' is true if(3,n) Test::Harness will consider the test to be a pass.

           'actual_ok' 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(3,n) it was a special test.  Normal tests have a
           type of ''.  Type can be one of the following:

               skip        see skip()
               todo        see todo()
               todo_skip   see todo_skip()
               unknown     see below

           Sometimes the Test::Builder test counter is incremented without it
           printing any test output, for example, when current_test() is
           changed.  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 actual_ok
           is left undef.

           For example "not ok 23 - hole count # TODO insufficient donuts"
           would result in(1,8) this structure:

               $tests[22] =    # 23 - 1, since arrays start from 0.
                 { ok        => 1,   # logically, the test passed since it's todo
                   actual_ok => 0,   # in(1,8) absolute terms, it failed
                   name      => 'hole count',
                   type      => 'todo',
                   reason    => 'insufficient donuts'

               my $todo_reason = $Test->todo;
               my $todo_reason = $Test->todo($pack(3,n,n pack-old));

           todo() looks for a $TODO variable in(1,8) your tests.  If set(7,n,1 builtins), all tests
           will be considered 'todo' (see Test::More and Test::Harness for
           details).  Returns the reason (ie. the value of $TODO) if(3,n) running
           as todo tests, false otherwise.

           todo() is pretty part about finding the right package to look(1,8,3 Search::Dict) for
           $TODO in.  It uses the exported_to() package to find it.  If that's
           not set(7,n,1 builtins), it's pretty good at guessing the right package to look(1,8,3 Search::Dict) at.

           Sometimes there is some confusion about where todo() should be
           looking for the $TODO variable.  If you want to be sure, tell it
           explicitly what $pack(3,n,n pack-old) to use.

               my $package = $Test->caller;
               my($pack(3,n,n pack-old), $file(1,n), $line) = $Test->caller;
               my($pack(3,n,n pack-old), $file(1,n), $line) = $Test->caller($height);

           Like the normal caller(), except it reports according to your

       In perl 5.8.0 and later, Test::Builder is thread-safe.  The test number
       is shared amongst all threads.  This means if(3,n) one thread sets the test
       number using current_test() they will all be effected.

       CPAN can provide the best examples.  Test::Simple, Test::More,
       Test::Exception and Test::Differences all use Test::Builder.

       Test::Simple, Test::More, Test::Harness

       Original code by chromatic, maintained by Michael G Schwern <schw->

       Copyright 2002 by chromatic <>,
                         Michael G Schwern <>.

       This program is free software; you can redistribute it and/or modify it
       under the same terms as Perl itself.


perl v5.8.5                       2001-09-21                  Test::Builder(3)

References for this manual (incoming links)