TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne

22
TDD for APIs Michael Kuehne

Transcript of TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne

TDD for APIs Michael Kuehne

Michael Kuehne •  Hamburg, Germany •  Independent Software

Engineer •  Likes Python & APIs •  Clients projects mostly API-

related

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

TDD Cycle

Inside-Out

Write a failing

Unit Test

Make the Test

pass

Refactor

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  

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

 

„Unit Tests confirm we have written the code right“ – Unknown

„Acceptance Tests confirm that we

built the right software.“ – Ian Cooper

Acceptance Tests on all Levels

BDD Cycle

Outside Inside

Write failing Acceptance

Tests

Write failing

Unit Tests

Make Tests pass

Refactor

Numbers from Clients Project

•  ~3.000 Tests •  ~2.000 Unit Tests •  ~1.000 Acceptance Tests •  > 300 PyTest Fixtures

Test Architecture

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

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

Testing Versioned endpoints with Django Rest Framework and PyTest

API  

Business  Logic  

Task  Management   ORM  

Message  Queue  

No9fica9on  System

 

Service  

API  

Models  Database  

API  

Asynchronous Architecture

Clients  

External  Services  

Tests for asynchronous behaviors

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

Wait for all task in celery_taskstate

Async Testing: Outside to Inside

API  

Business  Logic  

Celery  Task  

Task  without  Celery  

"TDD doesn't drive good design. TDD gives you immediate feedback

about what is likely to be bad design."

- Kent Beck

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

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

Thank you and use TDD for your APIs

Michael Kuehne [email protected]