Code Reference

API Client Test

Pytest · Example / how-to

Pytest · Example / how-to


📋 Overview

CRUD smoke for a JSON API with requests and unique ids so parallel runs stay isolated.

🔧 Core concepts

StepCheck
POST201 + id
GETSame fields
PATCHChanged field
DELETE204 / gone

💡 Examples

import os
import uuid
import requests
import pytest

BASE = os.getenv("API_URL", "http://127.0.0.1:8000")

@pytest.fixture
def item_payload():
    return {"title": f"note-{uuid.uuid4().hex[:8]}", "done": False}

def test_item_crud(item_payload):
    created = requests.post(f"{BASE}/api/items", json=item_payload, timeout=5)
    assert created.status_code in (200, 201)
    item_id = created.json()["id"]

    got = requests.get(f"{BASE}/api/items/{item_id}", timeout=5)
    assert got.status_code == 200
    assert got.json()["title"] == item_payload["title"]

    patched = requests.patch(
        f"{BASE}/api/items/{item_id}",
        json={"done": True},
        timeout=5,
    )
    assert patched.status_code == 200
    assert patched.json()["done"] is True

    deleted = requests.delete(f"{BASE}/api/items/{item_id}", timeout=5)
    assert deleted.status_code in (200, 204)

    missing = requests.get(f"{BASE}/api/items/{item_id}", timeout=5)
    assert missing.status_code == 404

⚠️ Pitfalls

  • Parallel runs need unique titles/ids (uuid).
  • Teardown if mid-test fails — use try/finally or yield fixtures.

On this page