Better API testing with the OpenAPI Specification

A presentation at OSCON in July 2018 in Portland, OR, USA by Taylor Barnett

Slide 1

Slide 1

BETTER API TESTING WITH OPENAPI SPECIFICATION TAYLOR BARNETT — @TAYLOR_ATX

Slide 2

Slide 2

LEAD COMMUNITY ENGINEER @ STOPLIGHT TAYLOR@STOPLIGHT.IO @TAYLOR_ATX HI! 👋

Slide 3

Slide 3

Slide 4

Slide 4

[COMPANY] [BLEEPING]

Slide 5

Slide 5

@taylor_atx

Slide 6

Slide 6

Image by Paul Downey, CC by 2.0

Slide 7

Slide 7

SINGLE SOURCE OF TRUTH ☝ @taylor_atx

Slide 8

Slide 8

SINGLE SOURCE OF TRUTH ▸ What does everything (humans and machines) look to? ▸ Messy, outdated design docs? ▸ Something that everyone agrees to - a contract @taylor_atx

Slide 9

Slide 9

OPENAPI SPECIFICATION @taylor_atx

Slide 10

Slide 10

Standard, structured approach for describing APIs that is both human and machine readable @taylor_atx

Slide 11

Slide 11

Image by OpenAPI Initiative

Slide 12

Slide 12

OPENAPI SPECIFICATION ▸ Development contract ▸ Prototyping and mocking ▸ Client SDKs and libraries ▸ Testing ▸ Server stubs @taylor_atx

Slide 13

Slide 13

OPENAPI SPECIFICATION ▸ Simplifies describing an API ▸ Standardizes terminology ▸ “API Fastness” @taylor_atx

Slide 14

Slide 14

OPENAPI SPECIFICATION ▸ Good tutorial to walk through the specification: ▸ https://apihandyman.io/openapi/ @taylor_atx

Slide 15

Slide 15

“OPENAPI IS A BRIDGE TO UNDERSTANDING AND BEING ABLE TO COMMUNICATE AROUND USING HTTP AS A TRANSPORT, AND OUR GREATEST HOPE FOR HELPING [PEOPLE] LEARN THEIR HTTPS AND 123S.” Kin Lane (@apievangelist) @taylor_atx

Slide 16

Slide 16

DESIGN-FRIST APIS ✍ @taylor_atx

Slide 17

Slide 17

DESIGN-FIRST APIS ▸ Consistency for users ▸ Important for critical APIs ▸ Code-first can be expensive ▸ Gain the benefits of using OpenAPI @taylor_atx

Slide 18

Slide 18

CONTRACT TESTING @taylor_atx

Slide 19

Slide 19

Tests that ensure the API implementation meets the standards and definitions described in a contract @taylor_atx

Slide 20

Slide 20

CONTRACT TESTING ▸ Popularity has increased with the growth of microservices ▸ Some kind of contract available to all parties ▸ Enables independent deployments while not breaking other people’s code ▸ What it is not testing: ▸ Service availability ▸ Load tolerance ▸ Deployment integrity @taylor_atx

Slide 21

Slide 21

OPENAPI SPECIFICATION API RESPONSE VALIDATION SERVER PASS OR FAIL @taylor_atx

Slide 22

Slide 22

BUT I ALREADY TEST MY APIS… 🤔 @taylor_atx

Slide 23

Slide 23

(SOME) DIFFERENT TYPES OF TESTING ▸ Integration/Functional Tests ▸ Unit Tests ▸ End-to-end Tests @taylor_atx

Slide 24

Slide 24

(SOME) DIFFERENT TYPES OF TESTING UI TESTING CONTRACT TESTING INTEGRATION TESTING UNIT TESTING @taylor_atx

Slide 25

Slide 25

EXAMPLE: BROKEN SCHEMA 💔 @taylor_atx

Slide 26

Slide 26

curl —request GET —url http://api.taylorbar.net/hello-world 200 👍 @taylor_atx

Slide 27

Slide 27

@taylor_atx

Slide 28

Slide 28

Test Greetings Scenario (0.009s) Hello World, GET http://api.taylorbar.net/hello-world (0.009s) ✗ hello: hello is required ✓ ✗ output.status (eq) 2xx 1 of 2 tests failed 😔 @taylor_atx

Slide 29

Slide 29

API SPEC { “ello”: “world” } @taylor_atx

Slide 30

Slide 30

Test Greetings Scenario (0.012s) Hello World, GET http://api.taylorbar.net/hello-world (0.011s) ✓ output.body (validate.contract) against JSON Schema ✓ output.status (eq) 2xx ✓ 2 tests passed 😁 @taylor_atx

Slide 31

Slide 31

Todo CRUD Scenario (0.831s) Create Todo, POST http://todos.stoplight.io/todos?apikey=123 (0.244s) ✗ user: user is required (root): Must validate all the schemas (allOf) ✓ output.status (eq) 201 Get Todo, GET http://todos.stoplight.io/todos/61904 (0.13s) ✗ user: user is required (root): Must validate all the schemas (allOf) ✓ output.status (eq) 200 Update Todo, PUT http://todos.stoplight.io/todos/61904?apikey=123 (0.137s) ✗ user: user is required (root): Must validate all the schemas (allOf) ✓ output.status (eq) 200 Delete Todo, DELETE http://todos.stoplight.io/todos/61904?apikey=123 (0.126s) ✓ output.status (eq) 204 ✓ output.body (validate.contract) against JSON Schema Make Sure Deleted, GET http://todos.stoplight.io/todos/61904 (0.192s) ✓ output.status (eq) 404 ✓ output.body (validate.contract) against JSON Schema ✗ 3 of 10 tests failed 😔 @taylor_atx

Slide 32

Slide 32

API SPEC { “completed”: null, “completed_at”: null, “created_at”: “2018-07-18T18:33:19.341Z”, “id”: 61907, “name”: “feed Yoda”, “updated_at”: “2018-07-18T18:33:19.341Z” } @taylor_atx

Slide 33

Slide 33

Todo CRUD Scenario (0.902s) Create Todo, POST http://todos.stoplight.io/todos?apikey=123 (0.277s) ✓ output.status (eq) 201 ✓ output.body (validate.contract) against JSON Schema Get Todo, GET http://todos.stoplight.io/todos/61908 (0.141s) ✓ output.status (eq) 200 ✓ output.body (validate.contract) against JSON Schema Update Todo, PUT http://todos.stoplight.io/todos/61908?apikey=123 (0.145s) ✓ output.status (eq) 200 ✓ output.body (validate.contract) against JSON Schema Delete Todo, DELETE http://todos.stoplight.io/todos/61908?apikey=123 (0.129s) ✓ output.status (eq) 204 ✓ output.body (validate.contract) against JSON Schema Make Sure Deleted, GET http://todos.stoplight.io/todos/61908 (0.209s) ✓ output.status (eq) 404 ✓ output.body (validate.contract) against JSON Schema ✓ 10 tests passed 😁 @taylor_atx

Slide 34

Slide 34

EXAMPLE: SDKS 🔥 @taylor_atx

Slide 35

Slide 35

@taylor_atx

Slide 36

Slide 36

ADD CONTRACT TESTING INTO CI/CD PIPELINE @taylor_atx

Slide 37

Slide 37

SDKS ▸ Helps maintainers ▸ Ensures API and SDK/libraries parity ▸ Go see Elmer Thomas’ talk at 5:05pm on “Managing SDKs and their communities in multiple programming languages” @taylor_atx

Slide 38

Slide 38

EXAMPLE: DOCS 📄 @taylor_atx

Slide 39

Slide 39

@taylor_atx

Slide 40

Slide 40

WHAT ABOUT PACT TESTING? @taylor_atx

Slide 41

Slide 41

SINGLE SOURCE OF TRUTH PITFALLS @taylor_atx

Slide 42

Slide 42

SINGLE SOURCE OF TRUTH PITFALLS ▸ Forgetting the “why” ▸ Failing to version ▸ “version” is in the Info Object ▸ Bad communication @taylor_atx

Slide 43

Slide 43

COMMUNICATION @taylor_atx

Slide 44

Slide 44

@taylor_atx

Slide 45

Slide 45

@taylor_atx

Slide 46

Slide 46

THEY BECOME THE CODIFICATION OF A SET OF DISCUSSIONS ABOUT WHAT A SERVICE API SHOULD LOOK LIKE, AND WHEN THEY BREAK, THEY BECOME A TRIGGER POINT TO HAVE CONVERSATIONS ABOUT HOW THAT API SHOULD EVOLVE. Sam Newman, “Building Microservices: Testing” @taylor_atx

Slide 47

Slide 47

TAYLOR@STOPLIGHT.IO @TAYLOR_ATX