Sub topic 7
Turning acceptance criteria into tests
This chapter assumes you can write pytest tests and have read the earlier chapters on requirements and task breakdown. You will turn acceptance criteria into automated tests, so you can check an agent's work by running them instead of reading every line.
Why tests make a spec executable
An acceptance criterion written in WHEN/THEN/SHALL form states one observable behavior. A test states the same behavior in code. If each criterion maps to at least one test, "does the agent's output match the spec?" becomes a command that passes or fails.
Reading agent output is slow and misses things. Plausible code often looks correct. A failing test gives you a specific, repeatable fact to hand back to the agent.
From criterion to test
Take two criteria from the password reset requirement:
- WHEN a user submits a registered email address THEN the system SHALL send a reset link that expires after 30 minutes.
- IF a reset link has already been used THEN the system SHALL reject any further use of that link.
The mapping is mechanical:
- The WHEN or IF clause becomes the setup (the "arrange" and "act" steps).
- The THEN clause becomes the assertion.
- The number in the criterion, such as 30 minutes, becomes a constant in the test.
The tests below cover criteria 1 and 3. The small ResetTokens class is a reference implementation so the file runs as shown. In real work, the agent writes this part and you write only the tests.
from datetime import datetime, timedelta, timezone
import pytest
TOKEN_TTL = timedelta(minutes=30)
class TokenError(Exception):
pass
class ResetTokens:
def __init__(self):
self._issued = {}
self._used = set()
def issue(self, token, now):
self._issued[token] = now
def redeem(self, token, now):
if token in self._used:
raise TokenError("already used")
issued = self._issued.get(token)
if issued is None or now - issued > TOKEN_TTL:
raise TokenError("expired or unknown")
self._used.add(token)
T0 = datetime(2026, 1, 1, tzinfo=timezone.utc)
def test_token_expires_after_30_minutes():
tokens = ResetTokens()
tokens.issue("abc", T0)
tokens.redeem("abc", T0 + timedelta(minutes=29)) # still valid
tokens.issue("def", T0)
with pytest.raises(TokenError):
tokens.redeem("def", T0 + timedelta(minutes=31))
def test_token_can_be_used_only_once():
tokens = ResetTokens()
tokens.issue("abc", T0)
tokens.redeem("abc", T0)
with pytest.raises(TokenError):
tokens.redeem("abc", T0)Save this as test_reset.py and run pytest. Both tests should pass.
Notice that the code passes the current time in as an argument (now). The tests never wait 30 minutes and never patch the clock. Specs that mention time, randomness, or external services are easier to test when the design lets you inject those values. This is a good reason to settle such details in the design document before the agent writes code.
Where tests fit in the task list
Link each test to a task and its requirement. In the task list from the previous chapter, task 2 says "Write tests first. Covers: Requirement 1.1, 1.3." That ordering matters. If the agent writes the tests and the code together, the tests tend to describe whatever the code does, including its bugs.
A workable sequence for each task:
- You or the agent write the tests from the criteria. You review them.
- Run them and confirm they fail for the right reason.
- The agent implements the task until they pass.
- Run the full suite, not just the new tests.
Common errors
- Tests that mirror the implementation. If the agent writes both, a wrong assumption appears in both and the test passes. Review the tests against the criteria, not against the code.
- Ambiguous boundaries. "Expires after 30 minutes" does not say whether minute 30 exactly is valid. The test above checks 29 and 31 and leaves the boundary open. Settle it in the requirement if it matters.
- Weak assertions. A test that only checks "no exception was raised" passes for many wrong implementations. Assert the specific outcome the THEN clause names.
- Untested criteria. Criterion 2 (same response for registered and unregistered emails) has no test here. Keep a list of criteria and the test covering each one.
- Editing tests to make them pass. Tell the agent in your instruction file not to change tests without asking.
Exercise
Write the test for criterion 2: "WHEN a user submits an unregistered email address THEN the system SHALL return the same response as for a registered address." Which part of the criterion becomes the setup, and which part becomes the assertion?
Show answer
The WHEN clause is the setup: call the reset-request endpoint once with a registered email and once with an unregistered one. The THEN clause is the assertion: check that the two responses are equal, including status code and body. Comparing the two responses directly is stronger than checking each one against a fixed value.