The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Builder - *DISCOURAGED* Module for building testing libraries.

DESCRIPTION

This module was previously the base module for almost any testing library. This module is now little more than a compatability wrapper around Test::Stream. If you are looking to write or update a testing library you should look at Test::Stream::Toolset.

However if you must support older versions of Test-Simple/More/Builder then you MUST use this module for your testing code.

PACKAGE VARS

$Test::Builder::Test

The variable that holds the Test::Builder singleton. It is best practice to leave this variable alone, messing with it can have unexpected consequences.

$Test::Builder::Level

In the past this variable was used to track stack depth so that Test::Builder could report the correct line number. If you use Test::Builder this will still work, but in new code it is better to use the Test::Stream::Context module unless you must support older versions of Test-Simple.

METHODS

CONSTRUCTORS

Test::Builder->new

Returns the singleton stored in $Test::Builder::Test.

Test::Builder->create
Test::Builder->create(use_shared => 1)

Returns a new instance of Test::Builder. It is important to note that this instance will not use the shared Test::Stream::Hub object unless you pass in the use_shared => 1 argument.

This is a way to get a new instance of Test::Builder that is not the singleton. This is usually done for testing code.

UTIL

$ctx = $TB->ctx

Helper method for Test::Builder to get a Test::Stream::Context object. This is primarily for internal use, and is NOT present on older versions of Test::Builder.

$depth = $TB->depth

Get the subtest depth. If this is called inside a subtest the value will be 1.

$todo = $TB->find_TODO($package, $set, $new_value)

This is a way to find and/or set the TODO reason. This method has complex and unintuitive logic, it is kept for legacy reasons, but it is recommended that you not use it.

Calling with no arguments it will try to find the $TODO variable for you and return the value.

Calling with a package will try to find the $TODO value of that package.

If you include $set and $new_value it will set the $TODO variable for the specified package.

$bool = $TB->in_todo

This will return true if TODO is set, false otherwise.

$TB->todo()
$TB->todo($package)

This finds the todo message currently set, if any. If you specify a package it will look there unless it finds a message set in the singleton.

OTHER

$pkg = $TB->caller
($pkg, $file, $line) $TB->caller

This will try to find the details about where the test was called.

$TB->carp($msg)

Warn from the perspective of the test caller.

$TB->croak

Throw an exception from the perspective of the test caller.

$TB->child($name)

DISCOURAGED

This used to be used internally to start a subtest. Subtests started in this way must call finalize() when they are done.

Use of this method never gained traction, and was never strictly necessary. It has always been better to use the subtest() method instead which handles the hard work for you. This is too low level for most use cases.

$TB->finalize

DISCOURAGED

Use this to end a subtest created via child().

$TB->explain(@stuff)

Interface to Data::Dumper that dumps whatever you give it. This is really just a quick way to dump human readable test of structures.

$TB->exported_to
$TB->exported_to($package)

DISCOURAGED

Test::Builder used to assume that tests would only ever be run from a single package, usually main. This is a way to tell Test::Builder what package is running tests. This assumption proved to be very dumb.

It is not uncommon to have tests run from many packages, as a result this method is pretty useless if not actively harmful. Almost nothing uses this, but it has been preserved for legacy modules.

$bool = $TB->is_fh($thing)

Check if something is a filehandile.

$num = $TB->level
$TB->level($num)

DISCOURAGED

Get/Set $Test::Builder::Level. $Level is a package var, and most things localize it, so this method is pretty useless.

$bool = $TB->maybe_regex($thing)

Check if something might be a regex. These days we have qr// and other things that may make this method seem silly, however in older versions of perl we did not have such luxuries. This method exists for old code and environments where strings may be used as regexes.

$TB->reset

Reset the builder object to a very basic and default state. You almost certainly do not need this unless you are writing a tool to test testing libraries. Even then you probably do not want this.

In newer code using Test::Stream this completely resets the state in the shared hub as well.

$TB->todo_start($msg)

Set a todo message. This will make all results 'TODO' if they are generated after this is set.

$TB->todo_end

Unset the TODO message.

HUB INTERFACE

In older versions of Test::Builder these methods directly effected the singleton. These days they are all compatability wrappers around Test::Stream. If possible you should use Test::Stream, however if you need to support older versions of Test::Builder these will work fine for both.

$fh = $TB->failure_output
$fh = $TB->output
$fh = $TB->todo_output
$TB->failure_output($fh)
$TB->output($fh)
$TB->todo_output($fh)

These allow you to get and/or set the filehandle for various types of output. Note that this will not effect UTF8 or other encodings that are specified using the Test::Stream interface. This only effects the 'legacy' encoding used by Test::Stream by default.

$TB->reset_outputs

This will reset all the outputs to the default. Note this only effects the 'legacy' encoding used by Test::Stream.

$TB->no_diag

Do not display Test::Stream::Event::Diag events.

$TB->no_ending($bool)

Do not do some special magic at the end that tells you what went wrong with tests.

$TB->no_header($bool)

Do not display the plan.

$TB->use_numbers($bool)

Turn numbers in TAP on and off.

$num = $TB->current_test

HISTORY

$TB->details

Get all the events that occured on this object. Each event will be transformed into a hash that matches the legacy output of this method.

$TB->expected_tests

Set/Get expected number of tests

$TB->has_plan

Check if there is a plan

$TB->summary

List of pass/fail results.

$TB->current_test($num)

Get/Set the current test number. Setting the test number is probably not something you want to do, except when validating testing tools.

$bool = $TB->is_passing

This is a way to check if the test suite is currently passing or failing.

EVENT GENERATORS

See Test::Stream::Context, Test::Stream::Toolset, and Test::More::Tools. Calling the methods below is not advised.

$TB->BAILOUT($reason)
$TB->BAIL_OUT($reason)

These will issue an Test::Stream::Event::Bail event. This will cause the test file to stop running immedietly with a message. In TAP this event is ALSO a signal to the harness to abort any remaining testing.

$TB->cmp_ok($got, $type, $expect, $name)
    $TB->cmp_ok('foo', 'eq', 'foo', 'check that foo is foo');

Check that a comparison of $type is true for the given values.

$TB->diag($msg)
    $TB->diag("This is a diagnostic message");

This will print a message, typically to STDERR. This message will get a '# ' prefix so that TAP harnesses see it as a comment.

$TB->done_testing
$TB->done_testing($num)

This will issue an Test::Stream::Event::Plan event. This plan will set the expected number of tests to the current test count. This will also tell Test::Stream that the test should be done.

If you provide an argument, that argument will be used as the expected number of tests.

$TB->is_eq($got, $expect, $name)

This will issue an Test::Stream::Event::Ok event. If $got matches $expect the test will pass, otherwise it will fail. This method expects values to be strings.

$TB->is_num($got, $expect, $name)

This will issue an Test::Stream::Event::Ok event. If $got matches $expect the test will pass, otherwise it will fail. This method expects values to be numerical.

$TB->isnt_eq($got, $dont_expect, $name)

This will issue an Test::Stream::Event::Ok event. If $got matches $dont_expect the test will fail, otherwise it will pass. This method expects values to be strings.

$TB->isnt_num($got, $dont_expect, $name)

This will issue an Test::Stream::Event::Ok event. If $got matches $dont_expect the test will fail, otherwise it will pass. This method expects values to be numerical.

$TB->like($thing, $regex, $name)

This will check $thing against the $regex. If it matches the test will pass.

$TB->unlike($thing, $regex, $name)

This will check $thing against the $regex. If it matches the test will fail.

$TB->no_plan

This tells Test::Builder that there should be no plan, and that is the plan.

$TB->note($message)

Send a message to STDOUT, it will be prefixed with '# ' so that TAP harnesses will see it as a comment.

$TB->ok($bool, $name)

Issues an Test::Stream::Event::Ok event. If $bool is true the test passes, otherwise it fails.

$TB->plan(tests => $num)

Set the number of tests that should be run.

$TB->plan(skip_all => $reason)

Skip all the tests for the specified reason. $reason is a string.

$TB->plan('no_plan')

The plan is that there is no plan.

$TB->skip($reason)

Skip a single test for the specified reason. This generates a single Test::Stream::Event::Ok event.

$TB->skip_all($reason)

Skip all the tests for the specified reason.

$TB->subtest($name, sub { ... })

Run the provided codeblock as a subtest. All results will be indented, and all that matters is the final OK.

$TB->todo_skip($reason)

Skip a single test with a todo message. This generates a single Test::Stream::Event::Ok event, it will have both it's 'todo' and its 'skip' set to $reason.

ACCESSORS

$hub = $TB->hub

Get the hub used by this builder (or the shared hub). This is not available on older test builders.

$TB->name

Name of the test builder instance, this is only useful inside a subtest.

$TB->parent

Parent builder instance, if this is a child.

MONKEYPATCHING

Many legacy testing modules monkeypatch ok(), plan(), and others. The abillity to monkeypatch these to effect all events of the specified type is now considered discouraged. For backwords compatability monkeypatching continues to work, however in the distant future it will be removed. Test::Stream upon which Test::Builder is now built, provides hooks and API's for doing everything that previously required monkeypatching.

TUTORIALS

Test::Tutorial

The original Test::Tutorial. Uses comedy to introduce you to testing from scratch.

Test::Tutorial::WritingTests

The Test::Tutorial::WritingTests tutorial takes a more technical approach. The idea behind this tutorial is to give you a technical introduction to testing that can easily be used as a reference. This is for people who say "Just tell me how to do it, and quickly!".

Test::Tutorial::WritingTools

The Test::Tutorial::WritingTools tutorial is an introduction to writing testing tools that play nicely with other Test::Stream and Test::Builder based tools. This is what you should look at if you want to write Test::MyWidget.

SOURCE

The source code repository for Test::More can be found at http://github.com/Test-More/test-more/.

MAINTAINER

Chad Granum <exodist@cpan.org>

AUTHORS

The following people have all contributed to the Test-More dist (sorted using VIM's sort function).

Chad Granum <exodist@cpan.org>
Fergal Daly <fergal@esatclear.ie>>
Mark Fowler <mark@twoshortplanks.com>
Michael G Schwern <schwern@pobox.com>
唐鳳

COPYRIGHT

There has been a lot of code migration between modules, here are all the original copyrights together:

Test::Stream
Test::Stream::Tester

Copyright 2015 Chad Granum <exodist7@gmail.com>.

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

Test::Simple
Test::More
Test::Builder

Originally authored by Michael G Schwern <schwern@pobox.com> with much inspiration from Joshua Pritikin's Test module and lots of help from Barrie Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa gang.

Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern <schwern@pobox.com>, wardrobe by Calvin Klein.

Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.

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

Test::use::ok

To the extent possible under law, 唐鳳 has waived all copyright and related or neighboring rights to Test-use-ok.

This work is published from Taiwan.

http://creativecommons.org/publicdomain/zero/1.0

Test::Tester

This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts are based on other people's work.

Under the same license as Perl itself

See http://www.perl.com/perl/misc/Artistic.html

Test::Builder::Tester

Copyright Mark Fowler <mark@twoshortplanks.com> 2002, 2004.

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