BDD For The Rest of Us - TarnovoConf 2017

BDD For The Rest of Us - TarnovoConf 2017

807dcecdfca0e4078d89127cd440c039?s=128

Boyan Yordanov

October 07, 2017
Tweet

Transcript

  1. BDD For The Rest of Us 1

  2. $> whoami Boyan Yordanov Twitter: @specter_bg Organizer of PHP Varna

    Member of VarnaLab Developer @ ShtrakBG 2
  3. Confession Time 3

  4. 4

  5. ... "Testing is critical for quality" 5

  6. 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
  7. 7

  8. Once we know there is a problem only then we

    can x it. 8
  9. What can we do about it? We can automate 9

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

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

  12. Types of Automated Testing Unit Test Integration Tests Acceptance (End-To-End)

    Tests 12
  13. Which type to use? All of them 13

  14. 14

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

    to test it is even more important 15
  16. -- almost everyone But my PM/boss/client/cat thinks that all this

    is a waste of time and money 16
  17. Behaviour Driven Development 17 . 1

  18. Isn't BDD the same as TDD? 17 . 2

  19. -- 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
  20. 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
  21. -- Liz Keogh Having conversations is more important than capturing

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

    you describe behaviour. behat.org/en/latest/user_guide/gherkin.html 18 . 1
  23. 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
  24. 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
  25. It's so cool, the bussiness people can write it too!

    19
  26. So who should write the features? 20

  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. Common pitfals not completely wrong, just not as good 23

    . 1
  33. 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
  34. 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
  35. 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
  36. 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
  37. How are we going to get from plain English to

    actual tests? 24
  38. Tools Java: JBehave PHP: Behat, PHPSpec Ruby: Cucumber, RSpec Python:

    Lettuce, Behave JavaScript: Cucumber.js, Yadda .NET: SpecFlow 25
  39. Behat "A PHP framework for autotesting your bussiness expectations" behat.org

    26
  40. Behat Contexts Match Gherkin with actual test code Just PHP

    classes Can hook into Behat's lifecycle 27
  41. 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
  42. De ning Steps /** @Given there is a conference :conferenceName

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

    { $this->report = new Report($this->conference); } 28 . 3
  44. 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
  45. 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
  46. -- Uncle Bob "The only way to go fast is

    to go well." 30
  47. 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
  48. Thank You http://bit.ly/tarnovoconf-17-bdd 32