Приглашаем посетить
Черный Саша (cherny-sasha.lit-info.ru)

Section 15-4.  PHPUnit2_Framework_TestCase

Previous
Table of Contents
Next

15-4. PHPUnit2_Framework_TestCase

Your test-case classes will inherit from PHPUnit2_Framework_ TestCase. Most of the time, you will run tests from automatically created test suites. In this case, each of your tests should be represented by a method named test* (by convention).

PHPUnit2_Framework_TestCase implements PHPUnit2_Framework_ Test::countTestCases( ) so that it always returns 1. The implementation of PHPUnit2_Framework_Test::run(PHPUnit2_ Framework_TestResult $result) in this class runs setUp( ), runs the test method, and then runs tearDown( ), reporting any exceptions to the PHPUnit2_Framework_TestResult.

Table 9 shows the external protocols implemented by PHPUnit2_Framework_TestCase.

Table 9. TestCase external protocols

Method

Description

__construct( )

Creates a test case.

__construct(String $name)

Creates a named test case. Names are used to print the test case and often as the name of the test method to be run by reflection.

String getName( )

Returns the name of the test case.

void setName($name( )

Sets the name of the test case.

PHPUnit2_Framework_TestResult run(PHPUnit2_Framework_TestResult $result)

Runs the test case and reports the result in $result.

void runTest( )

Overrides with a testing method if you do not want the testing method to be invoked by reflection.


There are two template methodssetUp( ) and tearDown( ) you can override to create and dispose of the objects against which you are going to test. Table 10 shows these methods.

Table 10. Template methods

Method

Meaning

void setUp( )

Overrides to create objects against which to test. Each test that runs will be run in its own test case, and setUp( ) will be called separately for each one.

void tearDown( )

Overrides to dispose of objects no longer needed once the test has finished. In general, you only need to explicitly dispose of external resources (files or sockets, for example) in tearDown( ).



Previous
Table of Contents
Next