Share
Embed
Transcript
48 slides
-
$> whoami Boyan Yordanov Twitter: @specter_bg Organizer of PHP Varna
Member of VarnaLab Developer @ ShtrakBG 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 -
Once we know there is a problem only then we
can x it. 8 -
The computer is always faster than you * and doesn't
make mistakes ** * almost always ** and not really 10 -
Testing is important Figuring out what to test and how
to test it is even more important 15 -
-- almost everyone But my PM/boss/client/cat thinks that all this
is a waste of time and money 16 -
-- 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 -
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 -
-- Liz Keogh Having conversations is more important than capturing
conversations is more important than automating conversations 17 . 5 -
Gherkin A Business Readable, Domain Speci c Language that lets
you describe behaviour. behat.org/en/latest/user_guide/gherkin.html 18 . 1 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
How are we going to get from plain English to
actual tests? 24 -
Tools Java: JBehave PHP: Behat, PHPSpec Ruby: Cucumber, RSpec Python:
Lettuce, Behave JavaScript: Cucumber.js, Yadda .NET: SpecFlow 25 -
Behat Contexts Match Gherkin with actual test code Just PHP
classes Can hook into Behat's lifecycle 27 -
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 -
De ning Steps /** @Given there is a conference :conferenceName
*/ public function thereIsAConference($conferenceName) { $this->conference = new Conference("VarnaConf"); } 28 . 2 -
/** @When I generete a report */ public function iGenerateReport()
{ $this->report = new Report($this->conference); } 28 . 3 -
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 -
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 -
-- Uncle Bob "The only way to go fast is
to go well." 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