Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Aplicações Comportadas com Behat

Aplicações Comportadas com Behat

Avatar for Alexandre Gaigalas

Alexandre Gaigalas

June 23, 2012
Tweet

More Decks by Alexandre Gaigalas

Other Decks in Programming

Transcript

  1. 23 de Junho sfcon 2012 4 Testes automatizados não existem.

    Existem verificações automatizadas. – Michael Bolton
  2. 23 de Junho sfcon 2012 7 Testar é explorar e

    aprender. Performance Acessibilidade
  3. 23 de Junho sfcon 2012 8 Testar é explorar e

    aprender. Código Performance Acessibilidade
  4. 23 de Junho sfcon 2012 9 Testar é explorar e

    aprender. Código Performance Acessibilidade Infra
  5. 23 de Junho sfcon 2012 10 Testar é explorar e

    aprender. Código Design Performance Acessibilidade Infra
  6. 23 de Junho sfcon 2012 11 Testar é explorar e

    aprender. Código Design Performance Acessibilidade Segurança Infra
  7. 23 de Junho sfcon 2012 12 Testar é explorar e

    aprender. Código Design Performance Funcionalidade Acessibilidade Segurança Infra
  8. 23 de Junho sfcon 2012 16 TDD BDD • Testar

    código • Testar software
  9. 23 de Junho sfcon 2012 17 TDD BDD • Testar

    código • Verifica unidade • Testar software
  10. 23 de Junho sfcon 2012 18 TDD BDD • Testar

    código • Verifica unidade • Testar software • Verifica feature
  11. 23 de Junho sfcon 2012 19 TDD BDD • Testar

    código • Verifica unidade • Melhora o código • Testar software • Verifica feature
  12. 23 de Junho sfcon 2012 20 TDD BDD • Testar

    código • Verifica unidade • Melhora o código • Testar software • Verifica feature • Melhora o software
  13. 23 de Junho sfcon 2012 21 TDD BDD • Testar

    código • Verifica unidade • Melhora o código • Casos de teste • Testar software • Verifica feature • Melhora o software
  14. 23 de Junho sfcon 2012 22 TDD BDD • Testar

    código • Verifica unidade • Melhora o código • Casos de teste • Testar software • Verifica feature • Melhora o software • Casos de uso
  15. 23 de Junho sfcon 2012 24 TDD BDD • Código

    testLike() • Comportamento
  16. 23 de Junho sfcon 2012 25 TDD BDD • Código

    • Comportamento testLike() shouldIncreaseLikeCount()
  17. 23 de Junho sfcon 2012 26 TDD BDD • Código

    • Comportamento testLike() shouldIncreaseLikeCount() Given I am in the page “Home” And The like count is “2” And I haven't liked it before When I like the page Then I should see like count “3”
  18. 23 de Junho sfcon 2012 27 TDD BDD • Código

    • Comportamento testLike() shouldIncreaseLikeCount() Given I am in the page “Home” And The like count is “2” And I haven't liked it before When I like the page Then I should see like count “3” Spec Estória
  19. 23 de Junho sfcon 2012 30 Spec Estória Vou ver

    aqui dentro como o software se comporta Blackbox
  20. 23 de Junho sfcon 2012 31 Spec Estória Vou ver

    aqui dentro como o software se comporta Vou ver aqui de fora como o software se comporta
  21. 23 de Junho sfcon 2012 32 class User { public

    $id; public function __construct($id) { $this->id = $id; } } class Page { public $likeCount = 0; public $likes = array(); public function likedBy(User $user) { $this->likes[$user->id] = true; $this->likeCount++; } public function hasLikes() { return ($likeCount + count($this->likes)) > 0; } } Nosso código maroto
  22. 23 de Junho sfcon 2012 33 function testLike() { $home

    = new Page("Home"); $home->likeCount = 2; $home->likedBy(new User("alganet")); $this->assertCount(3, $home->likeCount); } Teste Unitário
  23. 23 de Junho sfcon 2012 34 function testLike() { $home

    = new Page("Home"); $home->likeCount = 2; $home->likedBy(new User("alganet")); $this->assertCount(3, $home->likeCount); } Teste Unitário Verificação da implementação
  24. 23 de Junho sfcon 2012 35 function shouldIncreaseLikeCountWhenUserLikesPage() { $home

    = new Page("Home"); $home->likeCount = 2; $home->likedBy(new User("alganet")); $this->spec($home->likeCount)->should->equal(3); } function shouldHaveNoLikesIfNoOneLiked() { $home = new Page("Home"); $this->spec($home)->shouldNot->haveLikes(); } function shouldHaveLikesIfSomeoneLikedIt() { $home = new Page("Home"); $home->likedBy(new User("alganet")); $this->spec($home)->should->haveLikes(); } BDD com Specs
  25. 23 de Junho sfcon 2012 36 function shouldIncreaseLikeCountWhenUserLikesPage() { $home

    = new Page("Home"); $home->likeCount = 2; $home->likedBy(new User("alganet")); $this->spec($home->likeCount)->should->equal(3); } function shouldHaveNoLikesIfNoOneLiked() { $home = new Page("Home"); $this->spec($home)->shouldNot->haveLikes(); } function shouldHaveLikesIfSomeoneLikedIt() { $home = new Page("Home"); $home->likedBy(new User("alganet")); $this->spec($home)->should->haveLikes(); } BDD com Specs Verificação do comportamento
  26. 23 de Junho sfcon 2012 37 $ touch features/like.feature $

    vim features/like.feature Feature: Like In order to express what I like As an user I need to be able to like pages Scenario: Like a single page Given I am in the page "Home" And The like count is "2" And I haven't liked it before When I like the page Then I should see like count "3" $ bin/behat Finalmente, Behat!
  27. 23 de Junho sfcon 2012 38 $ touch features/like.feature $

    vim features/like.feature Feature: Like In order to express what I like As an user I need to be able to like pages Scenario: Like a single page Given I am in the page "Home" And The like count is "2" And I haven't liked it before When I like the page Then I should see like count "3" $ bin/behat Gherkin Finalmente, Behat!
  28. 23 de Junho sfcon 2012 39 $ touch features/like.feature $

    vim features/like.feature Feature: Like In order to express what I like As an user I need to be able to like pages Scenario: Like a single page Given I am in the page "Home" And The like count is "2" And I haven't liked it before When I like the page Then I should see like count "3" $ bin/behat Gherkin Finalmente, Behat! Behat CLI
  29. 23 de Junho sfcon 2012 40 You can implement step

    definitions for undefined steps with these snippets: /** * @Given /^I am in the page "([^"]*)"$/ */ public function iAmInThePage($arg1) { throw new PendingException(); } /** * @Given /^The like count is "([^"]*)"$/ */ public function theLikeCountIs($arg1); /** * @Given /^I haven\'t liked it before$/ */ public function iHavenTLikedItBefore(); /** * @When /^I like the page$/ */ public function iLikeThePage(); /*...*/
  30. 23 de Junho sfcon 2012 41 You can implement step

    definitions for undefined steps with these snippets: /** * @Given /^I am in the page "([^"]*)"$/ */ public function iAmInThePage($arg1) { throw new PendingException(); } /** * @Given /^The like count is "([^"]*)"$/ */ public function theLikeCountIs($arg1); /** * @Given /^I haven\'t liked it before$/ */ public function iHavenTLikedItBefore(); /** * @When /^I like the page$/ */ public function iLikeThePage(); /*...*/ Contexto Pendente
  31. 23 de Junho sfcon 2012 42 function iAmInThePage($name) { $this->user

    = new User("alganet"); $this->page = new Page($name); } function theLikeCountIs($count) { $this->page->likeCount = $count; } function IHaventLikedItBefore() { if (isset($this->page->likes[$this->user->id])) unset($this->page->likes[$this->user->id]); } Contextos de BDD com Estórias
  32. 23 de Junho sfcon 2012 43 function ILikeThePage($name) { $this->page->likedBy($this->user);

    } function IShouldSeeLikeCount($number) { if ($number !== $this->page->likeCount) throw new Exception("Like count is in fact $number"); } Contextos de BDD com Estórias
  33. 23 de Junho sfcon 2012 44 function ILikeThePage($name) { $this->page->likedBy($this->user);

    } function IShouldSeeLikeCount($number) { if ($number !== $this->page->likeCount) throw new Exception("Like count is in fact $number"); } Contextos de BDD com Estórias PHP Puro
  34. 23 de Junho sfcon 2012 45 function ILikeThePage($name) { $this->page->likedBy($this->user);

    } function IShouldSeeLikeCount($number) { assertCount($number, $this->page->likeCount); } Contextos de BDD com Estórias PHPUnit
  35. 23 de Junho sfcon 2012 46 function ILikeThePage($name) { $this->page->likedBy($this->user);

    } function IShouldSeeLikeCount($number) { v::equals($number)->assert($this->page->likeCount); } Contextos de BDD com Estórias Respect\Validation
  36. 23 de Junho sfcon 2012 47 $ bin/behat -f progress

    ......... 1 scenarios (1 passed, 0 failed) 5 steps (5 passed, 0 failed) 0m1.843s
  37. 23 de Junho sfcon 2012 50 $ cat features/like.feature Feature:

    Like In order to express what I like As an user I need to be able to like pages Scenario: Like a single page Given I am in the page "Home" And The like count is "2" And I haven't liked it before When I like the page Then I should see like count "3" $ bin/behat
  38. 23 de Junho sfcon 2012 51 $ cat features/like.feature Feature:

    Like In order to express what I like As an user I need to be able to like pages Scenario: Like a single page Given I am in the page "Home" And The like count is "2" And I haven't liked it before When I like the page Then I should see like count "3" $ bin/behat Mesma feature
  39. 23 de Junho sfcon 2012 52 function iAmInThePage($name) { $this->pageName

    = $name; $this->session->visit("http://example.com/$name"); } function theLikeCountIs($count) { $pageData = $this->mapper ->page(array('name' => $this->pageName)) ->fetch(); $pageData->like_count = $count; $this->mapper->page->persist($pageData); $this->mapper->flush(); } Outro contexto, mesma estória
  40. 23 de Junho sfcon 2012 53 function ILikeThePage($name) { $session->getPage()->findButton("like")->click();

    } function IShouldSeeLikeCount($number) { $pageLikeCount = $this->session ->getPage() ->find('css', '.likeCount') ->getText(); if ($number != $pageLikeCount); throw new Exception("Like count is in fact $number"); } Outro contexto, mesma estória
  41. 23 de Junho sfcon 2012 54 function ILikeThePage($name) { $session->getPage()->findButton("like")->click();

    } function IShouldSeeLikeCount($number) { $pageLikeCount = $this->session ->getPage() ->find('css', '.likeCount') ->getText(); if ($number != $pageLikeCount); throw new Exception("Like count is in fact $number"); } Outro contexto, mesma estória Mink
  42. 23 de Junho sfcon 2012 57 Behat e Mink •

    Perfis $ behat --profile dev $ behat --profile jenkins
  43. 23 de Junho sfcon 2012 58 Behat e Mink •

    Perfis • Tags $ behat --profile dev $ behat --profile jenkins
  44. 23 de Junho sfcon 2012 59 Behat e Mink •

    Perfis • Tags $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless'
  45. 23 de Junho sfcon 2012 60 Behat e Mink •

    Perfis • Tags $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless,@api' $ behat --tags '~@javascript'
  46. 23 de Junho sfcon 2012 61 Behat e Mink •

    Perfis • Tags • Drivers $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless,@api' $ behat --tags '~@javascript'
  47. 23 de Junho sfcon 2012 62 Behat e Mink •

    Perfis • Tags • Drivers • Goutte, Selenium, Selenium2, ... $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless,@api' $ behat --tags '~@javascript'
  48. 23 de Junho sfcon 2012 63 Behat e Mink •

    Perfis • Tags • Drivers • Goutte, Selenium, Selenium2, ... • Contextos Comuns $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless,@api' $ behat --tags '~@javascript'
  49. 23 de Junho sfcon 2012 64 Behat e Mink •

    Perfis • Tags • Drivers • Goutte, Selenium, Selenium2, ... • Contextos Comuns • MinkContext, SymfonyDoctrineContext, ... $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless,@api' $ behat --tags '~@javascript'
  50. 23 de Junho sfcon 2012 65 Resumão • Testar !==

    Verificar • Um teste não substitui outro • BDD é sobre escrever estórias