Structuring API Tests Around User Journeys, Not Endpoints
The most common way to organise an API test suite is by endpoint: a file for /users, a file for /orders, a file for /payments. It's tidy, it mirrors the API documentation, and it misses a whole category of bugs. Each endpoint can pass its own tests while the sequence a real user goes through, sign up, add a payment method, place an order, check its status, is broken somewhere in between.
Those in-between failures are where a lot of production incidents come from: an id format one endpoint returns that another doesn't accept, a status that never updates, a permission that works on create but not on read. Endpoint tests rarely catch them, because each test sets up its own perfect starting state.
Journey-level testing has picked up a second use. AI agents that call your API through tools perform journeys too: a sequence of calls to complete a task. Testing whether an agent completes the task, not just whether each call succeeds, is the same idea applied to a new kind of user. In this article I'll cover how I structure journey tests, how they sit alongside endpoint tests, and how the approach applies to agents.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
What a journey test looks like
A journey test follows one realistic path through the API, passing real outputs from each step into the next. Here's a checkout journey in pytest:
import uuid
def test_new_customer_can_place_and_track_an_order(api):
email = f"qa-{uuid.uuid4().hex[:10]}@example.test"
signup = api.post("/customers", json={"email": email, "name": "Journey Test"})
assert signup.status_code == 201, f"signup: {signup.text}"
customer_id = signup.json()["id"]
card = api.post(f"/customers/{customer_id}/payment-methods",
json={"token": "tok_test_visa"})
assert card.status_code == 201, f"add card: {card.text}"
order = api.post("/orders", json={
"customer_id": customer_id,
"payment_method_id": card.json()["id"],
"items": [{"sku": "QA-001", "quantity": 2}],
})
assert order.status_code == 201, f"place order: {order.text}"
order_id = order.json()["id"]
status = api.get(f"/orders/{order_id}")
assert status.status_code == 200, f"track order: {status.text}"
assert status.json()["customer_id"] == customer_id
assert status.json()["status"] in ("pending", "confirmed")
Two details make this useful rather than fragile. Every value passed between steps comes from a real response, not a hard-coded id, which is how integration mismatches get caught. And every assertion names its step in the failure message, so when the journey breaks you know where: "place order: 422 ..." points straight at the problem. The api fixture is a client that adds the base URL and auth, with cleanup handled by fixtures as usual. Chaining requests well has its own pitfalls, which I cover in writing API tests that cover chained request dependencies.
Organise the suite by journey
I organise journey tests by what the user is trying to do, not by which endpoints they touch:
tests/
journeys/
test_onboarding.py
test_checkout.py
test_refunds.py
test_account_closure.py
endpoints/
test_orders_validation.py
test_users_validation.py
Journeys use the vocabulary of the business, which makes them easy to discuss with people outside engineering. "The refund journey is failing" means something to a product owner; "test_post_refunds_3 is failing" doesn't. If your team uses Behave or Cucumber, journeys map naturally onto scenarios, and the feature file becomes a readable description of the flow.
Journeys don't replace endpoint tests
Journey tests are slow to run and coarse at diagnosis, so they shouldn't carry everything. The split I use:
- Journey tests for a small number of critical paths: the flows that would hurt most if broken. Each one checks the hand-offs between endpoints.
- Endpoint tests for breadth: validation rules, error codes, edge cases and permissions for each endpoint, usually heavily parametrised.
If a journey test is the only thing that catches a validation bug, that's a gap in the endpoint tests, not a success for the journey. A healthy suite has a few journeys and many focused tests. I write more about where each kind of test belongs in layering a test suite: unit, API, end-to-end.
Practices that keep journey tests reliable
- Create fresh data for each journey. A journey that relies on a pre-existing customer breaks when someone else's test modifies that customer.
- Fail at the right step. Assert after every call, not only at the end. A journey that fails on its final assertion because step two quietly returned an error wastes a lot of debugging time.
- Handle asynchronous steps honestly. If an order moves to "confirmed" in the background, poll with a timeout rather than sleeping a fixed amount.
- Clean up everything the journey created, including when it fails partway through.
- Keep journeys short. Four to six steps per journey is easier to maintain than one test that walks the entire product.
Journey tests are also good at documenting business rules, because the flow itself describes how the product is supposed to work. See writing API tests that document business rules.
When the user is an AI agent
If your product exposes tools to an AI agent, through an MCP server or a function-calling interface, the agent becomes a user of your API that performs journeys. "Find this customer's last order and request a refund" is a journey: look up the customer, list orders, pick the right one, call the refund tool. Every individual tool can work perfectly while the agent picks the wrong order or skips a step.
Endpoint-level tests of each tool are still necessary. They're deterministic and cheap. But they can't tell you whether the agent completes the task. For that you need journey-level evaluations: give the agent a task, let it run against a sandbox, and check the outcome and the path.
def test_agent_refunds_the_most_recent_order(run_agent, seeded_customer):
# seeded_customer has two orders; the newer one should be refunded.
results = [run_agent(f"Refund the most recent order for {seeded_customer['email']}")
for _ in range(5)]
passed = 0
for result in results:
refunded = [c for c in result.tool_calls if c.name == "refund_order"]
if (len(refunded) == 1
and refunded[0].args["order_id"] == seeded_customer["latest_order_id"]):
passed += 1
assert passed >= 4, f"{passed}/5 runs refunded exactly the latest order"
Here run_agent is your own harness, returning the tool calls the agent made. Three things differ from a normal journey test. The test checks the trajectory, which tools were called with which arguments, as well as the end state. It checks for things that must not happen, like refunding the wrong order or refunding twice. And because the agent's behaviour varies from run to run, it runs several times and asserts a pass rate rather than a single pass.
The same journey thinking helps when a coding agent writes your tests. Ask an agent for "tests for the orders API" and you'll get endpoint tests. Ask for "a test of the journey where a new customer places and tracks an order, passing real ids between steps and asserting after every call", and you get something much closer to what I showed above.
Test what users actually do
Endpoint tests tell you each piece works. Journey tests tell you the pieces work together for someone trying to get something done, whether that someone is a person or an agent. Keep a few critical journeys, many focused endpoint tests, and the boundary between them clear. If you'd like to build a suite structured this way, with Python and Behave or Node and Cucumber, my API testing courses walk through it using VS Code and GitHub Copilot.