use Test::Builder::Tester tests => 1; use Test::More; test_out("not ok 1 - foo"); test_fail(+1); fail("foo"); test_test("fail works");
The testing system is designed to be used by performing a three step process for each test you wish to test. This process starts with using "test_out" and "test_err" in advance to declare what the testsuite you are testing will output with Test::Builder to stdout and stderr.
You then can run the test(s) from your test suite that call Test::Builder. At this point the output of Test::Builder is safely captured by Test::Builder::Tester rather than being interpreted as real test output.
The final stage is to call "test_test" that will simply compare what you predeclared to what Test::Builder actually outputted, and report the results back with a ``ok'' or ``not ok'' (with debugging) to the normal output.
test_out("ok 1","ok 2");
is the same as
test_out("ok 1\nok 2");
which is even the same as
test_out("ok 1"); test_out("ok 2");
Once "test_out" or "test_err" (or "test_fail" or "test_diag") have been called once all further output from Test::Builder will be captured by Test::Builder::Tester. This means that your will not be able perform further tests to the normal output in the normal way until you call "test_test" (well, unless you manually meddle with the output filehandles)
test_err("# Failed test ($0 at line ".line_num(+1).")");
"test_fail" exists as a convenience function that can be called instead. It takes one argument, the offset from the current line that the line that causes the fail is on.
This means that the example in the synopsis could be rewritten more simply as:
test_out("not ok 1 - foo"); test_fail(+1); fail("foo"); test_test("fail works");
The "test_diag" function prepends comment hashes and spacing to the start and newlines to the end of the expected output passed to it and adds it to the list of expected error output. So, instead of writing
test_err("# Couldn't open file");
you can write
test_diag("Couldn't open file");
Remember that Test::Builder's diag function will not add newlines to the end of output and test_diag will. So to check
You would do
without the newlines.
This takes name/value pairs that effect how the test is run.
As a convience, if only one argument is passed then this argument is assumed to be the name of the test (as in the above examples.)
Once "test_test" has been run test output will be redirected back to the original filehandles that Test::Builder was connected to (probably STDOUT and STDERR,) meaning any further tests you run will function normally and cause success/errors for Test::Harness.
Essentially this is the same as the "__LINE__" macro, but the "line_num(+3)" idiom is arguably nicer.
In addition to the six exported functions there there exists one function that can only be accessed with a fully qualified function call.
To assist you, if you have the Term::ANSIColor module installed (which you should do by default from perl 5.005 onwards), "test_test" can colour the background of the debug information to disambiguate the different types of output. The debug output will have it's background coloured green and red. The green part represents the text which is the same between the executed and actual output, the red shows which part differs.
The "color" function determines if colouring should occur or not. Passing it a true or false value will enable or disable colouring respectively, and the function called with no argument will return the current setting.
To enable colouring from the command line, you can use the Text::Builder::Tester::Color module like so:
perl -Mlib=Text::Builder::Tester::Color test.t
Or by including the Test::Builder::Tester::Color module directly in the PERL5LIB.
The color function doesn't work unless Term::ANSIColor is installed and is compatible with your terminal.
Bugs (and requests for new features) can be reported to the author though the CPAN RT system: <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Builder-Tester>
Some code taken from Test::More and Test::Catch, written by by Michael G Schwern <[email protected]>. Hence, those parts Copyright Micheal G Schwern 2001. Used and distributed with permission.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Thanks to Richard Clamp <[email protected]> for letting me use his testing system to try this module out on.