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

BDD For The Rest of Us - TarnovoConf 2017

BDD For The Rest of Us - TarnovoConf 2017

Boyan Yordanov

October 07, 2017
Tweet

More Decks by Boyan Yordanov

Other Decks in Programming

Transcript

  1. 4

  2. How are you testing? 1. Make a change 2. Switch

    to a browser / application / console 3. Click around to see the new thing 4. Check that nothing is broken? 6
  3. 7

  4. The computer is always faster than you * and doesn't

    make mistakes ** * almost always ** and not really 10
  5. 11

  6. 14

  7. Testing is important Figuring out what to test and how

    to test it is even more important 15
  8. -- Dan North Yes. If you’re a programmer, and your

    entire team is programmers, and all your stakeholders are programmers and you have a single Subject Matter Expert embedded in the team. dannorth.net/2012/05/31/bdd-is-like-tdd-if/ 17 . 3
  9. Think about the behaviour of the system Have conversations with

    the product owner / manager / client Together come up with examples of the correct behaviour Write the examples down and then write test code 17 . 4
  10. -- Liz Keogh Having conversations is more important than capturing

    conversations is more important than automating conversations 17 . 5
  11. Gherkin A Business Readable, Domain Speci c Language that lets

    you describe behaviour. behat.org/en/latest/user_guide/gherkin.html 18 . 1
  12. Feature: Generate reports for a conference Scenario: Report for the

    attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I generate a report Then it should have 150 rows And each row should contain the attendee's name 18 . 2
  13. A Gherkin feature: describes the behaviour to be added business

    people understand it as well is not concerned with the actual implementation bonus: can be used as documentation 18 . 3
  14. Meet the three amigos * the domain Expert the developer

    the tester * in an ideal world in which you have all of them on your team 21
  15. Writing a feature Feature: Generate reports for a conference Scenario:

    Report for the attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I generate a report Then it should have 150 rows And each row should contain the attendee's name 22 . 1
  16. Feature: Generate reports for a conference Scenario: Report for the

    attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I generate a report Then it should have 150 rows And each row should contain the attendee's name 22 . 2
  17. Feature: Generate reports for a conference Scenario: Report for the

    attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I generate a report Then it should have 150 rows And each row should contain the attendee's name 22 . 3
  18. Feature: Generate reports for a conference Scenario: Report for the

    attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I generate a report Then it should have 150 rows And each row should contain the attendee's name 22 . 4
  19. Thinking that BDD is only for End-To-End testing Feature: Generate

    reports for a conference Scenario: Report for the attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I go to "/reports/1" And I press the "Generate Attendees Report" button Then I should be on "/reports/1/attendees" And I should see "Attendees Report for VarnaConf" And I should see "Name" And I should see "Boyan" 23 . 2
  20. What's the problem here? too tied to the UI and

    web browser easy to break, hard to reuse does not describe the feature well enough 23 . 3
  21. Describing API endpoints this was actually good Feature: Generate reports

    for a conference Scenario: Report for the attendees at VarnaConf Given there is a conference "VarnaConf" And it has 150 attendees When I request GET "/reports/1/attendees" Then the response status should be 200 And the content type should be "application/json" And the response should have the structure: """ some json structure ... """ 23 . 4
  22. describes the request well enough still not reusable to drive

    just the domain possibly slow because of the required http request Today: just use API Bluepring or Swagger with a tool for contract testing 23 . 5
  23. Tools Java: JBehave PHP: Behat, PHPSpec Ruby: Cucumber, RSpec Python:

    Lettuce, Behave JavaScript: Cucumber.js, Yadda .NET: SpecFlow 25
  24. Behat Contexts Match Gherkin with actual test code Just PHP

    classes Can hook into Behat's lifecycle 27
  25. Create a context and use hooks use Behat\Behat\Context\Context; class FeatureContext

    implements Context { /** @BeforeScenario */ public function someSetupForTheScenario() { // set up fakes, db, browser etc. } } 28 . 1
  26. De ning Steps /** @Given there is a conference :conferenceName

    */ public function thereIsAConference($conferenceName) { $this->conference = new Conference("VarnaConf"); } 28 . 2
  27. /** @When I generete a report */ public function iGenerateReport()

    { $this->report = new Report($this->conference); } 28 . 3
  28. Making assertions use PHPUnit\Framework\Assert; // ... /** @Then it should

    have :expectedNumber rows */ public function itShouldHaveRows($expectedNumber) { Assert::assertCount( $expectedNumber, $this->report->getRows(), "The report does not have the expected number of rows." ) } 28 . 4
  29. It's all about the context class WebContext extends RawMinkContext {

    /** @When I generate a report */ public function iGenerateReport() { $browser = $this->getSession(); $browser->visit("/reports/{$this->conferenceId}"); $browser->pressButton("#showAttendeesReport"); } // ... } 29
  30. Resources Introducing BDD - Dan North BDD is like TDD

    if… - Dan North 10 years of Doing Behaviour-Driven Development All Wrong by Liz Keogh Getting out of end-to-end testing jail - Konstantin Kudryashov Driving Design through Examples - Ciaran McNulty https://dannorth.net/introducing-bdd/ https://dannorth.net/2012/05/31/bdd-is-like-tdd-if Part 1: https://www.youtube.com/watch?v=2EM4itu7j7I Part 2: https://www.youtube.com/watch?v=AFCdE5KSREI https://skillsmatter.com/skillscasts/9142-a-talk-by-konstantin-kudryashov https://www.youtube.com/watch?v=83GbyDpJDI4 31