The Template::Test module defines the test_expect() and other related subroutines which can be used to automate test scripts for the Template Toolkit. See the numerous tests in the t sub-directory of the distribution for examples of use.

The test_expect() subroutine splits an input document into a number of separate tests, processes each one using the Template Toolkit and then compares the generated output against an expected output, also specified in the input document. It generates the familiar ok/not ok output compatible with Test::Harness.

The test input should be specified as a text string or a reference to a filehandle (e.g. GLOB or IO::Handle) from which it can be read. In particular, this allows the test input to be placed after the __END__ marker and read via the DATA filehandle.

Blank lines between test sections are generally ignored. Any line starting with # is treated as a comment and is ignored.

The second and third parameters to test_expect() are optional. The second may be either a reference to a Template object which should be used to process the template fragments, or a reference to a hash array containing configuration values which should be used to instantiate a new Template object.

The first object in the list is used by default. Other objects may be switched in with a '-- use $name --' marker. This should immediately follow a '-- test --' line. That object will then be used for the rest of the test, or until a different object is selected.

The test_expect() sub counts the number of tests, and then calls ntests() to generate the familiar "1..$ntests\n" test harness line. Each test defined generates two test numbers. The first indicates that the input was processed without error, and the second that the output matches that expected.

Additional test may be run before test_expect() by calling ok(). These test results are cached until ntests() is called and the final number of tests can be calculated. Then, the "1..$ntests" line is output, along with "ok $n" / "not ok $n" lines for each of the cached test result. Subsequent calls to ok() then generate an output line immediately.

If any tests are to follow after test_expect() is called then these should be pre-declared by setting the $EXTRA package variable. This value (default: 0) is added to the grand total calculated by ntests(). The results of the additional tests are also registered by calling ok().

If you don't want to call test_expect() at all then you can call ntests($n) to declare the number of tests and generate the test header line. After that, simply call ok() for each test passing a true or false values to indicate that the test passed or failed.

ntests(2);
ok(1);
ok(0);

If you're really lazy, you can just call ok() and not bother declaring the number of tests at all. All tests results will be cached until the end of the script and then printed in one go before the program exits.

ok( $x );
ok( $y );

You can identify only a specific part of the input file for testing using the '-- start --' and '-- stop --' markers. Anything before the first '-- start --' is ignored, along with anything after the next '-- stop --' marker.

For historical reasons and general utility, the module also defines a callsign() subroutine which returns a hash mapping the letters a to z to their phonetic alphabet equivalent (e.g. radio callsigns). This is used by many of the test scripts as a known source of variable values.

This module started its butt-ugly life as the t/texpect.pl script. It was cleaned up to became the Template::Test module some time around version 0.29. It underwent further cosmetic surgery for version 2.00 but still retains some remarkable rear-end resemblances.

Since then the Test::More and related modules have appeared on CPAN making this module mostly, but not entirely, redundant.