Share
Embed
Aplicações Comportadas com Behat
Alexandre Gaigalas
June 23, 2012
Transcript
67 slides
-
23 de Junho sfcon 2012 2 Alexandre Gomes Gaigalas http://about.me/alganet
Augusto Pascutti http://about.me/augustohp -
23 de Junho sfcon 2012 4 Testes automatizados não existem.
Existem verificações automatizadas. – Michael Bolton -
23 de Junho sfcon 2012 6 Testar é explorar e
aprender. Performance -
23 de Junho sfcon 2012 7 Testar é explorar e
aprender. Performance Acessibilidade -
23 de Junho sfcon 2012 8 Testar é explorar e
aprender. Código Performance Acessibilidade -
23 de Junho sfcon 2012 9 Testar é explorar e
aprender. Código Performance Acessibilidade Infra -
23 de Junho sfcon 2012 10 Testar é explorar e
aprender. Código Design Performance Acessibilidade Infra -
23 de Junho sfcon 2012 11 Testar é explorar e
aprender. Código Design Performance Acessibilidade Segurança Infra -
23 de Junho sfcon 2012 12 Testar é explorar e
aprender. Código Design Performance Funcionalidade Acessibilidade Segurança Infra -
23 de Junho sfcon 2012 13 Um tipo de teste
não substitui outro. -
23 de Junho sfcon 2012 16 TDD BDD • Testar
código • Testar software -
23 de Junho sfcon 2012 17 TDD BDD • Testar
código • Verifica unidade • Testar software -
23 de Junho sfcon 2012 18 TDD BDD • Testar
código • Verifica unidade • Testar software • Verifica feature -
23 de Junho sfcon 2012 19 TDD BDD • Testar
código • Verifica unidade • Melhora o código • Testar software • Verifica feature -
23 de Junho sfcon 2012 20 TDD BDD • Testar
código • Verifica unidade • Melhora o código • Testar software • Verifica feature • Melhora o software -
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 -
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 -
23 de Junho sfcon 2012 23 TDD BDD • Código
• Comportamento -
23 de Junho sfcon 2012 24 TDD BDD • Código
testLike() • Comportamento -
23 de Junho sfcon 2012 25 TDD BDD • Código
• Comportamento testLike() shouldIncreaseLikeCount() -
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” -
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 -
23 de Junho sfcon 2012 30 Spec Estória Vou ver
aqui dentro como o software se comporta Blackbox -
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 -
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 -
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 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 -
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 -
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 -
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! -
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! -
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 -
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(); /*...*/ -
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 -
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 -
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 -
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 -
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 -
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 -
23 de Junho sfcon 2012 47 $ bin/behat -f progress
......... 1 scenarios (1 passed, 0 failed) 5 steps (5 passed, 0 failed) 0m1.843s -
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 -
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 -
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 -
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 -
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 -
23 de Junho sfcon 2012 57 Behat e Mink •
Perfis $ behat --profile dev $ behat --profile jenkins -
23 de Junho sfcon 2012 58 Behat e Mink •
Perfis • Tags $ behat --profile dev $ behat --profile jenkins -
23 de Junho sfcon 2012 59 Behat e Mink •
Perfis • Tags $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless' -
23 de Junho sfcon 2012 60 Behat e Mink •
Perfis • Tags $ behat --profile dev $ behat --profile jenkins $ behat --tags '@headless,@api' $ behat --tags '~@javascript' -
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' -
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' -
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' -
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' -
23 de Junho sfcon 2012 65 Resumão • Testar !==
Verificar • Um teste não substitui outro • BDD é sobre escrever estórias