Designing Test Suites That Survive an API Version Bump
Nothing exposes weak test architecture faster than an API version bump. One day the suite is green; the next, a new /v2/ prefix, a renamed field or a changed pagination scheme turns dozens of tests red. The product usually isn't broken. The tests were written against a snapshot of one version rather than against a contract that can change in a controlled way.
A version bump is also a job teams now hand to coding agents: "update the tests for v2". It's a reasonable task for an agent, because much of it is mechanical. It's also risky, because the easiest way to make a red suite green is to change every expected value to match whatever v2 returns. If the suite is designed well, the agent's change is small and reviewable. If it isn't, you get a huge diff that nobody can check properly, and v1 coverage quietly disappears along the way.
Here's how I structure a suite so a version bump is a contained change: isolating the version in one place, using schemas as an early warning, running old and new versions side by side in CI, and keeping agent-assisted migrations reviewable.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Isolate the version so tests don't hard-code it
If /v1/ appears in a hundred test files, a version bump is a hundred-file change. The version belongs in exactly one place: configuration, read by the API client.
# conftest.py
import os
import pytest
from tests.api_client import ApiClient
def pytest_addoption(parser):
parser.addoption("--api-version", default=os.getenv("API_VERSION", "v1"))
@pytest.fixture(scope="session")
def api_version(request):
return request.config.getoption("--api-version")
@pytest.fixture(scope="session")
def api(api_version):
base = os.environ["API_BASE_URL"]
return ApiClient(f"{base}/{api_version}", headers={"Authorization": f"Bearer {os.environ['API_TOKEN']}"})
Tests then call api.get("/orders") and never mention a version. The same applies to versions carried in a header or media type: set it once in the client.
The second layer is field access. When tests reach deep into raw JSON everywhere, a renamed field breaks every one of them. I put small accessors or models in the client layer for the resources the suite uses heavily, so a rename is a change in one function:
def order_total(body, api_version):
if api_version == "v1":
return body["total"]
return body["amount"]["value"]
Keep this to genuine differences. If the accessor layer grows into a second copy of the API, it becomes its own maintenance problem. I go into keeping shared helpers stable in versioning your test helpers without breaking every test.
Schemas as a version-bump early warning
A per-version JSON Schema for each important response does two jobs. It catches unannounced changes within a version, and when a new version arrives it shows you exactly what changed before you touch a test.
tests/contracts/
v1/order.schema.json
v2/order.schema.json
The schema helper picks the right directory from api_version, so the same test validates against the right contract. Diffing the two schema files is the fastest summary of a version bump you'll get, and it's far easier to review than the test changes that follow from it. This is the same approach I use for contract testing for breaking changes, applied across versions.
Split tests into two groups as well. Most tests check behaviour that should be the same in every version: a cancelled order can't be shipped, an unauthenticated call gets a 401. Those should pass unchanged against v1 and v2. A smaller group checks version-specific behaviour, and I mark it explicitly:
import pytest
@pytest.mark.only_versions("v2")
def test_orders_use_cursor_pagination(api):
body = api.get("/orders", params={"limit": 5}).json()
assert "next_cursor" in body
# conftest.py
def pytest_configure(config):
config.addinivalue_line("markers", "only_versions(*versions): run only for these API versions")
def pytest_collection_modifyitems(config, items):
version = config.getoption("--api-version")
for item in items:
marker = item.get_closest_marker("only_versions")
if marker and version not in marker.args:
item.add_marker(pytest.mark.skip(reason=f"not applicable to {version}"))
The ratio between the two groups is a good health check. If most tests are version-specific, the suite is testing response shapes rather than behaviour.
Run old and new versions in parallel in CI
While both versions are live, both need to stay tested. A matrix job runs the same suite against each:
jobs:
api-tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
api_version: [v1, v2]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt
- run: pytest --api-version=${{ matrix.api_version }} --junitxml=results-${{ matrix.api_version }}.xml
env:
API_BASE_URL: ${{ vars.API_BASE_URL }}
API_TOKEN: ${{ secrets.API_TOKEN }}
fail-fast: false keeps a v2 failure from cancelling the v1 run, so you always see both results. When v1 is retired, removing it is a one-line change, and any only_versions("v1") tests show up in a search.
Letting an agent do the migration, safely
With the suite structured this way, an agent-assisted migration is a sequence of small, reviewable steps instead of one giant edit. I split the work into separate tasks and pull requests:
- Add the v2 schemas from the API's published spec. Review this one carefully; it defines what "correct" means for everything that follows.
- Add v2 to the CI matrix and let it fail. The failures are the migration to-do list.
- Update accessors and version-specific tests to get v2 green, without touching v1 behaviour.
The instructions file carries the rules, whether that's .github/copilot-instructions.md or your agent's equivalent:
## API version migrations
- Do not change expected values in shared behaviour tests. If one fails on v2,
report it: it is either a regression or an intentional change to confirm.
- Version differences go in the client accessors or only_versions tests.
- Do not remove or weaken v1 tests while v1 is still supported.
- Schemas in tests/contracts/ come from the published spec, not from responses.
The first rule is the most important. A shared behaviour test failing on v2 is exactly the signal you want, and an agent that "fixes" it by editing the expected value destroys it. In review I check whether any shared test changed, before looking at anything else.
The same structure helps if your API puts a model behind an endpoint. Changing the model or the prompt is a version bump in all but name: behaviour shifts without a schema change. Treat it the same way. Keep the behaviour checks fixed, run them against the old and new configuration side by side, compare pass rates over several runs, and review what changed before switching over.
A suite that survives a version bump keeps the version in one place, separates shared behaviour from version-specific shape, and tests both versions while both are live. For the broader design principles behind this, see my guide to test architecture best practices. If you'd like to build a client and fixture layer like this from scratch with Copilot's help, my API testing courses walk through it.