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

Behat by example

Behat by example

Behat is a known tool in php community. It's easy to learn and use. The hard part is to get into methodologies behind it, you need to spend quite some time thinking and understanding them in order to be successful with the tool. This talk will introduce you to the world of BDD by showing real life examples of how to use it for describing your application business rules and be successful.

Video: http://www.youtube.com/watch?v=QnPmbQbsTV0

Konstantin Kudryashov

June 08, 2012
Tweet

More Decks by Konstantin Kudryashov

Other Decks in Programming

Transcript

  1. B!"#$
    b y e x a m p l e
    B!"#$

    View Slide

  2. @everzet
    Konstantin
    Kudryashov

    View Slide

  3. This talk is not about:
    • how to setup Behat, Mink, *Extension
    • how to write your FeatureContext’s
    This talk is all about:
    • how to fix communications between you
    and your clients with StoryBDD
    • how to build successful products by
    concentrating on real business values
    • how to become a real Behat pro

    View Slide

  4. Behavior
    Driven
    Development

    View Slide

  5. Behavior
    Driven
    Development
    5.3+

    View Slide

  6. View Slide

  7. 1st
    commit
    2010-06-30

    View Slide

  8. 1st
    commit
    2010-06-30
    30 june -
    2y

    View Slide

  9. Some projects
    already using

    View Slide

  10. Some projects
    thinking
    just

    View Slide

  11. Some users
    comparing
    are

    View Slide

  12. Some
    comparing
    are
    users

    View Slide


  13. I would never have imagined that
    Behat and Mink were going to even
    compare to Cucumber and Capybara,
    but it does!!
    Brett Scott
    lead web developer @ bbc.com

    View Slide

  14. Is Behat awesome
    Functional testing
    Framework ?

    View Slide

  15. You

    View Slide

  16. are

    View Slide

  17. Wrong!

    View Slide

  18. It is awesome,
    but is it Functional testing
    Framework ?

    View Slide

  19. It is awesome,
    but is it Functional testing
    Framework ?

    View Slide

  20. NO,
    it s NOT

    View Slide

  21. We are not testing that application
    functions as we (developers) expect
    it to, but instead we’re testing that
    it fullfils clients business needs.

    View Slide

  22. Some applications could easily
    fullfill developers expectations and
    still not being able to cover business
    needs.

    View Slide

  23. You might think about Behat as a
    tool to teach your clients about
    testing, but in reality it's a tool to
    teach you about client business.

    View Slide

  24. Exploration
    OF Domain area

    View Slide

  25. 3 levels
    of domain
    exploration

    View Slide

  26. 1
    2
    3
    narrative
    possible scenarios
    scenario details

    View Slide

  27. Feature: Authorization
    In order to get access to the shopping history
    As a frequent buyer
    I need to be able to authenticate
    1
    2
    3
    possible scenarios
    scenario details

    View Slide

  28. Feature: Authorization
    In order to get access to the shopping history
    As a frequent buyer
    I need to be able to authenticate
    Scenario: Successfully authenticating
    with correct credentials
    1
    2
    3
    scenario details

    View Slide

  29. Feature: Authorization
    In order to get access to the shopping history
    As a frequent buyer
    I need to be able to authenticate
    Scenario: Successfully authenticating
    with correct credentials
    Given there is a user “everzet” with password “123”
    And I am on the login page
    When I fill in “username” with “everzet”
    And I fill in “password” with “123”
    And I press “login”
    Then I should see “Hello, everzet”
    1
    2
    3

    View Slide

  30. Feature: Authorization
    In order to get access to the shopping history
    As a frequent buyer
    I need to be able to authenticate
    Scenario: Successfully authenticating
    with correct credentials
    Scenario: Can not authenticate
    with wrong credentials
    Scenario: Blocked user can not
    authenticate, even
    with correct credentials

    View Slide

  31. Feature: Authorization
    In order to get access to the shopping history
    As a frequent buyer
    I need to be able to authenticate
    user
    story
    =>

    View Slide

  32. StoryBDD helps ensuring that
    development team has
    understanding of business on the
    same level that client does

    View Slide

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

    View Slide

  34. How?

    View Slide

  35. by Forcing you
    business
    questions
    to answer

    View Slide

  36. 1. For who?

    View Slide

  37. 1. For who?
    2. What?

    View Slide

  38. 1. For who?
    2. What?
    3. How?

    View Slide

  39. 1. For who?
    2. What?
    3. How?
    Why?

    View Slide

  40. ‘NuFF
    SAID

    View Slide

  41. case #1
    About Registration

    View Slide


  42. I want visitors to be able to register
    on my website through simple
    registration form.
    Your hypothetical client

    View Slide

  43. 1.Who?

    View Slide

  44. 1.Who?
    users?

    View Slide

  45. 1.Who?
    users visitors

    View Slide

  46. 1.Who?
    users visitors
    2.Why?

    View Slide

  47. 1.Who?
    users visitors
    2.Why?
    it’s common?

    View Slide

  48. 1.Who?
    users visitors
    2.Why?
    it’s common
    buyings history

    View Slide

  49. Know
    your client
    clients!

    View Slide

  50. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site

    View Slide

  51. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration

    View Slide

  52. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    benefit
    role (benefitiar)

    View Slide

  53. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    benefit
    role (benefitiar)
    in order to ...
    as a ...
    i need to ...
    Feature: ...
    benefit
    role (benefitiar)
    in order to ...
    as a ...
    i need to ...
    Feature: ...
    benefit
    role (benefitiar)
    in order to ...
    as a ...
    i need to ...
    Feature: ...
    benefit
    role (benefitiar)

    View Slide

  54. in order to ...
    as a ...
    i need to ...
    Feature: ...
    benefit
    role (benefitiar)
    in order to ...
    as a ...
    i need to ...
    Feature: ...
    benefit
    role (benefitiar)
    in order to ...
    as a ...
    i need to ...
    Feature: ...
    benefit
    role (benefitiar)
    in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    benefit
    role (benefitiar)
    SELECT f.*
    FROM ‘/features’ as f
    ORDER BY f.role, f.benefit
    LIMIT 10
    PSEUDOAgileQL

    View Slide

  55. Use
    gained
    knowledge!

    View Slide

  56. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration

    View Slide

  57. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info

    View Slide

  58. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Scenario: Unable to register
    when visitor misses
    required info

    View Slide

  59. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Scenario: Unable to register
    when visitor misses
    required info
    Scenario: ...

    View Slide

  60. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Scenario: Unable to register
    when visitor misses
    required info
    Scenario: ...
    Scenario: ...
    Scenario: ...
    Scenario: ...
    Scenario: ...

    View Slide

  61. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on “/register”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then the response code should be 200
    And the url should be “/”

    View Slide

  62. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on “/register”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then the response code should be 200
    And the url should be “/”

    View Slide

  63. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on “/register”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then the response code should be 200
    And the url should be “/”
    ?

    View Slide

  64. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then the response code should be 200
    And I should be on homepage

    View Slide

  65. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then the response code should be 200
    And I should be on homepage

    View Slide

  66. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then I should see “You have been successfully registered”
    And I should be on homepage

    View Slide

  67. in order to maintain my shopping history
    as a site visitor
    i need to be able to register on this site
    Feature: registration
    Scenario: Successful registration
    when visitor provides
    all the required info
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Then I should see “You have been successfully registered”
    And I should be on homepage

    View Slide

  68. case #2
    About Sending Emails

    View Slide


  69. I want my users to be able to send
    feedback messages through contact
    form.
    Your hypothetical client

    View Slide

  70. 1.Who?

    View Slide

  71. 1.Who?
    users

    View Slide

  72. 1.Who?
    users
    2.Why?

    View Slide

  73. 1.Who?
    users
    2.Why?
    to give feedback

    View Slide

  74. in order to give feedback
    as a site user
    i need to be able to use contact form

    View Slide

  75. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form

    View Slide

  76. Scenario: Successfuly sending
    feedback through a
    contact form
    in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form

    View Slide

  77. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Given I am on homepage
    And I follow “contact-us”
    And I do not follow redirects
    When I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  78. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Given I am on homepage
    And I follow “contact-us”
    And I do not follow redirects
    When I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”
    This text is a very
    boring text and I bet you
    will not manage to read the
    whole text since the only period
    in the text is the one that ends the whole
    text that is by the way so long and
    pointless that writing it is just as fun as
    the text becomes boring since it is so long and pointless and also
    quite boring since it does not happen a single thing the whole
    time it is going on which is very long so you will probably not even
    manage reading to this point which is somewhere halfway and yet you do incredibly as
    it is since you are reading these words in this long and boring text where nothing
    happens at all but you will probably stop reading soon as it is just too boring and long
    and pointless and I notice you are still reading my boring text and wonder how you can withstand it since it is so
    long and boring plus there is more fun things to do that I would prefer to do rather than reading this soulless
    text that is really damn boring and now even I am starting to get tired of it so now I am cutting it out.
    http://sou1forged.deviantart.com/art/Long-and-pointless-text-87288835

    View Slide

  79. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am on homepage
    And I follow “sign up”
    When I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    Given I am on homepage
    And I follow “contact-us”
    And I do not follow redirects
    When I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  80. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am on homepage
    And I follow “sign up”
    And I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  81. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am on homepage
    And I follow “sign up”
    And I fill in “email” with “[email protected]
    And I fill in “username” with “linkedin”
    And I fill in “password” with “sha1_without_salt”
    And I press “register”
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  82. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  83. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  84. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”

    View Slide

  85. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside “.notifications > p:first-child”
    Hey, techies
    it’s for
    you. Again!

    View Slide

  86. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside notification area

    View Slide

  87. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I do not follow redirects
    And I fill in “text” with “hello, guys”
    And I press “send”
    Then email with the subject “feedback” should have been sent
    And I should be redirected
    And I should see “message sent” inside notification area

    View Slide

  88. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I fill in “text” with “hello, guys”
    And I send a feedback
    Then the feedback email should have been sent
    And I should see “message sent” inside notification area

    View Slide

  89. in order to give feedback
    as a site user
    i need to be able to use contact form
    Feature: contact form
    Scenario: Successfuly sending
    feedback through a
    contact form
    Given I am registered user
    And I am on homepage
    When I follow “contact-us”
    And I fill in “text” with “hello, guys”
    And I send a feedback
    Then the feedback email should have been sent
    And I should see “message sent” inside notification area

    View Slide

  90. case #3
    Complex One

    View Slide


  91. I want students to optionally receive
    notifications about missed topics
    after failing tests.
    Your hypothetical client

    View Slide

  92. 1.Who?

    View Slide

  93. 1.Who?
    students

    View Slide

  94. 1.Who?
    students
    2.Why?

    View Slide

  95. 1.Who?
    students
    2.Why?
    to learn topics

    View Slide

  96. in order to fully learn topics
    as a student
    i need to receive notifications about badly learned topics
    Feature: topics notifications

    View Slide

  97. in order to fully learn topics
    as a student
    i need to receive notifications about badly learned topics
    Feature: topics notifications
    Scenario: Recieving notification
    about badly learned
    topics
    Given there is a test “test1” with questions:
    | topic | question | answers | correct |
    | math | 2+7 | 5, 8, 3, 9 | 9 |
    And I am authenticated student
    And I have activated notifications
    When I follow “test1”
    And I answer “5” for first question
    Then I should see “Test is not passed”
    And I should see “Learn math more”

    View Slide

  98. in order to fully learn topics
    as a student
    i need to receive notifications about badly learned topics
    Feature: topics notifications
    Scenario: Recieving notification
    about badly learned
    topics
    Given there is a test “test1” with questions:
    | topic | question | answers | correct |
    | math | 2+7 | 5, 8, 3, 9 | 9 |
    And I am authenticated student
    And I have activated notifications
    When I follow “test1”
    And I answer “5” for first question
    Then I should see “Test is not passed”
    And I should see “Learn math more”

    View Slide

  99. in order to fully learn topics
    as a student
    i need to receive notifications about badly learned topics
    Feature: topics notifications
    Scenario: Recieving notification
    about badly learned
    topics
    Given there are test “test1” with questions:
    | topic | question | answers | correct |
    | math | 2+7 | 5, 8, 3, 9 | 9 |
    And I am authenticated student
    And I have activated notifications
    When I follow “test1”
    And I answer “5” for first question
    Then I should see “Test is not passed”
    And I should see a “Learn math more”
    Later we’ve discovered, that this option should
    be available only for teacher accounts and
    should not be visible to students.

    View Slide

  100. 1.Who?

    View Slide

  101. 1.Who?
    teacher

    View Slide

  102. 1.Who?
    teacher
    2.Why?

    View Slide

  103. 1.Who?
    teacher
    2.Why?
    to help students

    View Slide

  104. in order to ensure students learn topics well
    as a teacher
    i need to be able to activate failed topics notifications
    Feature: topics notifications
    Scenario: Student with
    activated notifications
    should receive them.

    View Slide

  105. in order to ensure students learn topics well
    as a teacher
    i need to be able to activate failed topics notifications
    Feature: topics notifications
    Scenario: Student with
    activated notifications
    should receive them.
    Given there is a test “test1” with questions:
    | topic | question | answers | correct |
    | math | 2+7 | 5, 8, 3, 9 | 9 |
    And I am authenticated teacher
    And I have activated notifications for student “everzet”
    When student “everzet” fails test “test1”
    Then he should see “Learn math more”

    View Slide

  106. case #4
    Technical One

    View Slide

  107. in order to ensure that all emails have been sent
    as an technical administrator
    i need to be able to send all delayed emails through CLI
    Feature: sending delayed emails
    Scenario: Successfully sending
    delayed emails through
    CLI
    Given I am at the project root
    And there are 3 delayed emails in the project
    When I run “app/console project:delayed-emails:send”
    Then it should return 0
    And 3 emails should have been sent

    View Slide

  108. in order to ensure that all emails have been sent
    as an technical administrator
    i need to be able to send all delayed emails through CLI
    Feature: sending delayed emails
    Scenario: Successfully sending
    delayed emails through
    CLI
    Given I am at the project root
    And there are 3 delayed emails in the project
    When I run “app/console project:delayed-emails:send”
    Then it should return 0
    And 3 emails should have been sent
    ?

    View Slide

  109. in order to ensure that all emails have been sent
    as a technical administrator
    i need to be able to send all delayed emails through CLI
    Feature: sending delayed emails
    Scenario: Successfully sending
    delayed emails through
    CLI
    Given I am at the project root
    And there are 3 delayed emails in the project
    When I run “app/console project:delayed-emails:send”
    Then it should return 0
    And 3 emails should have been sent

    View Slide


  110. I think the real difference between
    mid-level and pro-level Behat users
    is the understanding of roles and
    their place in features.
    © Ryan Weaver

    View Slide

  111. Questions?

    View Slide


  112. I think the real difference between
    mid-level and pro-level Behat users
    is the understanding of roles and
    their place in features.
    © Ryan Weaver
    Awesome @weaverryan and my good
    friend has a wedding right now with
    amazing @leannapelhalm. Tweet
    them congrats right now!

    View Slide

  113. joind.in/6592

    View Slide