SfCon: Test Driven Development

1,781 views
1,702 views

Published on

Uma pequena introdução a TDD

Published in: Technology, Business

SfCon: Test Driven Development

  1. 1. Test Driven DevelopmentSaturday, June 23, 2012
  2. 2. @AUGUSTOHP @ALGANETSaturday, June 23, 2012
  3. 3. • Evolução dos testes AGENDA • Motivações • TDD (interativo)Saturday, June 23, 2012
  4. 4. EVOLUÇÃO DOS TESTESSaturday, June 23, 2012
  5. 5. var_dump($coisa);Saturday, June 23, 2012
  6. 6. //var_dump($coisa);Saturday, June 23, 2012
  7. 7. Breakpoints e Watchers!Saturday, June 23, 2012
  8. 8. Breakpoints e Watchers!Saturday, June 23, 2012
  9. 9. Testes automatizadosSaturday, June 23, 2012
  10. 10. Testes automatizadosSaturday, June 23, 2012
  11. 11. ão açautomatizados fic Testes ri VeSaturday, June 23, 2012
  12. 12. Test Driven DevelopmentSaturday, June 23, 2012
  13. 13. MOTIVAÇÃOSaturday, June 23, 2012
  14. 14. CÓDIGO LIMPO QUE FUNCIONASaturday, June 23, 2012
  15. 15. Esse é o código CÓDIGO LIMPO QUE FUNCIONASaturday, June 23, 2012
  16. 16. Esse “somos nozes” CÓDIGO LIMPO QUE FUNCIONASaturday, June 23, 2012
  17. 17. Objetivo do TDD CÓDIGO LIMPO QUE FUNCIONASaturday, June 23, 2012
  18. 18. 2 REGRASSaturday, June 23, 2012
  19. 19. CÓDIGO NOVO = TESTESaturday, June 23, 2012
  20. 20. REFATORESaturday, June 23, 2012
  21. 21. O MANTRA DO TDDSaturday, June 23, 2012
  22. 22. Saturday, June 23, 2012
  23. 23. • Vermelho : Escreva um teste (ele vai falhar)Saturday, June 23, 2012
  24. 24. • Vermelho : Escreva um teste (ele vai falhar) • Verde : Faça o teste funcionarSaturday, June 23, 2012
  25. 25. • Vermelho : Escreva um teste (ele vai falhar) • Verde : Faça o teste funcionar • RefatoreSaturday, June 23, 2012
  26. 26. SESSÃO INTERATIVA DE TDDSaturday, June 23, 2012
  27. 27. Saturday, June 23, 2012
  28. 28. O que faremos?Saturday, June 23, 2012
  29. 29. • Lista de tarefasSaturday, June 23, 2012
  30. 30. • Lista de tarefas • TítuloSaturday, June 23, 2012
  31. 31. 1 <?php 2 class TaskTest extends PHPUnit_Framework_TestCase 3 { 4 public function testTitle() 5 { 6 $task = new SfConTask; 7 $title = Teste; 8 $task->setTitle($title); 9 $this->assertEquals($title, $task->getTitle()); 10 $this->assertEquals($title, (string) $task); 11 } 12 }Saturday, June 23, 2012
  32. 32. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 4 Fatal error: Class SfConTask not found in /Users/augustopascutti/Desktop/tdd/TaskTest.php on line 6Saturday, June 23, 2012
  33. 33. 1 <?php 2 namespace SfCon; 3 4 class Task 5 { 6 protected $title; 7 8 public function setTitle($string) 9 { 10 $this->title = $string; 11 return $this; 12 } 13 14 public function getTitle() 15 { 16 return $this->title; 17 } 18 19 public function __toString() 20 { 21 return (string) $this->getTitle(); 22 } 23 }Saturday, June 23, 2012
  34. 34. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 . 4 5 Time: 0 seconds, Memory: 2.75Mb 6 7 OK (1 test, 2 assertions)Saturday, June 23, 2012
  35. 35. • Lista de tarefas • Título • IDSaturday, June 23, 2012
  36. 36. 1 <?php 2 require SfCon/Task.php; 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 // ... 7 public function testId() 8 { 9 $task = new SfConTask(); 10 $id = 1; 11 $task->setId($id); 12 $this->assertEquals($id, $task->getId()); 13 } 14 }Saturday, June 23, 2012
  37. 37. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 . 4 Fatal error: Call to undefined method SfConTask::setId() in /Users/augustopascutti/Desktop/tdd/TaskTest.php online 19Saturday, June 23, 2012
  38. 38. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 // ... 7 public function testId() 8 { 9 $task = new SfConTask(); 10 $id = 1; 11 $task->setId($id); 12 $this->assertEquals($id, $task->getId()); 13 } 14 }Saturday, June 23, 2012
  39. 39. • Lista de tarefas • Título • ID • Completa?Saturday, June 23, 2012
  40. 40. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 // ... 7 public function testDone() 8 { 9 $task = new SfConTask(); 10 $this->assertFalse($task->isDone()); 11 $task->setDone(); // Default: true 12 $this->assertTrue($task->isDone()); 13 $task->setDone(false); 14 $this->assertFalse($task->isDone()); 15 } 16 }Saturday, June 23, 2012
  41. 41. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 .. 4 Fatal error: Call to undefined method SfConTask::isDone() in /Users/augustopascutti/Desktop/tdd/TaskTest.php on line 26Saturday, June 23, 2012
  42. 42. 1 <?php 2 namespace SfCon; 3 4 class Task 5 { 6 // ... 7 protected $done = false; 8 9 // ... 10 public function setDone($bool=true) 11 { 12 $this->done = (boolean) $bool; 13 return $this; 14 } 15 16 public function isDone() 17 { 18 return $this->done; 19 } 20 }Saturday, June 23, 2012
  43. 43. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 ... 4 5 Time: 0 seconds, Memory: 2.75Mb 6 7 OK (3 tests, 6 assertions)Saturday, June 23, 2012
  44. 44. • Lista de tarefas • Título • ID • Completa? • Salvar tarefaSaturday, June 23, 2012
  45. 45. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 // ... 7 public function testInsert() 8 { 9 $pdo = new Pdo(sqlite::memory:); 10 $pdo->exec(CREATE TABLE tasks ( 11 id INTEGER PRIMARY KEY, 12 title TEXT, 13 done INTEGER 14 )); 15 $task = new SfConTask($pdo); 16 $expectId = 1; 17 $task->setTitle(Test); 18 $task->insert(); // Insert defines ID 19 $this->assertEquals($expectId, $task->getId()); 20 } 21 }Saturday, June 23, 2012
  46. 46. 1 <?php 2 namespace SfCon; 3 4 class Task 5 { 6 // ... 7 protected $pdo; 8 9 public function __construct(Pdo $pdo=null) 10 { 11 if (!is_null($pdo)) 12 $this->pdo = $pdo; 13 } 14 // ... 15 public function insert() 16 { 17 $sql = INSERT INTO tasks (id, title, done) VALUES (?, ?, ?); 18 $st = $this->pdo->prepare($sql); 19 $st->bindValue(1, $this->getId()); 20 $st->bindValue(2, $this->getTitle()); 21 $st->bindValue(3, $this->isDone()); 22 $result = $st->execute(); 23 $this->setId($this->pdo->lastInsertId()); 24 return $result; 25 } 26 }Saturday, June 23, 2012
  47. 47. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 .... 4 5 Time: 0 seconds, Memory: 2.75Mb 6 7 OK (4 tests, 9 assertions)Saturday, June 23, 2012
  48. 48. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 Task 4 [x] Title 5 [x] Id 6 [x] Done 7 [x] InsertSaturday, June 23, 2012
  49. 49. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 public function testSetterGetterForTitle() 7 { 8 // ... 9 } 10 11 public function testSetterGetterForId() 12 { 13 // ... 14 } 15 16 public function testSetterGetterForDone() 17 { 18 // ... 19 } 20 }Saturday, June 23, 2012
  50. 50. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 Task 4 [x] Setter getter for title 5 [x] Setter getter for id 6 [x] Setter getter for done 7 [x] InsertSaturday, June 23, 2012
  51. 51. Saturday, June 23, 2012
  52. 52. Saturday, June 23, 2012
  53. 53. Saturday, June 23, 2012
  54. 54. • Lista de tarefas • Cobrir variações • Título • ID • Completa? • Salvar tarefaSaturday, June 23, 2012
  55. 55. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 // ... 7 public function provideValidTitles() 8 { 9 return array( 10 array(This is a valid title), 11 array(This is also a valid title ...), 12 array(Hello World), 13 array(Hakuna Matata), 14 array(Do some more tests) 15 ); 16 } 17 18 /** 19 * @dataProvider provideValidTitles 20 */ 21 public function testSetterGetterForTitle($title) 22 { 23 $this->fixture->setTitle($title); 24 $this->assertEquals($title, $this->fixture->getTitle()); 25 $this->assertEquals($title, (string) $this->fixture); 26 } 27 // ...Saturday, June 23, 2012
  56. 56. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 protected $fixture; 7 protected $pdo; 8 9 public function setUp() 10 { 11 $this->pdo = new Pdo(sqlite::memory:); 12 $this->fixture = new SfConTask($this->pdo); 13 $this->pdo->exec(CREATE TABLE IF NOT EXISTS tasks ( 14 id INTEGER PRIMARY KEY, 15 title TEXT, 16 done INTEGER 17 )); 18 } 19 20 public function tearDown() 21 { 22 $this->pdo->exec(DROP TABLE tasks); 23 } 24 // ... 25 }Saturday, June 23, 2012
  57. 57. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 .............. 4 5 Time: 0 seconds, Memory: 2.75Mb 6 7 OK (14 tests, 45 assertions)Saturday, June 23, 2012
  58. 58. Saturday, June 23, 2012
  59. 59. CUIDADO COM O 100% DE COVERAGESaturday, June 23, 2012
  60. 60. Linhas não testadasSaturday, June 23, 2012
  61. 61. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 /** 7 * @dataProvider provideValidTitles 8 */ 9 public function testSetterGetterForTitle($title) 10 { 11 $instance = $task->setTitle($title); 12 $this->assertEquals($task, $instance); 13 $this->assertEquals($title, $task->getTitle()); 14 $this->assertEquals($title, (string) $task); 15 } 16 }Saturday, June 23, 2012
  62. 62. • Lista de tarefas • Cobrir variações • Título • Mocks / Stubs • ID • Completa? • Salvar tarefaSaturday, June 23, 2012
  63. 63. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 public function testInsert() 7 { 8 $con = array(sqlite::memory:); 9 $met = array(prepare, lastInsertId);10 // ...11 $this->pdo = $this->getMock(Pdo, $met, $con);12 $this->pdo->expects($this->once())13 ->method(prepare)14 ->with($this->equalTo(SfConTask::SQL_INSE15 ->will($this->returnValue($mockIns));16 }17 }Saturday, June 23, 2012
  64. 64. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 public function testInsert() 7 { 8 // ... 9 $met = array(bindValue, execute);10 $mockIns = $this->getMock(PdoStatement, $met);11 $mockIns->expects($this->exactly(3))12 ->method(bindValue)13 ->with($this->greaterThan(0),14 $this->anything());15 // ...16 }17 }Saturday, June 23, 2012
  65. 65. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 public function testInsert() 7 { 8 // ... 9 $mockIns->expects($this->once())10 ->method(execute)11 ->will($this->returnValue(true));12 // ...13 }14 }Saturday, June 23, 2012
  66. 66. 1 <?php 2 require SfCon/Task.php; 3 4 class TaskTest extends PHPUnit_Framework_TestCase 5 { 6 public function testInsert() 7 { 8 // ... 9 $this->pdo->expects($this->once())10 ->method(lastInsertId)11 ->will($this->returnValue(1));1213 $task = new SfConTask($this->pdo);14 $task->setTitle($title);15 $task->insert();16 $this->assertEquals($expectId, $task->getId());17 }18 }Saturday, June 23, 2012
  67. 67. 1 PHPUnit 3.6.10 by Sebastian Bergmann. 2 3 ............... 4 5 Time: 0 seconds, Memory: 3.25Mb 6 7 OK (15 tests, 45 assertions)Saturday, June 23, 2012
  68. 68. • Lista de tarefas • Cobrir variações • Título • Mocks / Stubs • ID • tas? Bugs regressivos u n • Completa? e rg • Salvar tarefa pSaturday, June 23, 2012

×