{
    "mode": "perldoc",
    "parameter": "Template::Test",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Template%3A%3ATest/json",
    "generated": "2026-07-06T21:17:31Z",
    "synopsis": "use Template::Test;\n$Template::Test::DEBUG = 0;   # set this true to see each test running\n$Template::Test::EXTRA = 2;   # 2 extra tests follow testexpect()...\n# ok() can be called any number of times before testexpect\nok( $trueorfalse )\n# testexpect() splits $input into individual tests, processes each\n# and compares generated output against expected output\ntestexpect($input, $template, \\%replace );\n# $input is text or filehandle (e.g. DATA section after END)\ntestexpect( $text );\ntestexpect( \\*DATA );\n# $template is a Template object or configuration hash\nmy $templatecfg = { ... };\ntestexpect( $input, $templatecfg );\nmy $templateobj = Template->new($templatecfg);\ntestexpect( $input, $templateobj );\n# $replace is a hash reference of template variables\nmy $replace = {\na => 'alpha',\nb => 'bravo'\n};\ntestexpect( $input, $template, $replace );\n# ok() called after testexpect should be declared in $EXTRA (2)\nok( $trueorfalse )\nok( $trueorfalse )",
    "sections": {
        "NAME": {
            "content": "Template::Test - Module for automating TT2 test scripts\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Template::Test;\n\n$Template::Test::DEBUG = 0;   # set this true to see each test running\n$Template::Test::EXTRA = 2;   # 2 extra tests follow testexpect()...\n\n# ok() can be called any number of times before testexpect\nok( $trueorfalse )\n\n# testexpect() splits $input into individual tests, processes each\n# and compares generated output against expected output\ntestexpect($input, $template, \\%replace );\n\n# $input is text or filehandle (e.g. DATA section after END)\ntestexpect( $text );\ntestexpect( \\*DATA );\n\n# $template is a Template object or configuration hash\nmy $templatecfg = { ... };\ntestexpect( $input, $templatecfg );\nmy $templateobj = Template->new($templatecfg);\ntestexpect( $input, $templateobj );\n\n# $replace is a hash reference of template variables\nmy $replace = {\na => 'alpha',\nb => 'bravo'\n};\ntestexpect( $input, $template, $replace );\n\n# ok() called after testexpect should be declared in $EXTRA (2)\nok( $trueorfalse )\nok( $trueorfalse )\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"Template::Test\" module defines the testexpect() and other related subroutines which can be\nused to automate test scripts for the Template Toolkit. See the numerous tests in the t\nsub-directory of the distribution for examples of use.\n",
            "subsections": []
        },
        "PACKAGE SUBROUTINES": {
            "content": "textexpect()\nThe \"testexpect()\" subroutine splits an input document into a number of separate tests,\nprocesses each one using the Template Toolkit and then compares the generated output against an\nexpected output, also specified in the input document. It generates the familiar \"ok\"/\"not ok\"\noutput compatible with \"Test::Harness\".\n\nThe test input should be specified as a text string or a reference to a filehandle (e.g. \"GLOB\"\nor \"IO::Handle\") from which it can be read. In particular, this allows the test input to be\nplaced after the \"END\" marker and read via the \"DATA\" filehandle.\n\nuse Template::Test;\n\ntestexpect(\\*DATA);\n\nEND\n# this is the first test (this is a comment)\n-- test --\nblah blah blah [% foo %]\n-- expect --\nblah blah blah valueoffoo\n\n# here's the second test (no surprise, so is this)\n-- test --\nmore blah blah [% bar %]\n-- expect --\nmore blah blah valueofbar\n\nBlank lines between test sections are generally ignored. Any line starting with \"#\" is treated\nas a comment and is ignored.\n\nThe second and third parameters to \"testexpect()\" are optional. The second may be either a\nreference to a Template object which should be used to process the template fragments, or a\nreference to a hash array containing configuration values which should be used to instantiate a\nnew Template object.\n\n# pass reference to config hash\nmy $config = {\nINCLUDEPATH => '/here/there:/every/where',\nPOSTCHOMP   => 1,\n};\ntestexpect(\\*DATA, $config);\n\n# or create Template object explicitly\nmy $template = Template->new($config);\ntestexpect(\\*DATA, $template);\n\nThe third parameter may be used to reference a hash array of template variable which should be\ndefined when processing the tests. This is passed to the Template process() method.\n\nmy $replace = {\na => 'alpha',\nb => 'bravo',\n};\n\ntestexpect(\\*DATA, $config, $replace);\n\nThe second parameter may be left undefined to specify a default Template configuration.\n\ntestexpect(\\*DATA, undef, $replace);\n\nFor testing the output of different Template configurations, a reference to a list of named\nTemplate objects also may be passed as the second parameter.\n\nmy $tt1 = Template->new({ ... });\nmy $tt2 = Template->new({ ... });\nmy @tts = [ one => $tt1, two => $tt1 ];\n\nThe first object in the list is used by default. Other objects may be switched in with a '\"--\nuse $name --\"' marker. This should immediately follow a '\"-- test --\"' line. That object will\nthen be used for the rest of the test, or until a different object is selected.\n\n-- test --\n-- use one --\n[% blah %]\n-- expect --\nblah, blah\n\n-- test --\nstill using one...\n-- expect --\n...\n\n-- test --\n-- use two --\n[% blah %]\n-- expect --\nblah, blah, more blah\n\nThe \"testexpect()\" sub counts the number of tests, and then calls ntests() to generate the\nfamiliar \"\"1..$ntests\\n\"\" test harness line. Each test defined generates two test numbers. The\nfirst indicates that the input was processed without error, and the second that the output\nmatches that expected.\n\nAdditional test may be run before \"testexpect()\" by calling ok(). These test results are cached\nuntil ntests() is called and the final number of tests can be calculated. Then, the\n\"\"1..$ntests\"\" line is output, along with \"\"ok $n\"\" / \"\"not ok $n\"\" lines for each of the cached\ntest result. Subsequent calls to ok() then generate an output line immediately.\n\nmy $something = SomeObject->new();\nok( $something );\n\nmy $other = AnotherThing->new();\nok( $other );\n\ntestexpect(\\*DATA);\n\nIf any tests are to follow after \"testexpect()\" is called then these should be pre-declared by\nsetting the $EXTRA package variable. This value (default: 0) is added to the grand total\ncalculated by ntests(). The results of the additional tests are also registered by calling ok().\n\n$Template::Test::EXTRA = 2;\n\n# can call ok() any number of times before testexpect()\nok( $didthatwork );\nok( $makesure );\nok( $deadcertain );\n\n# <some> number of tests...\ntestexpect(\\*DATA, $config, $replace);\n\n# here's those $EXTRA tests\nok( defined $someresult && ref $someresult eq 'ARRAY' );\nok( $someresult->[0] eq 'some expected value' );\n\nIf you don't want to call \"testexpect()\" at all then you can call \"ntests($n)\" to declare the\nnumber of tests and generate the test header line. After that, simply call ok() for each test\npassing a true or false values to indicate that the test passed or failed.\n\nntests(2);\nok(1);\nok(0);\n\nIf you're really lazy, you can just call ok() and not bother declaring the number of tests at\nall. All tests results will be cached until the end of the script and then printed in one go\nbefore the program exits.\n\nok( $x );\nok( $y );\n\nYou can identify only a specific part of the input file for testing using the '\"-- start --\"'\nand '\"-- stop --\"' markers. Anything before the first '\"-- start --\"' is ignored, along with\nanything after the next '\"-- stop --\"' marker.\n\n-- test --\nthis is test 1 (not performed)\n-- expect --\nthis is test 1 (not performed)\n\n-- start --\n\n-- test --\nthis is test 2\n-- expect --\nthis is test 2\n\n-- stop --\n\n...\n\nntests()\nSubroutine used to specify how many tests you're expecting to run.\n\nok($test)\nGenerates an \"\"ok $n\"\" or \"\"not ok $n\"\" message if $test is true or false.\n\nnotok($test)\nThe logical inverse of ok(). Prints an \"\"ok $n\"\" message is $test is *false* and vice-versa.\n\ncallsign()\nFor historical reasons and general utility, the module also defines a \"callsign()\" subroutine\nwhich returns a hash mapping the letters \"a\" to \"z\" to their phonetic alphabet equivalent (e.g.\nradio callsigns). This is used by many of the test scripts as a known source of variable values.\n\ntestexpect(\\*DATA, $config, callsign());\n\nbanner()\nThis subroutine prints a simple banner including any text passed as parameters. The $DEBUG\nvariable must be set for it to generate any output.\n\nbanner('Testing something-or-other');\n\nexample output:\n\n#------------------------------------------------------------\n# Testing something-or-other (27 tests completed)\n#------------------------------------------------------------\n",
            "subsections": []
        },
        "PACKAGE VARIABLES": {
            "content": "$DEBUG\nThe $DEBUG package variable can be set to enable debugging mode.\n\n$PRESERVE\nThe $PRESERVE package variable can be set to stop the testexpect() from converting newlines in\nthe output and expected output into the literal strings '\\n'.\n",
            "subsections": []
        },
        "HISTORY": {
            "content": "This module started its butt-ugly life as the \"t/texpect.pl\" script. It was cleaned up to became\nthe \"Template::Test\" module some time around version 0.29. It underwent further cosmetic surgery\nfor version 2.00 but still retains some remarkable rear-end resemblances.\n\nSince then the \"Test::More\" and related modules have appeared on CPAN making this module mostly,\nbut not entirely, redundant.\n\nBUGS / KNOWN \"FEATURES\"\nImports all methods by default. This is generally a Bad Thing, but this module is only used in\ntest scripts (i.e. at build time) so a) we don't really care and b) it saves typing.\n\nThe line splitter may be a bit dumb, especially if it sees lines like \"-- this --\" that aren't\nsupposed to be special markers. So don't do that.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Andy Wardley <abw@wardley.org> <http://wardley.org/>\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.\n\nThis module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Template\n",
            "subsections": []
        }
    },
    "summary": "Template::Test - Module for automating TT2 test scripts",
    "flags": [],
    "examples": [],
    "see_also": []
}