Manuals / API Testing / Ch 7

C · Automation & CIIntermediate70 min read

7. Code-based API automation

API Testing · 44 pages source format

Postman scales to a point; code scales further. Automate with Python requests, JavaScript fetch/axios, or Playwright APIRequestContext — pick one stack.

What you'll learn

  • requests / fetch patterns
  • pytest or Jest structure
  • Setup and teardown
  • Assertions on JSON

Pick your stack

Python: requests + pytest. JS: fetch + Jest/Vitest. Playwright: request fixture for hybrid UI+API teams.

# Python + pytest + requests
import requests

def test_get_user():
    r = requests.get("https://jsonplaceholder.typicode.com/users/1")
    assert r.status_code == 200
    assert "@" in r.json()["email"]

Do this now

Install stack. One test: GET users/1, assert status 200 and email contains @.

Clear?

CRUD test module

test_create_post, test_get_post, test_update_post, test_delete_post — isolated, order-independent.

Do this now

Automate full CRUD for /posts. Use unique title per run.

Clear?

Fixtures for base URL and session

conftest.py or beforeAll sets base_url and auth headers once.

Do this now

Extract BASE_URL from env var. Document in README.

Clear?

When Postman vs code

Postman: exploration, manual QA, quick sharing. Code: CI-native, version control, complex logic, hybrid suites.

Do this now

Write TOOLING.md: when your team uses Postman vs code (2 paragraphs).

Clear?

Checklist