{
    "mode": "perldoc",
    "parameter": "Test::Unit::TestCase",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Test%3A%3AUnit%3A%3ATestCase/json",
    "generated": "2026-05-30T10:45:16Z",
    "synopsis": "package FooBar;\nuse base qw(Test::Unit::TestCase);\nsub new {\nmy $self = shift()->SUPER::new(@);\n# your state for fixture here\nreturn $self;\n}\nsub setup {\n# provide fixture\n}\nsub teardown {\n# clean up after test\n}\nsub testfoo {\nmy $self = shift;\nmy $obj = ClassUnderTest->new(...);\n$self->assertnotnull($obj);\n$self->assertequals('expected result', $obj->foo);\n$self->assert(qr/pattern/, $obj->foobar);\n}\nsub testbar {\n# test the bar feature\n}",
    "sections": {
        "NAME": {
            "content": "Test::Unit::TestCase - unit testing framework base class\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package FooBar;\nuse base qw(Test::Unit::TestCase);\n\nsub new {\nmy $self = shift()->SUPER::new(@);\n# your state for fixture here\nreturn $self;\n}\n\nsub setup {\n# provide fixture\n}\nsub teardown {\n# clean up after test\n}\nsub testfoo {\nmy $self = shift;\nmy $obj = ClassUnderTest->new(...);\n$self->assertnotnull($obj);\n$self->assertequals('expected result', $obj->foo);\n$self->assert(qr/pattern/, $obj->foobar);\n}\nsub testbar {\n# test the bar feature\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Test::Unit::TestCase is the 'workhorse' of the PerlUnit framework. When\nwriting tests, you generally subclass Test::Unit::TestCase, write\n\"setup\" and \"teardown\" functions if you need them, a bunch of \"test*\"\ntest methods, then do\n\n$ TestRunner.pl My::TestCase::Class\n\nand watch as your tests fail/succeed one after another. Or, if you want\nyour tests to work under Test::Harness and the standard perlish 'make\ntest', you'd write a t/foo.t that looked like:\n\nuse Test::Unit::HarnessUnit;\nmy $r = Test::Unit::HarnessUnit->new();\n$r->start('My::TestCase::Class');\n",
            "subsections": [
                {
                    "name": "How To Use Test::Unit::TestCase",
                    "content": "(Taken from the JUnit TestCase class documentation)\n\nA test case defines the \"fixture\" (resources need for testing) to run\nmultiple tests. To define a test case:\n\n1   implement a subclass of TestCase\n\n2   define instance variables that store the state of the fixture (I\nsuppose if you are using Class::MethodMaker this is possible...)\n\n3   initialize the fixture state by overriding \"setup()\"\n\n4   clean-up after a test by overriding \"teardown()\".\n\nImplement your tests as methods. By default, all methods that match the\nregex \"/^test/\" are taken to be test methods (see \"listtests()\" and\n\"getmatchingmethods()\"). Note that, by default all the tests defined\nin the current class and all of its parent classes will be run. To\nchange this behaviour, see \"NOTES\".\n\nBy default, each test runs in its own fixture so there can be no side\neffects among test runs. Here is an example:\n\npackage MathTest;\nuse base qw(Test::Unit::TestCase);\n\nsub new {\nmy $self = shift()->SUPER::new(@);\n$self->{value1} = 0;\n$self->{value2} = 0;\nreturn $self;\n}\n\nsub setup {\nmy $self = shift;\n$self->{value1} = 2;\n$self->{value2} = 3;\n}\n\nFor each test implement a method which interacts with the fixture.\nVerify the expected results with assertions specified by calling\n\"$self->assert()\" with a boolean value.\n\nsub testadd {\nmy $self = shift;\nmy $result = $self->{value1} + $self->{value2};\n$self->assert($result == 5);\n}\n\nOnce the methods are defined you can run them. The normal way to do this\nuses reflection to implement \"runtest\". It dynamically finds and\ninvokes a method. For this the name of the test case has to correspond\nto the test method to be run. The tests to be run can be collected into\na TestSuite. The framework provides different test runners, which can\nrun a test suite and collect the results. A test runner either expects a\nmethod \"suite()\" as the entry point to get a test to run or it will\nextract the suite automatically.\n"
                },
                {
                    "name": "Writing Test Methods",
                    "content": "The return value of your test method is completely irrelevant. The\nvarious test runners assume that a test is executed successfully if no\nexceptions are thrown. Generally, you will not have to deal directly\nwith exceptions, but will write tests that look something like:\n\nsub testsomething {\nmy $self = shift;\n# Execute some code which gives some results.\n...\n# Make assertions about those results\n$self->assertequals('expected value', $resultA);\n$self->assertnotnull($resultobject);\n$self->assert(qr/somepattern/, $resultB);\n}\n\nThe assert methods throw appropriate exceptions when the assertions\nfail, which will generally stringify nicely to give you sensible error\nreports.\n\nTest::Unit::Assert has more details on the various different \"assert\"\nmethods.\n\nTest::Unit::Exception describes the Exceptions used within the\n\"Test::Unit::*\" framework.\n"
                },
                {
                    "name": "Helper methods",
                    "content": "maketestfromcoderef (CODEREF, [NAME])\nTakes a coderef and an optional name and returns a Test case that\ninherits from the object on which it was called, which has the\ncoderef installed as its \"runtest\" method. Class::Inner has more\ndetails on how this is generated.\n\nlisttests\nReturns the list of test methods in this class and its parents. You\ncan override this in your own classes, but remember to call\n\"SUPER::listtests\" in there too. Uses \"getmatchingmethods\".\n\ngetmatchingmethods (REGEXP)\nReturns the list of methods in this class matching REGEXP.\n\nsetup\nteardown\nIf you don't have any setup or tear down code that needs to be run,\nwe provide a couple of null methods. Override them if you need to.\n\nannotate (MESSAGE)\nYou can accumulate helpful debugging for each testcase method via\nthis method, and it will only be outputted if the test fails or\nencounters an error.\n"
                },
                {
                    "name": "How it All Works",
                    "content": "The PerlUnit framework is achingly complex. The basic idea is that you\nget to write your tests independently of the manner in which they will\nbe run, either via a \"make test\" type script, or through one of the\nprovided TestRunners, the framework will handle all that for you. And it\ndoes. So for the purposes of someone writing tests, in the majority of\ncases the answer is 'It just does.'.\n\nOf course, if you're trying to extend the framework, life gets a little\nmore tricky. The core class that you should try and grok is probably\nTest::Unit::Result, which, in tandem with whichever TestRunner is being\nused mediates the process of running tests, stashes the results and\ngenerally sits at the centre of everything.\n\nBetter docs will be forthcoming.\n"
                }
            ]
        },
        "NOTES": {
            "content": "Here's a few things to remember when you're writing your test suite:\n\nTests are run in 'random' order; the list of tests in your TestCase are\ngenerated automagically from its symbol table, which is a hash, so\nmethods aren't sorted there.\n\nIf you need to specify the test order, you can do one of the following:\n\n*   Set @TESTS\n\nour @TESTS = qw(mytest mytest2);\n\nThis is the simplest, and recommended way.\n\n*   Override the \"listtests()\" method\n\nto return an ordered list of methodnames\n\n*   Provide a \"suite()\" method\n\nwhich returns a Test::Unit::TestSuite.\n\nHowever, even if you do manage to specify the test order, be careful,\nobject data will not be retained from one test to another, if you want\nto use persistent data you'll have to use package lexicals or globals.\n(Yes, this is probably a bug).\n\nIf you only need to restrict which tests are run, there is a filtering\nmechanism available. Override the \"filter()\" method in your testcase\nclass to return a hashref whose keys are filter tokens and whose values\nare either arrayrefs of test method names or coderefs which take the\nmethod name as the sole parameter and return true if and only if it\nshould be filtered, e.g.\n\nsub filter {{\nslow => [ qw(myslowtest myreallyslowtest) ],\nmatchingfoo => sub {\nmy $method = shift;\nreturn $method =~ /foo/;\n}\n}}\n\nThen, set the filter state in your runner before the test run starts:\n\n# @filtertokens = ( 'slow', ... );\n$runner->filter(@filtertokens);\n$runner->start(@args);\n\nThis interface is public, but currently undocumented (see doc/TODO).\n",
            "subsections": []
        },
        "BUGS": {
            "content": "See note 1 for at least one bug that's got me scratching my head.\nThere's bound to be others.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright (c) 2000-2002, 2005 the PerlUnit Development Team (see\nTest::Unit or the AUTHORS file included in this distribution).\n\nAll rights reserved. This program is free software; you can redistribute\nit and/or modify it under the same terms as Perl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "*   Test::Unit::Assert\n\n*   Test::Unit::Exception\n\n*   Test::Unit::TestSuite\n\n*   Test::Unit::TestRunner\n\n*   Test::Unit::TkTestRunner\n\n*   For further examples, take a look at the framework self test\ncollection (t::tlib::AllTests).\n",
            "subsections": []
        }
    },
    "summary": "Test::Unit::TestCase - unit testing framework base class",
    "flags": [],
    "examples": [],
    "see_also": []
}