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:
  1. <?php
  2. // Call ArrayObjectTest::main() if this source file is executed directly.
  3. if (!defined('PHPUnit_MAIN_METHOD')) {
  4.     define('PHPUnit_MAIN_METHOD', 'ArrayObjectTest::main');
  5. }
  6.  
  7. require_once 'PHPUnit/Framework.php';
  8.  
  9. /**
  10.  * Test class for ArrayObject.
  11.  * Generated by PHPUnit on 2007-07-26 at 00:47:29.
  12.  */
  13. class ArrayObjectTest extends PHPUnit_Framework_TestCase {
  14.     /**
  15.      * Runs the test methods of this class.
  16.      *
  17.      * @access public
  18.      * @static
  19.      */
  20.     public static function main() {
  21.         require_once 'PHPUnit/TextUI/TestRunner.php';
  22.  
  23.         $suite  = new PHPUnit_Framework_TestSuite('ArrayObjectTest');
  24.         $result = PHPUnit_TextUI_TestRunner::run($suite);
  25.     }
  26.  
  27.     /**
  28.      * Sets up the fixture, for example, opens a network connection.
  29.      * This method is called before a test is executed.
  30.      *
  31.      * @access protected
  32.      */
  33.     protected function setUp() {
  34.     }
  35.  
  36.     /**
  37.      * Tears down the fixture, for example, closes a network connection.
  38.      * This method is called after a test is executed.
  39.      *
  40.      * @access protected
  41.      */
  42.     protected function tearDown() {
  43.     }
  44.  
  45.     /**
  46.      * @todo Implement testOffsetExists().
  47.      */
  48.     public function testOffsetExists() {
  49.         $this->markTestIncomplete('Este teste não foi implementado');
  50.     }
  51.  
  52.     /**
  53.      * @todo Implement testOffsetGet().
  54.      */
  55.     public function testOffsetGet() {
  56.         $o = new ArrayObject();
  57.         $o->append('Valor 1');
  58.  
  59.         $this->assertEquals('Valor 2', $o->offsetGet(0), '\'Valor 2\' não é igual a \'' . $o->offsetGet(0) . '\'');
  60.     }
  61.  
  62.     /**
  63.      * @todo Implement testOffsetSet().
  64.      */
  65.     public function testOffsetSet() {
  66.         $o = new ArrayObject();
  67.         $o->offsetSet(0, 10);
  68.  
  69.         $this->assertEquals(10, $o->offsetGet(0), '\<int> \'10\' não é igual a \'' . $o->offsetGet(0) . '\'');
  70.     }
  71.  
  72.     /**
  73.      * @todo Implement testOffsetUnset().
  74.      */
  75.     public function testOffsetUnset() {
  76.         $this->markTestSkipped('Teste saltado');
  77.     }
  78.  
  79.     /**
  80.      * @todo Implement testAppend().
  81.      */
  82.     public function testAppend() {
  83.         throw new Exception('Este teste irá gerar um erro');
  84.     }
  85. }
  86.  
  87. // Call ArrayObjectTest::main() if this source file is executed directly.
  88. if (PHPUnit_MAIN_METHOD == 'ArrayObjectTest::main') {
  89.     ArrayObjectTest::main();
  90. }
  91. ?>

salve este código em um arquivo chamado ArrayObjectTest.php

Compartilhe:
  • del.icio.us
  • Google
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • LinkedIn
  • Live
  • Rec6
  • Technorati
  • TwitThis
1 Estrela2 Estrela3 Estrela4 Estrela5 Estrela (Nenhuma avaliação ainda)
Loading ... Loading ...

Páginas: 1 2 3

No Comments yet »

Trackback URI | Comments RSS

Leave a Reply

« É anunciado o fim do PHP4 | Zend Framework - MVC na linha de comando »