Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TDD for APIs @ Europython 2015 by Michael Kuehne

TDD for APIs @ Europython 2015 by Michael Kuehne

It is always tough to test a complex API comprehensively. The additional level of complexity brings us to the question “How can we validate that our API is working as intended?”

In this talk I will explain how to use test driven development for APIs to solve this problem and even further how TDD can drive an API Design towards a more usable design. I will outline my practical approach with an implementation example based on django. And finally I will give you a brief summary of my lessons learned using this approach in customer projects.

More Decks by Michael Kuehne-Schlinkert

Other Decks in Programming

Transcript

  1. Michael Kuehne •  Hamburg, Germany •  Independent Software Engineer • 

    Likes Python & APIs •  Clients projects mostly API- related
  2. 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  
  3. 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
  4. „Unit Tests confirm we have written the code right“ –

    Unknown „Acceptance Tests confirm that we built the right software.“ – Ian Cooper
  5. Numbers from Clients Project •  ~3.000 Tests •  ~2.000 Unit

    Tests •  ~1.000 Acceptance Tests •  > 300 PyTest Fixtures
  6. Test Architecture •  Integration tests => different test project • 

    Use your own client sdk •  Functional & Unit tests => main test project •  Debian packages •  Mock only external services
  7. 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
  8. API   Business  Logic   Task  Management   ORM  

    Message  Queue   No9fica9on   System   Service   API   Models   Database   API   Asynchronous Architecture Clients   External   Services  
  9. 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
  10. Async Testing: Outside to Inside API   Business  Logic  

    Celery  Task   Task  without  Celery  
  11. "TDD doesn't drive good design. TDD gives you immediate feedback

    about what is likely to be bad design." - Kent Beck
  12. 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
  13. 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