TEST ARCHITECTURE

Tagging API Tests So CI and Agents Run the Right Subset

One of the quietest sources of CI friction I see in API test suites is the all-or-nothing run. Every push triggers the full suite, developers wait for results that are mostly irrelevant to their change, and eventually someone weakens the gate because it's in the way. The fix usually isn't a faster machine. It's tagging tests so the pipeline can run exactly the subset it needs at each stage.

There's a second reader for those tags now. When GitHub Copilot, Claude Code or a similar coding agent changes an endpoint, it runs tests to check its own work. If the suite has a clear taxonomy, the agent can run pytest -m auth in seconds and iterate. If it doesn't, the agent either runs everything on every attempt or guesses, and agents also write new tests that need the right tags from day one. A tagging scheme is now part of the instructions you give your tools, not just part of your CI file.

In this article I'll walk through the tagging patterns I rely on in pytest and Behave, the CI configuration that puts them to work, how I make tags enforceable so neither people nor agents can skip them, and where tests that call LLM features fit in.

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

A tag taxonomy that survives a real API suite

Before writing a single marker, decide what questions your tags need to answer. Three axes cover almost every CI use case I've run into:

  • Tier: smoke, regression, slow. How much of the suite should run right now?
  • Domain: auth, payments, search. Which tests are relevant to this change?
  • Boundary: contract, external. Is it safe to run this against a live third-party service?

Each axis is queryable on its own, and the combinations are where tags pay off. In pytest, register every marker in pytest.ini and turn on strict markers:

[pytest]
addopts = --strict-markers
markers =
    smoke: fast sanity checks suitable for every PR
    regression: full-depth scenarios run on merge to main
    slow: tests that take more than a few seconds each
    auth: authentication and authorization flows
    contract: schema and contract validation
    external: calls a live third-party service
    llm: calls a model-backed feature; judged by pass rate, not a single run

The --strict-markers line is the part people skip. Registering markers on its own only gets you a warning for a typo like @pytest.mark.smke. With strict markers, an unregistered marker is a collection error, so a misspelled tag fails loudly instead of quietly dropping a test out of every run.

import pytest


@pytest.mark.smoke
@pytest.mark.auth
def test_valid_login_returns_200(api_client):
    response = api_client.post("/auth/login", json={"user": "test", "password": "secret"})
    assert response.status_code == 200


@pytest.mark.regression
@pytest.mark.auth
def test_expired_token_returns_401(api_client):
    response = api_client.get("/profile", headers={"Authorization": "Bearer expired_token"})
    assert response.status_code == 401

A test carries one marker per axis. When someone touches the auth service, pytest -m auth gives them every auth test regardless of tier. The pre-merge gate runs pytest -m smoke. Both selections are correct, and neither needs a hand-maintained file list.

In Behave, tags go on the feature or scenario:

@smoke @auth
Scenario: Valid credentials return a token
  Given I have valid credentials
  When I POST to /auth/login
  Then the response status is 200
  And the response body contains an access_token

Run a single tag with behave --tags=smoke. Combining tags is where Behave versions differ: recent releases accept boolean expressions like --tags="smoke and auth", while older ones use the comma-and-repeat syntax. Check which version your project pins before you write the CI command.

Wiring tags into CI stages

The pattern I keep coming back to is three stages, each with its own selection:

  • Every PR: smoke only. Fast, high-signal, blocks the merge if red.
  • Merge to main: regression, excluding external services.
  • Nightly or release: everything, including slow, external and llm.

Where each stage sits in the delivery flow matters as much as what it runs; I cover that in where API tests belong in a CI/CD pipeline. In GitHub Actions the first two stages look like this:

jobs:
  smoke:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -r requirements.txt
      - run: pytest -m smoke --tb=short -q

  regression:
    runs-on: ubuntu-latest
    needs: smoke
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -r requirements.txt
      - run: pytest -m "regression and not external and not llm" --tb=short

needs: smoke means regression never starts if smoke is already red. The marker expression uses pytest's boolean syntax to keep live third-party calls and model-backed tests out of the merge run. The nightly job flips it around with pytest -m "regression or slow or external or llm", so a new external test lands in the nightly run automatically and nowhere else.

One habit I push back on: using tags only to exclude. Teams mark a few tests slow and run -m "not slow" everywhere. That works until someone forgets to tag a new slow test and it silently inflates the PR gate. For any gate with a time budget, prefer explicit inclusion.

Making tags enforceable, for people and agents

Tag drift is the long-term problem. Tests grow until they aren't smoke tests anymore, and new tests arrive with no tier at all. AI-generated tests make this worse in a specific way: they tend to be thorough, chain several calls, and get tagged optimistically, or not at all. So I don't rely on review alone. A small hook in conftest.py fails collection when a test has no tier:

import pytest

TIERS = {"smoke", "regression", "slow"}


def pytest_collection_modifyitems(config, items):
    untiered = [
        item.nodeid
        for item in items
        if not TIERS & {mark.name for mark in item.iter_markers()}
    ]
    if untiered:
        raise pytest.UsageError(
            "Tests missing a tier marker (smoke/regression/slow):\n" + "\n".join(untiered)
        )

That turns the taxonomy into something a coding agent gets immediate feedback on. If it writes a test without a tier, its own test run fails with a message that says exactly what to fix. The other half is telling the agent the rules up front. Copilot reads .github/copilot-instructions.md, and most other agents have an equivalent instructions file:

## Test tags
- Every test gets exactly one tier marker: smoke, regression or slow.
- smoke = one request, no data seeding, under a second. If in doubt, use regression.
- Add a domain marker (auth, payments, search) matching the code under test.
- After changing code in a domain, run: pytest -m "<domain> and not external"
- Never remove or change a tag to make a CI stage pass; explain the problem instead.

The "run this after a change" line is the one that saves the most time in practice. It gives the agent a fast, scoped feedback loop instead of the full suite, which keeps its iterations short and keeps it from calling live third-party services while it experiments.

Where model-backed tests fit

If your API includes an LLM feature, such as a summary endpoint or a support-reply generator, the tests for it don't behave like the rest of the suite. The same input can produce different output, so a single pass or fail tells you little. I tag those tests llm and keep them out of the PR gate entirely. In the nightly job they run several times, and the result that matters is the pass rate over those runs, compared against the previous night's.

The tag is what makes that separation possible. Without it, a model-backed test sits in the smoke run, fails one time in ten, and teaches everyone to hit "re-run" on red builds. That habit then spreads to tests that really are broken. Keeping non-deterministic tests behind their own marker protects the meaning of every other red build.

Keeping the taxonomy honest

  • Watch smoke runtime. If the smoke run keeps getting slower, something got mislabelled.
  • Review tags like assertions. A test tagged smoke that seeds a database and makes five chained calls is not a smoke test, whoever wrote it.
  • Keep external small. These are the tests most likely to flake for reasons unrelated to your code. Document why each needs a live call, and convert to a mock or contract check where you can.
  • Document the markers in the repo. A short section listing each marker, its meaning and its CI stage helps new teammates, and the same text can go straight into your agent instructions.

A well-tagged suite respects everyone's time: the developer waiting on a PR, the reviewer checking which tests cover an endpoint, and the agent that needs a quick, honest signal before it hands work back to you. If you want to build a suite like this from scratch with Behave or Cucumber, VS Code and GitHub Copilot in the loop, that's what my API testing courses cover, and the guide to Python automation frameworks is a good companion read.