Sub topic 4
Writing requirements with testable acceptance criteria
This chapter assumes you can read basic Python and Markdown and have used Git. It also assumes you have prompted a coding agent before and know what an automated test is. It covers the requirements phase of the workflow: what a requirement contains, how to phrase acceptance criteria so each one can be tested, and the mistakes that make criteria useless.
Why requirements are the contract
A coding agent fills any gap in your instructions with a guess. A requirement document narrows the guesses. It is also the artifact that the design, the task list, and the tests all point back to. If a requirement is vague, every later phase inherits the vagueness, and nobody can say whether the agent's output is right.
A good requirement has two parts:
- A user story, which states who wants what and why. It gives the agent the purpose behind the behavior.
- Acceptance criteria, which are statements about observable behavior. Each one can be checked as true or false.
The story explains intent. The criteria decide whether the work is done. If you can't test a criterion, it is a wish, not a criterion.
Anatomy of a requirement
Here is the requirement used throughout this tutorial.
### Requirement 1: Password reset
**User story:** As a registered user, I want to reset my password by email, so that I can regain access to my account.
#### Acceptance criteria
1. WHEN a user submits a registered email address THEN the system SHALL send a reset link that expires after 30 minutes.
2. WHEN a user submits an unregistered email address THEN the system SHALL return the same response as for a registered address.
3. IF a reset link has already been used THEN the system SHALL reject any further use of that link.Three things make this usable:
- Each criterion states one behavior. Criterion 1 names a trigger (a registered email is submitted), a response (a link is sent), and a constraint (30 minutes).
- Criteria are numbered. Tasks and tests can cite "Requirement 1.3", so a reviewer can trace any piece of code back to the sentence that justified it.
- Criterion 2 is a security behavior. It prevents an attacker from learning which emails have accounts. Many agents skip behavior like this unless you write it down. A criterion is the right place to record a rule that a prompt would easily leave out.
EARS-style phrasing
EARS (Easy Approach to Requirements Syntax) is a small set of sentence templates. The templates force you to name the trigger and the response. You do not need to follow EARS strictly. Its value is that it makes you fill in each slot.
| Pattern | Template | Use it for |
|---|---|---|
| Ubiquitous | The system SHALL ... | Behavior that always holds |
| Event-driven | WHEN event THEN the system SHALL ... | A response to a trigger |
| State-driven | WHILE state, the system SHALL ... | Behavior that depends on a mode or condition |
| Unwanted behavior | IF condition THEN the system SHALL ... | Errors, abuse, and failures |
| Optional feature | WHERE feature is enabled, the system SHALL ... | Behavior that exists only in some configurations |
The example uses two patterns. Criteria 1 and 2 are event-driven. Criterion 3 is an unwanted-behavior criterion, because it handles a case the user should not be able to trigger.
The word SHALL marks a binding statement. Use it consistently. If some criteria say "should" and others say "shall", the agent and the reviewer cannot tell which are mandatory.
What makes a criterion testable
Apply four checks to each criterion before you accept it.
- Observable. You can see the result from outside the system: a response, a stored record, a sent message. "The code is clean" fails this check.
- Specific. It contains numbers, names, or exact outcomes instead of adjectives. "Expires after 30 minutes" passes. "Expires quickly" fails.
- Single. It describes one behavior. If a criterion contains "and" joining two outcomes, a test can pass on one half while the other half is broken. Split it.
- Independent of implementation. It says what happens, not how. "Store the token in Redis" belongs in the design document. If you put it in a requirement, you lock the agent into a choice that the design phase should make.
Rewriting weak criteria
| Weak | Problem | Stronger |
|---|---|---|
| The reset should be fast. | "Fast" has no number. | WHEN a user submits a registered email THEN the system SHALL respond within 500 ms at the 95th percentile. |
| The system handles bad tokens securely. | "Securely" is not observable. | IF a token is unknown THEN the system SHALL reject it with the same error as an expired token. |
| Send an email and log the event. | Two behaviors in one. | Split into two numbered criteria. |
| Use JWT for reset links. | This is a design choice. | Move to the design document. |
Edge cases that criteria must settle
Criteria fail most often at boundaries. Read each one and ask what happens at the limit.
Criterion 1 says a link "expires after 30 minutes". At exactly 30 minutes and 0 seconds, is it valid? The text does not say. One agent will write > and another will write >=. Both are defensible. The spec must pick one, for example: "A link SHALL be valid for 30 minutes after it is issued, and SHALL be rejected after that." Then a test can check 29 minutes and 31 minutes without arguing about the exact boundary, or check the boundary if you decided on one.
Other questions to ask:
- Empty or malformed input. What happens when the email field is blank or not an email address?
- Repeated requests. If a user requests three resets, are the earlier links still valid?
- Concurrency. If two requests redeem the same link at the same moment, does exactly one succeed? Criterion 3 implies this, but concurrent use is where bugs hide.
- Partial failure. If the mail server is down, does the user see an error? Does the system still treat the link as issued?
You do not need to answer every question for every feature. Answer the ones where a wrong guess would cause real harm, and write the answer as a criterion.
From criterion to test
A criterion is ready when you can sketch its test. Criterion 2 shows why the "same response" wording matters. A test can compare two responses directly.
REGISTERED = {"[email protected]"}
def request_reset(email):
# The body and status must not reveal whether the address is registered.
if email in REGISTERED:
pass # a real implementation would queue the reset email here
return {
"status": 202,
"message": "If the address is registered, a reset link was sent.",
}
def test_unregistered_email_gets_same_response():
known = request_reset("[email protected]")
unknown = request_reset("[email protected]")
assert known == unknownIf the criterion had said "the system SHALL protect user privacy", you could not write this test. The specific wording is what makes the check possible. A later chapter covers turning whole sets of criteria into tests. For now, use the sketch as a quality check on your wording. If you can't sketch an assertion, rewrite the criterion.
Note that this test compares the response only. It does not check timing. An implementation could still leak information because registered addresses take longer to process. If that matters to you, add a criterion about it. A test only covers what the criteria say.
Working with an agent on requirements
You can ask the agent to draft requirements, and this is often a good use of it. Treat the draft as a proposal.
- Give the agent the feature description and the user story you want. Ask for EARS-style criteria.
- Read every criterion against the four checks above. Agents tend to produce plausible but vague wording, such as "the system SHALL handle errors gracefully".
- Look for what is missing. Agents rarely volunteer the unwanted-behavior cases. Ask directly: "What can go wrong with each criterion?"
- Remove criteria that describe implementation. Agents often leak design choices into requirements.
- Commit the approved version to Git before any implementation begins. The commit is your baseline, and later changes to it appear in review.
Common mistakes
- Adjectives instead of numbers. Words like "quick", "secure", "user-friendly", and "robust" cannot be tested. Replace them with a threshold or an observable result.
- Compound criteria. Two behaviors in one sentence let a partial implementation pass.
- Design in the requirements. Naming libraries, tables, or classes locks in decisions too early and makes later design review pointless.
- Only the happy path. If every criterion starts with WHEN and none starts with IF, you have not specified failure behavior. The agent will invent it.
- No identifiers. Without numbers, tasks and tests cannot cite criteria, and drift becomes hard to detect.
- Editing criteria to match the code. If the agent's output disagrees with a criterion, decide which one is wrong. Changing the criterion silently to make a test pass defeats the purpose of having one.
Checklist
Before you move on to the design phase, check that each requirement has:
- A one-sentence user story with a role, a goal, and a reason.
- Numbered criteria, each with one behavior.
- At least one unwanted-behavior (IF ... THEN) criterion.
- Numbers or exact outcomes wherever a quantity or a message matters.
- No technology choices.
- A way to test each criterion, even if you have not written the test yet.
Exercise
Rewrite this criterion so that it is testable: "When a user logs in too many times with the wrong password, the system should lock the account for a while and tell the user." Give at least two criteria, and say which problems you fixed.
Show answer
One acceptable answer: (1) IF a user submits an incorrect password 5 times within 10 minutes THEN the system SHALL lock the account for 15 minutes. (2) WHILE an account is locked, the system SHALL reject every login attempt, including attempts with the correct password, with a message stating that the account is temporarily locked. The exact numbers can differ, but they must be stated. Problems fixed: "too many" and "a while" had no numbers, "should" was replaced by SHALL, and the original combined two behaviors (locking and notifying) in one sentence. The rewrite also settles what happens during the lock, which the original left open.
Exercise
A teammate writes this acceptance criterion: "WHEN a user requests a reset THEN the system SHALL store a hashed token in the tokens table." Why does it belong somewhere else, and what would you write instead?
Show answer
It names a storage mechanism (hashing, a specific table), which is a design decision. It also does not describe behavior a user or test could observe from outside. Move the hashing and table choice to the design document. In the requirements, state the observable outcome, for example: "WHEN a user submits a registered email THEN the system SHALL send a reset link that can be used to set a new password," and an unwanted-behavior criterion such as "IF a reset link is used after it expires THEN the system SHALL reject it." If a security rule truly matters at the requirement level, phrase it as behavior, for example "the system SHALL NOT include the user's password or a reusable credential in any reset message."