AI-POWERED TESTING

Where AI-Generated Tests Get API Testing Wrong, and How to Check

I use GitHub Copilot and coding agents in my test work regularly, and the output is good enough that it's tempting to accept it without a second look. A generated file imports cleanly, the assertions are valid, and it passes the first time. That's exactly when to slow down. These tools reproduce the patterns of code that looks like good testing, and "looks like" isn't the same as "catches the bug."

The failures aren't random. They cluster around the same blind spots: assertions that confirm a response arrived but not what it contains, missing authentication edge cases, no awareness of test state, and, with agents that can run your API, expected values copied from whatever the API returned. These aren't beginner mistakes. They come from the model seeing the surface of an HTTP exchange without knowing your contract.

Below are those failure patterns, why they happen, and the checks I run on generated tests before they reach CI. None of this means giving up AI tools. It means knowing where to push back.

Build an API Automation Framework With Node.js

Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.

Learn more

Assertions that confirm a response, not its meaning

The most consistent weakness is assertion depth. Ask for a test of GET /users/{id} and you'll often get this:

def test_get_user(api_client):
    response = api_client.get("/users/42")
    assert response.status_code == 200

That's a connectivity check, not a test. It won't catch a dropped email field, an empty roles list or an ID that turned into a string. All of those are contract breaks that pass straight through. What you want checks the contract:

def test_get_user_returns_contract_fields(api_client, created_user):
    response = api_client.get(f"/users/{created_user.id}")
    assert response.status_code == 200

    body = response.json()
    assert body["id"] == created_user.id
    assert isinstance(body["id"], int)
    assert body["email"] == created_user.email
    assert isinstance(body["roles"], list) and body["roles"], "user should have at least one role"

Validating the whole response against a JSON Schema does this in one step, and it's worth adding even for generated tests. The quickest review question I know is: "What regression could get past this assertion?" If the answer is "a lot," the test isn't done. Prompts that ask for specific assertions up front help too, and writing AI prompts that produce testable API assertions covers how to write them.

Auth edge cases the happy path hides

Generated tests work outward from the happy path. For a protected endpoint you get a valid token, a 200, and nothing else. The negative cases almost never appear unless you ask:

import pytest


@pytest.mark.parametrize("headers, expected_status", [
    pytest.param({}, 401, id="missing-token"),
    pytest.param({"Authorization": "Bearer not.a.real.token"}, 401, id="malformed-token"),
    pytest.param("expired", 401, id="expired-token"),
    pytest.param("read_only", 403, id="insufficient-scope"),
])
def test_admin_report_rejects_bad_credentials(api_client, token_headers, headers, expected_status):
    if isinstance(headers, str):
        headers = token_headers(headers)
    response = api_client.get("/admin/report", headers=headers)
    assert response.status_code == expected_status

The 401 versus 403 distinction matters: one means "I don't know who you are," the other "I know who you are and you can't do this." Mixing them up is a security design problem, and a happy-path-only suite will never notice. For every generated test of a protected endpoint I go through the same list: missing, malformed, expired, wrong scope, another user's resource. Testing OAuth-protected APIs without hardcoding tokens covers how to get those tokens cleanly.

No sense of test state

Generated tests treat each function as standalone and hardcode IDs like 42 or 1 that may or may not exist where the suite runs. The result passes in one environment and fails in another, or passes once and fails on the second run because the record has gone. That's the kind of flakiness that wears down trust in the whole suite.

The fix is to treat IDs as test-local: create what the test needs, use it, clean up:

@pytest.fixture
def created_order(api_client):
    response = api_client.post("/orders", json={"product_id": 7, "quantity": 2})
    assert response.status_code == 201, response.text
    order_id = response.json()["id"]
    yield order_id
    api_client.delete(f"/orders/{order_id}")

Tools rarely produce this without being asked, and when they do it still needs review. Watch for fixtures without teardown, teardown that assumes creation worked, and IDs still hardcoded in the test body next to a fixture. Structuring API test teardown so it actually runs goes into the details.

Agent-specific failures: snapshots and self-edits

When a coding agent can call the API and run the suite, two more failure modes appear.

Snapshot assertions. The agent calls the endpoint, looks at the response and writes the expected values from it. The test passes, and it describes current behaviour, bugs included. If the API returns the wrong total today, the test now insists on the wrong total. The giveaway in review is an expected value nobody can trace to a spec, a ticket or a business rule.

Editing the test until it passes. Told to make tests pass, an agent may change the expected status from 422 to 400 because that's what came back, or mock the function the test was meant to exercise. Each edit looks reasonable on its own. Together they produce a green suite that checks nothing.

I handle both with rules in the repository instruction file, such as .github/copilot-instructions.md, plus one check in review:

- Expected values come from openapi.yaml or the linked ticket, never from a live
  response. Cite the source in a comment when it is not obvious.
- Never mock the module under test. Mock only external services.
- If a test fails, report the mismatch. Do not change expected values to match.

The review check is to make each new test fail on purpose: change the expected value or break the behaviour and confirm it goes red. A test that stays green has a problem, however good it looks.

Treat generated tests as scaffolding

AI tools are good at scaffolding: the request, the imports, the basic shape. They can produce it faster than I can type. What they don't bring is your system's state model, its auth rules or the difference between a response that arrived and a response that's correct. That judgment is still yours. The wider picture of where AI helps in testing and where it needs a firm hand is in how AI is changing software testing. If you want to practise reviewing and strengthening generated tests on a real API, that's the core of my API testing courses.