BDD for APIs

42
BDD for APIs Jason Harmon

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 of BDD for APIs

Page 1: BDD for APIs

BDD for APIs

Jason Harmon

Page 2: BDD for APIs

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

Some things to keep in mind…About 80s movies/tv

and software

Page 4: BDD for APIs

Humans > Robots

Terminator taught us…

Page 5: BDD for APIs

Agile

• INDIVIDUALS AND INTERACTIONS over processes and tools–Inside our organization and with our

users

Page 6: BDD for APIs

Automationcan

be ourfriend too

Johnny Fivetaught us…

Page 7: BDD for APIs

Agile

• WORKING SOFTWARE over comprehensive documentation–Not only in our product but our

testing & regression

Page 8: BDD for APIs

Sometimesthere isno timeto plan

Always havea plan

Page 9: BDD for APIs

Agile

• RESPONDING TO CHANGE over following a plan– APIs will change for your business,

better have tests

Page 10: BDD for APIs

Work together!

Page 11: BDD for APIs

Agile

• CUSTOMER COLLABORATION over contract negotiation– Get good at sharing ideas

Page 12: BDD for APIs

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

* 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

Q2 Automated & Manual Tests

Functional TestsExamplesStory TestsPrototypesSimulations

Supp

ortin

g th

e Te

amBusiness Facing

BDD

Page 15: BDD for APIs

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

GherkinOne Description to Rule Them All

• https://github.com/cucumber/gherkin • Business Readable• DSL• Not code

Page 17: BDD for APIs
Page 18: BDD for APIs

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

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

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
Page 22: BDD for APIs

Step Definitions

Page 23: BDD for APIs

Scenarios“…until it’s green like a cuke”

Page 24: BDD for APIs

Scenario outlines

Page 25: BDD for APIs

ErrorsRed means stop

Page 26: BDD for APIs

Tags

Lots of power to selectively run groups of tests

Page 27: BDD for APIs

DEVE

LOPE

RS

Page 28: BDD for APIs

TEST

ERS

Page 29: BDD for APIs

STAK

EHOL

DER

S

Page 30: BDD for APIs

PROD

UCT

Page 31: BDD for APIs
Page 32: BDD for APIs

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

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

Prod

uct

Dev

elop

ers

Test

ers

Stak

ehol

ders

Real world silos teach us…

Page 35: BDD for APIs

Humans die inside silos!!!

Page 36: BDD for APIs

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
Page 38: BDD for APIs

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

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
Page 42: BDD for APIs

Thank YouJason Harmon

@jasonh_n_austinhttp://jhr.mn