Layering a Test Suite: Unit, API, End-to-End, and AI Evals
One of the most persistent mistakes I see is treating all tests as interchangeable. Teams write only end-to-end tests because they feel real, or only unit tests because they're fast, then wonder why the suite misses production bugs or grinds CI to a halt. The fix isn't picking the right layer. It's knowing what each layer is responsible for.
The testing pyramid is the usual picture: many unit tests, a solid middle of API tests, a few end-to-end tests on top. The shape reflects cost: how long a test takes, how often it breaks for reasons unrelated to your code, and how much setup it needs. That logic still holds. What's changed is who writes the tests. When Copilot or a coding agent can produce dozens of tests in a minute, the risk isn't too few tests. It's lots of tests at the wrong layer: mocked unit tests that duplicate each other, or slow end-to-end checks for things an API test would catch in milliseconds.
In this article I'll walk through what belongs in each layer, how I tell agents where their tests go, and how the same layering applies when the thing under test calls a language model.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Unit tests: logic in isolation
Unit tests cover the smallest pieces of your code with no network, no database and no file system. In an API test project that means request-building logic, response parsers, retry helpers and validation functions:
# utils/response_parser.py
def extract_user_ids(response_json):
return [item["id"] for item in response_json.get("users", [])]
# tests/unit/test_response_parser.py
from utils.response_parser import extract_user_ids
def test_extracts_ids_from_valid_payload():
assert extract_user_ids({"users": [{"id": 1}, {"id": 2}]}) == [1, 2]
def test_returns_empty_list_when_users_key_missing():
assert extract_user_ids({}) == []
These run in milliseconds and never fail because a service is down, so people actually run them on every save. Where they stop is the boundary with the outside world. A mocked HTTP response only proves your code handles the shape you imagined. It says nothing about whether the real API returns that shape. A useful warning sign: if a mock is more complex than the code it replaces, the question belongs in the next layer.
The API layer: where integration failures live
This is where I spend most of my time and where most teams underinvest. API tests call real endpoints on a local service or a test environment and assert on real responses, with no browser and no full user journey:
# tests/api/test_users.py
def test_get_user_returns_expected_fields(api_client):
response = api_client.get("/users/1")
assert response.status_code == 200
body = response.json()
assert isinstance(body["id"], int)
assert "email" in body
def test_unknown_user_returns_404(api_client):
assert api_client.get("/users/999999").status_code == 404
This layer catches what unit tests can't: a wrong auth scheme, a field that changed type in a deploy, pagination that breaks on page two. Cover each happy path, each documented error response, and the edge cases that have hurt you before. Deliberate, not exhaustive. If you're building this layer from scratch, API testing fundamentals is the place to start, and contract testing for breaking changes adds a cheap guard on the shapes this layer depends on.
End-to-end: fewer, slower, worth it
End-to-end tests drive the system the way a user or downstream consumer would: several services, real infrastructure, a full flow from trigger to outcome. Every hop is real, which is why you keep the count low. Before writing one, ask: is there a bug only this test can catch? If not, you're paying end-to-end cost for API-layer coverage.
# tests/e2e/test_order_fulfilment.py
import time
import pytest
@pytest.mark.e2e
def test_paid_order_is_fulfilled(api_client):
order = api_client.post("/orders", json={"product_id": 42, "quantity": 1})
assert order.status_code == 201
order_id = order.json()["id"]
paid = api_client.post("/payments", json={"order_id": order_id, "amount": "29.99"})
assert paid.status_code == 200
deadline = time.monotonic() + 30
while time.monotonic() < deadline:
if api_client.get(f"/orders/{order_id}").json().get("status") == "fulfilled":
return
time.sleep(2)
pytest.fail(f"order {order_id} not fulfilled within 30s")
It's explicit about each step, bounded by a deadline rather than waiting forever, and it crosses real service boundaries. That's the bar.
Telling agents which layer a test belongs in
Left to itself, a coding agent writes whatever test is easiest from where it's standing. Ask it to "add tests for the users endpoint" while a parser file is open and you may get ten mocked unit tests that check the same thing; ask from an E2E file and you get another slow flow. The fix is to make the layers explicit in the repository, so the agent and the reviewer share the same map:
- One directory per layer:
tests/unit,tests/api,tests/e2e. - Markers registered in configuration, with
--strict-markers, so CI can select layers and a typo fails. - A short instruction file the agent reads, such as
.github/copilot-instructions.mdfor Copilot:
## Test layers
- tests/unit: no network, no database. Pure functions and helpers.
- tests/api: real HTTP via the api_client fixture. Default home for new endpoint tests.
- tests/e2e: cross-service flows only. Do not add one without stating which bug
only an end-to-end test can catch.
- Before adding a test, check the layer below does not already cover the behaviour.
In review I ask one question of each agent-written test: is this the cheapest layer that could catch this bug? That catches more waste than any coverage report.
The same layers for LLM features
If your product calls a language model, the pyramid still applies, with one twist at the top:
- Unit: prompt assembly, output parsing and guardrails, tested with an injected fake
generate(prompt) -> strthat returns fixed text. Fast, deterministic, on every commit. - API and tool contracts: if the model calls tools, test each tool's input and output contract directly, and test that your code rejects a malformed tool call. No model needed.
- Evals: real model calls against a fixed set of cases, scored and reported as a pass rate over several runs, because the same input can give different output. These run on a schedule or when the prompt or model changes, like end-to-end tests: few, slow and worth it.
The mistake to avoid is pushing everything into evals because "it's AI". Most failures in LLM features are ordinary bugs in parsing, validation and tool handling, and they belong in the cheap layers.
Cadence ties it together
Unit tests on every commit. API tests on every pull request. End-to-end and evals on merge to main, on a schedule, or both. That keeps the fast loop fast and still catches cross-service and model regressions before production. Where API tests belong in a CI/CD pipeline goes into the pipeline wiring.
Layering is about matching a test's cost to the question it answers. Unit: does this logic work? API: does this service behave at its interface? End-to-end: does the system deliver the outcome? Evals: is the model's behaviour good enough, often enough? When each layer does its own job, the suite is something you trust. My API testing courses build the API layer in depth with Python or Node and GitHub Copilot.