TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
-
Upload
michael-kuehne -
Category
Software
-
view
405 -
download
1
Transcript of TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
Michael Kuehne • Hamburg, Germany • Independent Software
Engineer • Likes Python & APIs • Clients projects mostly API-
related
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
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
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
"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