Sub topic 4
Writing requirements with testable acceptance criteria
This chapter assumes you can read and write basic tests, use Git, and have prompted a coding agent before. It covers the requirements phase of the spec-driven workflow: what a requirement contains, how to phrase acceptance criteria so each one can be tested, and the errors that make criteria useless.
What a requirement contains
A requirement has two parts:
- A user story names who wants something and why. It gives the agent the purpose behind the feature.
- Acceptance criteria are the conditions that must hold for the requirement to count as done. Each one describes one behavior that someone can observe.
The user story sets direction. The criteria set the boundary. An agent can misread a story, but it has much less room to misread a precise criterion.
Use EARS-style phrasing
EARS (Easy Approach to Requirements Syntax) is a set of sentence templates. This chapter uses two of them:
WHEN <event> THEN the system SHALL <response>for behavior triggered by an event.IF <condition> THEN the system SHALL <response>for behavior that depends on a state or an unwanted situation.
Each sentence has a trigger and a response. That structure maps directly onto a test, which has a setup, an action and an assertion.
### 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.Several details in this example are deliberate:
- The numbers are stable. Tasks and tests can cite "Requirement 1.3" and stay accurate.
- Criterion 1 gives a concrete value, 30 minutes, instead of "a short time".
- Criterion 2 covers a security behavior (not revealing which emails are registered) that an agent would probably not infer from the story alone.
- Each criterion describes what the system does, not how it does it. The design document handles the "how".
The testability check
Before you accept a criterion, ask whether two people could run the system and agree on pass or fail. If they could not, rewrite it.
| Weak criterion | Problem | Testable version |
|---|---|---|
| The reset should be fast. | "Fast" has no threshold. | WHEN a user submits a registered email THEN the system SHALL respond within 2 seconds. |
| The link should be secure. | No observable behavior. | IF a reset link has already been used THEN the system SHALL reject any further use of that link. |
| Handle errors gracefully. | Which errors? What response? | IF the email service is unavailable THEN the system SHALL return an error response and SHALL NOT discard the reset request. |
Common errors
- Several behaviors in one criterion. "WHEN a user resets a password THEN the system SHALL email a link and log the event and lock other sessions" cannot fail in a clear way. Split it into three criteria.
- Vague quantities. Words like "quickly", "secure" and "user-friendly" invite the agent to pick its own meaning.
- Implementation in the requirement. "SHALL store tokens in Redis" belongs in the design. Putting it here makes a design choice look like a business need.
- Only the happy path. Agents usually handle the success case unprompted. Spend your effort on expiry, reuse, empty input and unknown users.
- Unstable references. If you renumber criteria after tasks cite them, the traceability breaks.
Check the criteria before the agent starts
Read each criterion as if you had to write its test. If you cannot picture the assertion, the agent cannot either. The next chapters cover the design and task phases, and a later chapter turns these criteria into runnable tests.
Exercise
Rewrite this criterion so it is testable and covers one behavior: "The system should handle failed logins safely."
Show answer
One possible answer: "IF a user enters an incorrect password 5 times in a row THEN the system SHALL reject further login attempts for that account for 15 minutes." It has a trigger (5 failures), a specific response (reject attempts) and a measurable value (15 minutes). A test can submit 5 wrong passwords and check that a sixth attempt is rejected. Other behaviors, such as logging the failure, should become separate criteria.