Perl version

Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Test::Builder

Perl 5 version 8.8 documentation
Recently read

Test::Builder

NAME

Test::Builder - Backend for building test libraries

SYNOPSIS

  1. package My::Test::Module;
  2. use Test::Builder;
  3. require Exporter;
  4. @ISA = qw(Exporter);
  5. @EXPORT = qw(ok);
  6. my $Test = Test::Builder->new;
  7. $Test->output('my_logfile');
  8. sub import {
  9. my($self) = shift;
  10. my $pack = caller;
  11. $Test->exported_to($pack);
  12. $Test->plan(@_);
  13. $self->export_to_level(1, $self, 'ok');
  14. }
  15. sub ok {
  16. my($test, $name) = @_;
  17. $Test->ok($test, $name);
  18. }

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.

Construction

  • new
    1. 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 new always returns the same 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 .

  • create
    1. my $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're testing a Test::Builder based module, but otherwise you probably want new .

    NOTE: the implementation is not complete. level , for example, is still shared amongst all Test::Builder objects, even ones created using this method. Also, the method name may change in the future.

  • reset
    1. $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.

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.

  • exported_to
    1. my $pack = $Test->exported_to;
    2. $Test->exported_to($pack);

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

  • plan
    1. $Test->plan('no_plan');
    2. $Test->plan( skip_all => $reason );
    3. $Test->plan( tests => $num_tests );

    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.

  • expected_tests
    1. my $max = $Test->expected_tests;
    2. $Test->expected_tests($max);

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

  • no_plan
    1. $Test->no_plan;

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

  • has_plan
    1. $plan = $Test->has_plan

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

  • skip_all
    1. $Test->skip_all;
    2. $Test->skip_all($reason);

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

Running tests

These actually run the tests, analogous to the functions in Test::More.

$name is always optional.

  • ok
    1. $Test->ok($test, $name);

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

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

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

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

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

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

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

  • isnt_num
    1. $Test->is_num($got, $dont_expect, $name);

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

  • like
    1. $Test->like($this, qr/$regex/, $name);
    2. $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.

  • unlike
    1. $Test->unlike($this, qr/$regex/, $name);
    2. $Test->unlike($this, '/$regex/', $name);

    Like Test::More's unlike(). Checks if $this does not match the given $regex.

  • maybe_regex
    1. $Test->maybe_regex(qr/$regex/);
    2. $Test->maybe_regex('/$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 messages, could be written as:

    1. sub laconic_like {
    2. my ($self, $this, $regex, $name) = @_;
    3. my $usable_regex = $self->maybe_regex($regex);
    4. die "expecting regex, found '$regex'\n"
    5. unless $usable_regex;
    6. $self->ok($this =~ m/$usable_regex/, $name);
    7. }
  • cmp_ok
    1. $Test->cmp_ok($this, $type, $that, $name);

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

    1. $Test->cmp_ok($big_num, '!=', $other_big_num);
  • BAIL_OUT
    1. $Test->BAIL_OUT($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.

  • skip
    1. $Test->skip;
    2. $Test->skip($why);

    Skips the current test, reporting $why.

  • todo_skip
    1. $Test->todo_skip;
    2. $Test->todo_skip($why);

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

    1. print "not ok $tnum # TODO $why\n";
  • skip_rest
    1. $Test->skip_rest;
    2. $Test->skip_rest($reason);

    Like skip(), only it skips all the rest of the tests you plan to run and terminates the test.

    If you're running under no_plan, it skips once and terminates the test.

Test style

  • level
    1. $Test->level($how_high);

    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:

    1. {
    2. local $Test::Builder::Level = 2;
    3. $Test->ok($test);
    4. }
  • use_numbers
    1. $Test->use_numbers($on_or_off);

    Whether or not the test should output numbers. That is, this if true:

    1. ok 1
    2. ok 2
    3. ok 3

    or this if false

    1. ok
    2. ok
    3. 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.

  • no_diag
    1. $Test->no_diag($no_diag);

    If set true no diagnostics will be printed. This includes calls to diag().

  • no_ending
    1. $Test->no_ending($no_ending);

    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.

  • no_header
    1. $Test->no_header($no_header);

    If set to true, no "1..N" header will be printed.

Output

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.

  • diag
    1. $Test->diag(@msgs);

    Prints out the given @msgs. Like C, arguments are simply appended together.

    Normally, it uses the failure_output() handle, but if 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 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 (C) it "passes through" the failure.

    1. return ok(...) || diag(...);
  • _print
    1. $Test->_print(@msgs);

    Prints to the output() filehandle.

  1. $Test->_print_diag(@msg);

Like _print, but prints to the current diagnostic filehandle.

  1. $Test->output($fh);
  2. $Test->output($file);

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

Defaults to STDOUT.

  1. $Test->failure_output($fh);
  2. $Test->failure_output($file);

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

Defaults to STDERR.

  1. $Test->todo_output($fh);
  2. $Test->todo_output($file);

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

Defaults to STDOUT.

Test Status and Info

  • current_test
    1. my $curr_test = $Test->current_test;
    2. $Test->current_test($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.

  • summary
    1. 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...

  • details
    1. my @tests = $Test->details;

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

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

    'ok' is true if 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 it was a special test. Normal tests have a type of ''. Type can be one of the following:

    1. skip see skip()
    2. todo see todo()
    3. todo_skip see todo_skip()
    4. 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 this structure:

    1. $tests[22] = # 23 - 1, since arrays start from 0.
    2. { ok => 1, # logically, the test passed since it's todo
    3. actual_ok => 0, # in absolute terms, it failed
    4. name => 'hole count',
    5. type => 'todo',
    6. reason => 'insufficient donuts'
    7. };
  • todo
    1. my $todo_reason = $Test->todo;
    2. my $todo_reason = $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 for details). 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 exported_to() 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 be looking for the $TODO variable. If you want to be sure, tell it explicitly what $pack to use.

  • caller
    1. my $package = $Test->caller;
    2. my($pack, $file, $line) = $Test->caller;
    3. my($pack, $file, $line) = $Test->caller($height);

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

  • _sanity_check
    1. $self->_sanity_check();

    Runs a bunch of end of test sanity checks to make sure reality came through ok. If anything is wrong it will die with a fairly friendly error message.

  • _whoa
    1. _whoa($check, $description);

    A sanity check, similar to assert(). If the $check is true, something has gone horribly wrong. It will die with the given $description and a note to contact the author.

  • _my_exit
    1. _my_exit($exit_num);

    Perl seems to have some trouble with exiting inside an END block. 5.005_03 and 5.6.1 both seem to do odd things. Instead, this function edits $? directly. It should ONLY be called from inside an END block. It doesn't actually exit, that's your job.

EXIT CODES

If 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) will be 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...

  1. 0 all tests successful
  2. 255 test died or all passed but wrong # of tests run
  3. any other number how many failed (including missing or extras)

If you fail more than 254 tests, it will be reported as 254.

THREADS

In 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 current_test() they will all be effected.

Test::Builder is only thread-aware if threads.pm is loaded I Test::Builder.

EXAMPLES

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

AUTHORS

Original code by chromatic, maintained by Michael G Schwern Eschwern@pobox.comE

COPYRIGHT

Copyright 2002, 2004 by chromatic Echromatic@wgz.orgE and Michael G Schwern Eschwern@pobox.comE.

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

See F