Versioning Test Helpers Without Breaking Every Test (or Agent)
Shared test helpers feel like a win until they don't. You pull out a build_auth_headers() function, use it everywhere, and then someone needs a new required parameter for a token refresh flow. One change and a large part of the suite goes red. I've seen teams stop touching shared utilities altogether because they can't predict what will break. That's a problem worth fixing directly.
Helpers changing isn't the problem. Everything in a living codebase changes. The problem is treating helpers as loose code instead of as an API with callers. A library that breaks its public interface bumps a version and your dependency manager tells you. A test helper that breaks its implicit contract tells you when CI fails on several branches at once.
This has become more pressing as coding agents write more of the tests. An agent copies whatever helper usage it finds, including deprecated ones, and it will also offer to update a hundred call sites in one go. Both are manageable if your helpers send clear signals. Here are the design, deprecation and structure habits I use, and how I hand a migration to an agent safely.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Design helpers as an API from day one
Every parameter name, default and return shape of a helper is a contract its callers rely on. The difference between a sturdy helper and a fragile one is whether that contract was designed or just grew out of what the first caller needed.
The first habit is keyword-only optional arguments. A helper that starts with positional parameters makes every later addition risky, because call sites rely on argument order. The * fixes that:
def make_user(username: str, role: str, *, active: bool = True, extra_claims: dict | None = None) -> dict:
payload = {"username": username, "role": role, "active": active}
if extra_claims:
payload.update(extra_claims)
return payload
Everything after * has to be named at the call site, so you can add new options later without touching existing callers.
The second habit is returning a structured object, not a bare value. If a helper returns just a token string and you later need the expiry too, every caller has to change. A small dataclass from the start lets you add fields without breaking anyone:
from dataclasses import dataclass
@dataclass(frozen=True)
class AuthToken:
token: str
scheme: str = "Bearer"
expires_at: str | None = None
def header(self) -> dict:
return {"Authorization": f"{self.scheme} {self.token}"}
def generate_auth_token(user_id: str, *, scheme: str = "Bearer") -> AuthToken:
return AuthToken(token=f"test-token-{user_id}", scheme=scheme)
The third habit is avoiding boolean flags that switch behaviour. make_user("alice", "viewer", True, False) is unreadable, and each new flag makes it worse. Named variants such as make_admin_user() are clearer and easier to extend. Most helpers end up in fixtures, so the same thinking applies to keeping conftest files from becoming a mess.
Deprecate before you delete
Renaming or removing a helper in one commit is the fastest way to break the suite. The better approach is a short deprecation cycle: keep the old name working, make it warn, and point to the replacement.
import warnings
def make_auth_header(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
def build_auth_header(token: str) -> dict:
"""Deprecated. Replace build_auth_header(t) with make_auth_header(t)."""
warnings.warn(
"build_auth_header() is deprecated; use make_auth_header() instead",
DeprecationWarning,
stacklevel=2,
)
return make_auth_header(token)
stacklevel=2 makes the warning point at the calling test, not at the helper, so the output tells you exactly which call site to change. pytest shows these in its warnings summary. When you're ready to enforce the migration, make them errors in your config:
# pytest.ini
[pytest]
filterwarnings =
error::DeprecationWarning:tests.*
Scoping the filter to your own test modules stops third-party deprecation warnings from failing your build. You can also run pytest -W error::DeprecationWarning in a single CI job first to see how many call sites remain.
Put the migration in the docstring or the warning as a concrete before-and-after. People copy and paste, and so do agents. A short CHANGELOG.md in the helpers package, a bullet per change and what to do instead, answers the question everyone asks when they hit the warning.
Give the helper package a public surface
Design and deprecation don't help much if every helper lives in one long helpers.py that everything imports from. Treat helpers as a small internal package:
tests/
helpers/
__init__.py # the public surface
auth.py
factories/
user.py
order.py
_internal/
token_crypto.py # implementation detail, not imported by tests
The package's __init__.py re-exports only what tests should use, with an explicit __all__. Anything under _internal can change freely because nothing outside the package imports it. When the public surface does change, there's one file that shows exactly what's exposed.
Then add a small contract test for the helpers themselves: import every public name, call it with valid inputs and check the return type. It's cheap and it catches signature changes before hundreds of real tests fail in confusing ways. This kind of self-checking infrastructure is part of the test architecture practices that let a suite grow without getting fragile.
Coding agents as helper consumers and migrators
Agents interact with helpers in two ways, and both are worth planning for.
First, as consumers. When Copilot or Claude Code writes a new test, it copies the helper usage it finds nearby. If half the suite still calls build_auth_header(), new tests will too. Tell it otherwise in the instruction file your agent reads, such as .github/copilot-instructions.md:
## Test helpers
- Import helpers only from tests.helpers (the package root). Never import from
tests.helpers._internal.
- Do not call deprecated helpers. If pytest reports a DeprecationWarning from
tests/, use the replacement named in the warning.
- Do not change a helper's signature. Propose the change in your summary instead.
Second, as migrators. Updating call sites after a rename is exactly the mechanical work an agent does well, and deprecation warnings make it checkable. My prompt states what done means: "Replace every call to build_auth_header with make_auth_header. Change only call sites, not assertions or test logic. Done means the suite passes with -W error::DeprecationWarning and the diff touches no assert lines."
That last condition makes review fast. I scan the diff for any changed assert line, and if there are none, the migration is mechanical and the full run confirms it. If there are some, the agent went beyond the task and those lines get a careful read.
Helpers designed this way are something you can improve with confidence, whether a person or an agent makes the change. If you'd like to build a suite structured like this from the start, my API testing courses cover it.