API Testing Fundamentals
What is API testing?
Section titled “What is API testing?”API testing validates a service at the HTTP interface level:
- requests and responses
- status codes and headers
- payload correctness (JSON shape)
- authentication/authorization
- error handling and rate limits
What to test (checklist)
Section titled “What to test (checklist)”- Status codes: 200/201/204, 400/401/403/404, 429, 5xx
- Response body: required keys, value types, constraints
- Contracts: backwards compatibility
- Auth: missing token, invalid token, role permissions
- Idempotency: repeated requests behave correctly
- Performance: basic response time budgets
Diagram: test levels around an API
Section titled “Diagram: test levels around an API”graph TD A["Unit tests\n(request builders, validators)"] --> B["API tests\n(HTTP-level)"] B --> C["End-to-end tests\n(user workflows)"]
- Write API tests as deterministic as possible.
- Avoid relying on external services in CI.
Visualize it
Section titled “Visualize it”An API test builds a request, sends it to the endpoint, and checks the response before reporting pass or fail.
flowchart LR A["Build request"] --> B["Send to endpoint"] B --> C["Receive response"] C --> D["Assert status code + body"] D --> E["Pass"] D --> F["Fail"]
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Write a unittest TestCase
Section titled “Exercise 1 – Write a unittest TestCase”Exercise 2 – assertRaises
Section titled “Exercise 2 – assertRaises”Exercise 3 – setUp and tearDown
Section titled “Exercise 3 – setUp and tearDown”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading