3 // +------------------------------------------------------------------------+
5 // +------------------------------------------------------------------------+
6 // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
7 // +------------------------------------------------------------------------+
8 // | This source file is subject to version 3.00 of the PHP License, |
9 // | that is available at http://www.php.net/license/3_0.txt. |
10 // | If you did not receive a copy of the PHP license and are unable to |
11 // | obtain it through the world-wide-web, please send a note to |
12 // | license@php.net so we can mail you a copy immediately. |
13 // +------------------------------------------------------------------------+
15 // $Id: TestCase.php,v 1.15 2004/10/01 06:11:39 sebastian Exp $
18 require_once 'PHPUnit/Assert.php';
19 require_once 'PHPUnit/TestResult.php';
22 * A TestCase defines the fixture to run multiple tests.
24 * To define a TestCase
26 * 1) Implement a subclass of PHPUnit_TestCase.
27 * 2) Define instance variables that store the state of the fixture.
28 * 3) Initialize the fixture state by overriding setUp().
29 * 4) Clean-up after a test by overriding tearDown().
31 * Each test runs in its own fixture so there can be no side effects
38 * class MathTest extends PHPUnit_TestCase {
42 * function MathTest($name) {
43 * $this->PHPUnit_TestCase($name);
54 * For each test implement a method which interacts with the fixture.
55 * Verify the expected results with assertions specified by calling
56 * assert with a boolean.
60 * function testPass() {
61 * $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
66 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
67 * @copyright Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
68 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
72 class PHPUnit_TestCase
extends PHPUnit_Assert
{
80 * The name of the test case.
88 * PHPUnit_TestResult object
96 * Constructs a test case with the given name.
101 function PHPUnit_TestCase($name = FALSE) {
102 if ($name !== FALSE) {
103 $this->setName($name);
108 * Counts the number of test cases executed by run(TestResult result).
113 function countTestCases() {
118 * Gets the name of a TestCase.
128 * Runs the test case and collects the results in a given TestResult object.
134 function run(&$result) {
135 $this->_result
= &$result;
136 $this->_result
->run($this);
138 return $this->_result
;
142 * Runs the bare test sequence.
154 * Override to run the test and assert its state.
168 * Sets the name of a TestCase.
173 function setName($name) {
174 $this->_name
= $name;
178 * Returns a string representation of the test case.
183 function toString() {
188 * Creates a default TestResult object.
193 function &createResult() {
194 return new PHPUnit_TestResult
;
198 * Fails a test with the given message.
203 function fail($message = '') {
204 $this->_result
->addFailure($this, $message);
205 $this->_failed
= TRUE;
214 if (!$this->_failed
) {
215 $this->_result
->addPassedTest($this);
220 * Sets up the fixture, for example, open a network connection.
221 * This method is called before a test is executed.
226 function setUp() { /* abstract */ }
229 * Tears down the fixture, for example, close a network connection.
230 * This method is called after a test is executed.
235 function tearDown() { /* abstract */ }