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.
5. 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
12. Test Architecture
• Integration tests => different test project
• Use your own client sdk
• Functional & Unit tests => main test project
• Debian packages
• Mock only external services
13. 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
15. API
Business
Logic
Task
Management
ORM
Message
Queue
No9fica9on
System
Service
API
Models
Database
API
Asynchronous Architecture
Clients
External
Services
16. 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
19. "TDD doesn't drive good design.
TDD gives you immediate feedback
about what is likely to be bad
design."
- Kent Beck
20. 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
21. 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
22. Thank you
and use TDD for your APIs
Michael Kuehne
michael@mkuehne.com