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

F742011db78e0c681ed4bc885bfee982?s=128

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!"#$
  2. @everzet Konstantin Kudryashov

  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
  4. Behavior Driven Development

  5. Behavior Driven Development 5.3+

  6. None
  7. 1st commit 2010-06-30

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

  9. Some projects already using

  10. Some projects thinking just

  11. Some users comparing are

  12. Some comparing are users

  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
  14. Is Behat awesome Functional testing Framework ?

  15. You

  16. are

  17. Wrong!

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

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

  20. NO, it s NOT

  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.
  22. Some applications could easily fullfill developers expectations and still not

    being able to cover business needs.
  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.
  24. Exploration OF Domain area

  25. 3 levels of domain exploration

  26. 1 2 3 narrative possible scenarios scenario details

  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
  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
  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
  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
  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 =>
  32. StoryBDD helps ensuring that development team has understanding of business

    on the same level that client does
  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
  34. How?

  35. by Forcing you business questions to answer

  36. 1. For who?

  37. 1. For who? 2. What?

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

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

  40. ‘NuFF SAID

  41. case #1 About Registration

  42. ” I want visitors to be able to register on

    my website through simple registration form. Your hypothetical client
  43. 1.Who?

  44. 1.Who? users?

  45. 1.Who? users visitors

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

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

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

  49. Know your client clients!

  50. in order to maintain my shopping history as a site

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

    visitor i need to be able to register on this site Feature: registration
  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)
  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)
  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
  55. Use gained knowledge!

  56. in order to maintain my shopping history as a site

    visitor i need to be able to register on this site Feature: registration
  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
  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
  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: ...
  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: ...
  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 “linkedin@example.com” 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 “/”
  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 “linkedin@example.com” 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 “/”
  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 “linkedin@example.com” 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 “/” ?
  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 “linkedin@example.com” 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
  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 “linkedin@example.com” 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
  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 “linkedin@example.com” 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
  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 “linkedin@example.com” 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
  68. case #2 About Sending Emails

  69. ” I want my users to be able to send

    feedback messages through contact form. Your hypothetical client
  70. 1.Who?

  71. 1.Who? users

  72. 1.Who? users 2.Why?

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

  74. in order to give feedback as a site user i

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

    need to be able to use contact form Feature: contact form
  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
  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 “linkedin@example.com” 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”
  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 “linkedin@example.com” 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
  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 “linkedin@example.com” 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”
  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 “linkedin@example.com” 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”
  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 “linkedin@example.com” 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”
  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”
  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”
  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”
  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!
  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
  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
  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
  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
  90. case #3 Complex One

  91. ” I want students to optionally receive notifications about missed

    topics after failing tests. Your hypothetical client
  92. 1.Who?

  93. 1.Who? students

  94. 1.Who? students 2.Why?

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

  96. in order to fully learn topics as a student i

    need to receive notifications about badly learned topics Feature: topics notifications
  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”
  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”
  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.
  100. 1.Who?

  101. 1.Who? teacher

  102. 1.Who? teacher 2.Why?

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

  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.
  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”
  106. case #4 Technical One

  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
  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 ?
  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
  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
  111. Questions?

  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!
  113. joind.in/6592