Slide 1

Slide 1 text

TDD for APIs Michael Kuehne

Slide 2

Slide 2 text

Michael Kuehne •  Hamburg, Germany •  Independent Software Engineer •  Likes Python & APIs •  Clients projects mostly API- related

Slide 3

Slide 3 text

"How can we validate that our API is working as intended?“ - Steve Klabnik

Slide 4

Slide 4 text

TDD Cycle Inside-Out Write a failing Unit Test Make the Test pass Refactor

Slide 5

Slide 5 text

API   Business  Logic   Task  Management   ORM   Message  Queue   No9fica9on   System   Mobile  Device   Single  Page  App   Service   API   Models   Database   API   Example Architecture Clients   External   Services  

Slide 6

Slide 6 text

Defined  Input   POST /mailboxes HTTP/1.1 Host: http://api.mkuehne.com/ Content-Type: application/json { ‘username’: ‘michael’ ‘mail_address’: ‘[email protected]’ } Defined  Output   HTTP/1.1 201 CREATED Date: Wed, 22 Jul 2015 11:45 Content-Type: application/hal +json Location: /mailboxes/1a3b3s24bd

Slide 7

Slide 7 text

„Unit Tests confirm we have written the code right“ – Unknown „Acceptance Tests confirm that we built the right software.“ – Ian Cooper

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Acceptance Tests on all Levels

Slide 10

Slide 10 text

BDD Cycle Outside Inside Write failing Acceptance Tests Write failing Unit Tests Make Tests pass Refactor

Slide 11

Slide 11 text

Numbers from Clients Project •  ~3.000 Tests •  ~2.000 Unit Tests •  ~1.000 Acceptance Tests •  > 300 PyTest Fixtures

Slide 12

Slide 12 text

Test Architecture •  Integration tests => different test project •  Use your own client sdk •  Functional & Unit tests => main test project •  Debian packages •  Mock only external services

Slide 13

Slide 13 text

How to design API tests? •  Always test to the end •  F.I.R.S.T. Principle •  Reusable helper classes •  Deterministic test environment •  PyTest: Fixtures, fixtures, fixtures §  Clean database & ORM §  Test data §  Versioned Endpoints

Slide 14

Slide 14 text

Testing Versioned endpoints with Django Rest Framework and PyTest

Slide 15

Slide 15 text

API   Business  Logic   Task  Management   ORM   Message  Queue   No9fica9on   System   Service   API   Models   Database   API   Asynchronous Architecture Clients   External   Services  

Slide 16

Slide 16 text

Tests for asynchronous behaviors •  Celery (task management) caches! •  Validate the input parameters synchronous •  Test Deltas (validation processing) •  Make aync results accessable •  Wait for all async tasks in each test

Slide 17

Slide 17 text

Wait for all task in celery_taskstate

Slide 18

Slide 18 text

Async Testing: Outside to Inside API   Business  Logic   Celery  Task   Task  without  Celery  

Slide 19

Slide 19 text

"TDD doesn't drive good design. TDD gives you immediate feedback about what is likely to be bad design." - Kent Beck

Slide 20

Slide 20 text

How can TDD help improve API design or avoid bad design? •  Inverse Conway Maneuver •  API design evolves from the outside to inside •  Refactoring without changing behavior •  Endpoint Versioning with confidence

Slide 21

Slide 21 text

Lessons learned •  Great API specifications required •  Best API design come from outside the team •  Don‘t forget your versioned Endpoints •  Don‘t underestimated the test effort for API with asynchronous behaviors •  Complexity vs. deterministic environments

Slide 22

Slide 22 text

Thank you and use TDD for your APIs Michael Kuehne [email protected]