Ïðèãëàøàåì ïîñåòèòü
ßçûêîâ (yazykov.lit-info.ru)

Section 16-6.  Implement PHPUnit2_Framework_TestListener

Previous
Table of Contents
Next

16-6. Implement PHPUnit2_Framework_TestListener

You do not necessarily need to write a whole subclass of PHPUnit2_Framework_TestResult to customize it. Most of the time, it will suffice to implement a new PHPUnit2_Framework_ TestListener (see Table 15) and attach it to the PHPUnit2_ Framework_TestResult object, before running the tests.

Example 26 shows a simple implementation of the PHPUnit2_ Framework_TestListener interface.

Example 26. A simple test listener
<?php
require_once 'PHPUnit2/Framework/TestListener.php';

class SimpleTestListener
implements PHPUnit2_Framework_TestListener { 
 public function 
 addError(PHPUnit2_Framework_Test $test, Exception $e) {
	printf(
	 "Error while running test '%s'.\n",
	 $test->getName( )
  );
 }

 public function 
 addFailure(PHPUnit2_Framework_Test $test, 
			 PHPUnit2_Framework_AssertionFailedError $e) {
  printf(
	 "Test '%s' failed.\n",
	 $test->getName( )

  );
 }

 public function
 addIncompleteTest(PHPUnit2_Framework_Test $test,
				Exception $e) {

	printf(
	 "Test '%s' is incomplete.\n",
	 $test->getName( )
  );
 }

 public function startTest(PHPUnit2_Framework_Test $test) {
  printf(
	 "Test '%s' started.\n",
	 $test->getName( )

  );
 }

 public function endTest(PHPUnit2_Framework_Test $test) {
  printf(
	 "Test '%s' ended.\n",
	 $test->getName( )
  );
 }

 public function 
 startTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  printf(
	 "TestSuite '%s' started.\n",
	 $suite->getName( )
  );
 }

 public function 
 endTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  printf(
	 "TestSuite '%s' ended.\n",
	 $suite->getName( )
  );
 }
}
?>

Example 27 shows how to run and observe a test suite.

Example 27. Running and observing a test suite
<?php
require_once 'PHPUnit2/Framework/TestResult.php';
require_once 'PHPUnit2/Framework/TestSuite.php';

require_once 'ArrayTest.php';
require_once 'SimpleTestListener.php';

// Create a test suite that contains the tests
// from the ArrayTest class.
$suite = new PHPUnit2_Framework_TestSuite('ArrayTest');
// Create a test result and attach a SimpleTestListener
// object as an observer to it.
$result = new PHPUnit2_Framework_TestResult;
$result->addListener(new SimpleTestListener);

// Run the tests.
$suite->run($result);
?>

TestSuite 'ArrayTest' started.
Test 'testNewArrayIsEmpty' started.
Test 'testNewArrayIsEmpty' ended.
Test 'testArrayContainsAnElement' started.
Test 'testArrayContainsAnElement' ended.
TestSuite 'ArrayTest' ended.


Previous
Table of Contents
Next