Julho 26th 2007 04:41 am
Criando Listener para o PHPUnit
o PHPUnit pode ser estendido de várias maneiras, vou apresentar neste post uma forma simples de manipular os resultados dos testes, criando um listener para escutar os resultados obtidos através da execução dos testes unitários.
Você não necessita necessariamente escrever uma subclasse da classe PHPUnit_Framework_TestResult a fim customizar seus resultados de execução. Na maioria das vezes, bastará implementar a interface PHPUnit_Framework_TestListener e uni-lo ao objeto de PHPUnit_Framework_TestResult, antes de executar os testes.
Se você ainda não tem nenhum contato com PHPUnit recomendo a leitura deste post http://blog.diegotremper.com/archives/15.
Primeiramente vamos criar um TestCase de uma classe qualquer, neste exemplo criei um TestCase da Classe ArrayObject disponível no SPL do PHP:
-
<?php
-
// Call ArrayObjectTest::main() if this source file is executed directly.
-
if (!defined('PHPUnit_MAIN_METHOD')) {
-
define('PHPUnit_MAIN_METHOD', 'ArrayObjectTest::main');
-
}
-
-
require_once 'PHPUnit/Framework.php';
-
-
/**
-
* Test class for ArrayObject.
-
* Generated by PHPUnit on 2007-07-26 at 00:47:29.
-
*/
-
class ArrayObjectTest extends PHPUnit_Framework_TestCase {
-
/**
-
* Runs the test methods of this class.
-
*
-
* @access public
-
* @static
-
*/
-
public static function main() {
-
require_once 'PHPUnit/TextUI/TestRunner.php';
-
-
$suite = new PHPUnit_Framework_TestSuite('ArrayObjectTest');
-
$result = PHPUnit_TextUI_TestRunner::run($suite);
-
}
-
-
/**
-
* Sets up the fixture, for example, opens a network connection.
-
* This method is called before a test is executed.
-
*
-
* @access protected
-
*/
-
protected function setUp() {
-
}
-
-
/**
-
* Tears down the fixture, for example, closes a network connection.
-
* This method is called after a test is executed.
-
*
-
* @access protected
-
*/
-
protected function tearDown() {
-
}
-
-
/**
-
* @todo Implement testOffsetExists().
-
*/
-
public function testOffsetExists() {
-
$this->markTestIncomplete('Este teste não foi implementado');
-
}
-
-
/**
-
* @todo Implement testOffsetGet().
-
*/
-
public function testOffsetGet() {
-
$o = new ArrayObject();
-
$o->append('Valor 1');
-
-
$this->assertEquals('Valor 2', $o->offsetGet(0), '\'Valor 2\' não é igual a \'' . $o->offsetGet(0) . '\'');
-
}
-
-
/**
-
* @todo Implement testOffsetSet().
-
*/
-
public function testOffsetSet() {
-
$o = new ArrayObject();
-
$o->offsetSet(0, 10);
-
-
$this->assertEquals(10, $o->offsetGet(0), '\<int> \'10\' não é igual a \'' . $o->offsetGet(0) . '\'');
-
}
-
-
/**
-
* @todo Implement testOffsetUnset().
-
*/
-
public function testOffsetUnset() {
-
$this->markTestSkipped('Teste saltado');
-
}
-
-
/**
-
* @todo Implement testAppend().
-
*/
-
public function testAppend() {
-
throw new Exception('Este teste irá gerar um erro');
-
}
-
}
-
-
// Call ArrayObjectTest::main() if this source file is executed directly.
-
if (PHPUnit_MAIN_METHOD == 'ArrayObjectTest::main') {
-
ArrayObjectTest::main();
-
}
-
?>
salve este código em um arquivo chamado ArrayObjectTest.php
No Comments yet »













