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

Story BDD with Behat

Story BDD with Behat

Saša Stamenković

November 12, 2015
Tweet

More Decks by Saša Stamenković

Other Decks in Programming

Transcript

  1. Story BDD with Behat
    Agile Testing Days 2015

    View Slide

  2. Saša Stamenković
    @umpirsky

    View Slide

  3. View Slide

  4. KNP Labs

    View Slide

  5. View Slide

  6. BDD

    View Slide

  7. Bug Driven Development

    View Slide

  8. Beer Driven Development

    View Slide

  9. Behaviour Driven Development

    View Slide

  10. The Problem

    View Slide

  11. How the customer explained it.

    View Slide

  12. How the project leader understood it.

    View Slide

  13. How the programmer wrote it.

    View Slide

  14. What the beta testers received.

    View Slide

  15. How the project was documented.

    View Slide

  16. What the customer really needed.

    View Slide

  17. The Solution

    View Slide

  18. Let's all use same vocabulary
    Developer Project manager Product owner Business analyst Tester

    View Slide

  19. ...for planning, implementing and testing feature
    Developer Project manager Product owner Business analyst Tester

    View Slide

  20. ...with the focus on the behavior of the feature
    Developer Project manager Product owner Business analyst Tester

    View Slide

  21. StoryBDD helps ensuring
    that development team has
    understanding of business
    on the same level that
    client does.
    And even leveling up
    clients knowledge
    of his own business.

    View Slide

  22. But how?

    View Slide

  23. By forcing you to answer
    business questions.

    View Slide

  24. Who?
    Who benefits
    from this feature?

    View Slide

  25. What?
    The business
    value.

    View Slide

  26. How?
    Feature description.

    View Slide

  27. Gherkin

    View Slide

  28. A structured language to
    describe a feature.

    View Slide

  29. Feature: News admin panel
    In order to maintain a list of news
    As a site administrator
    I need to be able to edit news

    View Slide

  30. Feature: News admin panel
    In order to maintain a list of news
    As a site administrator
    I need to be able to edit news
    custom title

    View Slide

  31. Feature: News admin panel
    In order to maintain a list of news
    As a site administrator
    I need to be able to edit news
    business value

    View Slide

  32. Feature: News admin panel
    In order to maintain a list of news
    As a site administrator
    I need to be able to edit news
    who benefits

    View Slide

  33. Feature: News admin panel
    In order to maintain a list of news
    As a site administrator
    I need to be able to edit news
    feature desc.

    View Slide

  34. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"

    View Slide

  35. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"
    scenario title

    View Slide

  36. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"
    initial state

    View Slide

  37. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"
    taking action

    View Slide

  38. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"
    can be added to
    create multiple...

    View Slide

  39. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"
    ...Given/When/Then

    View Slide

  40. Scenario: Add new article
    Given I am on the "/admin/news" page
    When I click "New Article"
    And I fill in "Title" with "Learn BDD"
    And I press "Save"
    Then I should see "A new article was added"
    asserting state of
    the system

    View Slide

  41. Gherkin
    Consistent language to describe
    features and their scenarios.

    View Slide

  42. it’s something

    View Slide

  43. View Slide

  44. Imagine that we can execute
    Gherkin sentences as functional
    tests.

    View Slide

  45. Behat
    Maps each step (Gherkin
    sentence) to a PHP callback.

    View Slide

  46. $ composer require behat/behat
    Installing Behat

    View Slide

  47. $ php bin/behat --init
    +d features - place your *.feature files here
    +d features/bootstrap - place your context classes here
    +f features/bootstrap/FeatureContext.php - place your
    definitions, transformations and hooks here
    Initialization

    View Slide

  48. I didn't write
    Linux using BDD.

    View Slide

  49. But we can!

    View Slide

  50. Feature: ls
    In order to see the directory structure
    As a UNIX user
    I need to be able to list directory's contents
    features/ls.feature

    View Slide

  51. Scenario: List 2 files in a directory
    Given I have a file named "foo"
    And I have a file named "bar"
    When I run "ls"
    Then I should see "foo" in the output
    And I should see "bar" in the output
    features/ls.feature

    View Slide

  52. Talk is cheap.
    Show me the
    code.

    View Slide

  53. View Slide

  54. We can map each step to a PHP
    callback with Behat.

    View Slide

  55. NOT BAD

    View Slide

  56. Mink

    View Slide

  57. Mink
    Mink is an open source browser
    controller/emulator for web
    applications.

    View Slide

  58. One API
    ★ Goutte
    ★ Selenium
    ★ Zombie.js
    ★ ...

    View Slide

  59. Using Mink
    use Behat\Mink\Session;
    use Behat\Mink\Driver\GoutteDriver;
    $session = new Session(new GoutteDriver());
    $session->visit($startUrl);
    $session->getPage()->findLink('Downloads')->click();
    echo 'Status: '.$session->getPage()->getStatusCode();
    echo 'Content: '.$session->getPage()->getContent();

    View Slide

  60. Mink inside
    FeatureContext.

    View Slide

  61. Mink Extension
    Easy to use Mink inside
    FeatureContext.

    View Slide

  62. Update dependencies
    {
    "require": {
    "behat/behat": "~3.0",
    "behat/mink-extension": "~2.0",
    "behat/mink-goutte-driver": "~1.0",
    "behat/mink-selenium2-driver": "~1.1"
    }
    }

    View Slide

  63. Mink inside FeatureContext
    use Behat\MinkExtension\Context\RawMinkContext;
    class FeatureContext extends RawMinkContext
    {
    public function doSomething()
    {
    $this->getSession()
    ->visit('https://github.com/')
    ;
    }
    }

    View Slide

  64. Yes, moar of that!

    View Slide

  65. FeatureContext extends MinkContext
    use Behat\MinkExtension\Context\MinkContext;
    class FeatureContext extends MinkContext
    {
    public function doSomething()
    {
    $this->visit('/foo');
    $this->selectOption('Country', 'Germany');
    $this->pressButton('Save');
    $this->assertPageContainsText('Country changed.');
    }
    }

    View Slide

  66. After adding MinkContext
    $ ./bin/behat -dl
    default | Given /^(?:|I )am on "(?P[^"]+)"$/
    default | When /^(?:|I )reload the page$/
    default | When /^(?:|I )move backward one page$/
    default | When /^(?:|I )move forward one page$/
    default | When /^(?:|I )press "(?P(?:[^"]|\\")*)"$/
    default | When /^(?:|I )follow "(?P(?:[^"]|\\")*)"$/
    default | Then /^(?:|I )should be on "(?P[^"]+)"$/
    default | Then /^(?:|I )should see "(?P(?:[^"]|\\")*)"$/
    ...

    View Slide

  67. Which means
    We can write functional tests
    without writing a single line of
    PHP code!

    View Slide

  68. View Slide

  69. Feature: Search
    In order to find articles
    As a website visitor
    I need to be able to search for articles
    features/search.feature

    View Slide

  70. Scenario: Searching for an article that exists
    Given I am on the homepage
    When I fill in "search" with "BDD"
    And I press "go"
    And I follow "Behavior-driven development"
    Then I should see "History"
    And I should see "Principles of BDD"
    And I should see "Story versus specification"
    features/search.feature

    View Slide

  71. View Slide

  72. Scenario: Finishing my talk
    Given I am done with talking
    When I ask "Are there any questions?"
    Then I should see your hands in the air

    View Slide