bdd for apis
DESCRIPTION
Supporting a talk by @jasonh_n_austin given to the @austinapi meetup on 9/25/13.. The usability of APIs has to be a forethought from product down to test. We must ensure a developer-focused mentality throughout the development lifecycle. BDD provides us with an elegant way to implement a plain English, plain text testing fixture, in a variety of languages.TRANSCRIPT
![Page 1: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/1.jpg)
BDD for APIs
Jason Harmon
![Page 2: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/2.jpg)
Jason Harmon• Blogger on apiux.com• API Architect @uShip
• Likes Python• Works in everything else
• Funny mustache• Loves weiner dogs
![Page 3: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/3.jpg)
Some things to keep in mind…About 80s movies/tv
and software
![Page 4: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/4.jpg)
Humans > Robots
Terminator taught us…
![Page 5: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/5.jpg)
Agile
• INDIVIDUALS AND INTERACTIONS over processes and tools–Inside our organization and with our
users
![Page 6: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/6.jpg)
Automationcan
be ourfriend too
Johnny Fivetaught us…
![Page 7: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/7.jpg)
Agile
• WORKING SOFTWARE over comprehensive documentation–Not only in our product but our
testing & regression
![Page 8: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/8.jpg)
Sometimesthere isno timeto plan
Always havea plan
![Page 9: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/9.jpg)
Agile
• RESPONDING TO CHANGE over following a plan– APIs will change for your business,
better have tests
![Page 10: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/10.jpg)
Work together!
![Page 11: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/11.jpg)
Agile
• CUSTOMER COLLABORATION over contract negotiation– Get good at sharing ideas
![Page 12: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/12.jpg)
What is BDD?
• “Behavior Driven Development”• English readable description of how our product
behaves• Agreed upon by the team: not Dev, QA or
product owned• Used to verify the acceptance criteria for
features in a product• Conceptually like TDD (test-first), but for
functional testing (not code architecture)
![Page 13: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/13.jpg)
* Agile Testing: A Practical Guide for Testers and Agile Teams (Addison-Wesley, 2009)
http://lisacrispin.com/2011/11/08/using-the-agile-testing-quadrants/
![Page 14: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/14.jpg)
Q2 Automated & Manual Tests
Functional TestsExamplesStory TestsPrototypesSimulations
Supp
ortin
g th
e Te
amBusiness Facing
BDD
![Page 15: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/15.jpg)
Polyglot friendly
• Cucumber– Ruby-based, the granddaddy of BDD
• Cucumber-JVM– Java implementation, plays nice with Junit and all JVM
languages (Groovy is quite nice here)• Specflow
– .NET, Visual Studio integrated• Lettuce
– Python-based, very similar to Cucumber• Behat
– PHP-based
![Page 16: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/16.jpg)
GherkinOne Description to Rule Them All
• https://github.com/cucumber/gherkin • Business Readable• DSL• Not code
![Page 17: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/17.jpg)
![Page 18: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/18.jpg)
Gherkin ExampleImperative Style
Scenario: Get nearby place nameGiven I use the geonames hostWhen I access the resource url
"/findNearbyPlaceNameJSON" And I provide parameter "username" as "jharmon" And I provide parameter "lat" as "30.4754724" And I provide parameter "lng" as "-98.1564068"
And I retrieve the JSON resultsThen the status code should be 200And it should have a list "geonames"And the list should have at least 1 item
![Page 19: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/19.jpg)
Gherkin ExampleDeclarative Style
Scenario: Get nearby place nameGiven I use the geonames hostWhen I use the “Find Nearby Place by JSON” endpoint
And I identify my “geonames” user credentials And I provide coordinates 30.4754724, -98.1564068
And I retrieve the JSON resultsThen it should have at least 1 valid "geonames" item
![Page 20: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/20.jpg)
Behavior Driven
• User experience trumps all• API is no exception• Treat API consumers like first class citizens• Describe how your API behaves from a
consumer perspective
![Page 21: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/21.jpg)
![Page 22: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/22.jpg)
Step Definitions
![Page 23: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/23.jpg)
Scenarios“…until it’s green like a cuke”
![Page 24: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/24.jpg)
Scenario outlines
![Page 25: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/25.jpg)
ErrorsRed means stop
![Page 26: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/26.jpg)
Tags
Lots of power to selectively run groups of tests
![Page 27: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/27.jpg)
DEVE
LOPE
RS
![Page 28: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/28.jpg)
TEST
ERS
![Page 29: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/29.jpg)
STAK
EHOL
DER
S
![Page 30: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/30.jpg)
PROD
UCT
![Page 31: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/31.jpg)
![Page 32: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/32.jpg)
Agreement
• Decide during planning how you will test for Acceptance Criteria
• …AS A TEAM…• Gherkin/feature definitions will allow a
ubiquitous language• Technically, this can get easier as the critical
mass of your API interactions is in your testing framework, and scenario steps are predictable
![Page 33: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/33.jpg)
Productivity
• Stakeholders know what they’re getting• Devs know how to build it• Testers know how to test it• Product knows how to sign off• Documentation has a head start
![Page 34: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/34.jpg)
Prod
uct
Dev
elop
ers
Test
ers
Stak
ehol
ders
Real world silos teach us…
![Page 35: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/35.jpg)
Humans die inside silos!!!
![Page 36: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/36.jpg)
Domain Language
• Focus on how you describe everything• Find agreement on how to describe behavior• Recognize when behavior is different than
before– “Wait, we’ve never deleted before!”
• Homework: Domain Driven Design
![Page 37: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/37.jpg)
![Page 38: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/38.jpg)
Share!
• All BDD tests can be committed to SCM, including branches
• Provide process which allows collaborative coding on building test framework
• Devs provide expertise on building a scalable test framework
• Testers utilize existing step definitions to quickly build out tests
![Page 39: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/39.jpg)
Continuous Integration
• Regression is running all the time• Use tags to select the right depth of regression• Very fast identification of what’s broken
![Page 40: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/40.jpg)
![Page 41: BDD for APIs](https://reader035.vdocuments.us/reader035/viewer/2022062513/555127fcb4c905b3598b487e/html5/thumbnails/41.jpg)
Samples
• Python/Lettuce– https://github.com/jasonh-n-austin/api-bdd-tests
• Groovy/Cucumber-JVM– https://github.com/jasonh-n-austin/TwitterRestTests
• .NET/SpecFlow– https://github.com/jasonh-n-austin/TwitterRestTests