From User Stories to Test Scenarios and Test Code
Can you write a test scenario before the code exists?
Section titled âCan you write a test scenario before the code exists?âThe Given-When-Then acceptance criteria built in this seriesâ third article form the skeleton of a test scenario. But trying to go straight from AC to runnable test code in one shot causes a problem: AI plausibly fills in API names that donât exist yet, error codes that havenât been decided yet, and exception-handling behavior that hasnât been implemented yet. The same âstate when information is missingâ principle from the fourth article applies here too.
The fix is to split test scenario writing and test code writing into two separate passes.
Two-pass test design
Section titled âTwo-pass test designâPass 1: document-first: before code exists
Section titled âPass 1: document-first: before code existsâThe goal at this stage isnât runnable code: itâs listing exhaustively what needs to be verified. The only input is the user story and AC; the API spec or actual field names may not be decided yet. In that case, use placeholders instead of real values, and state that fact explicitly in the scenario.
TC-UNIT-001: Prevent duplicate team name registration- Target: [registration handler function] (placeholder â API spec not finalized)- Input: [team name field] matching an already-registered team name- Expected result: [duplicate-error status code] (exact code TBD at API design time â open item)- Verification items: - [ ] Does the duplicate-name detection logic run? - [ ] Is registration actually rejected on a duplicate (not persisted to DB)? - [ ] Is the error returned in a form the user can understand?This scenario isnât runnable yet, but it already answers âwhat must not be missed.â The reason an SQE (software quality engineer) or the team reviews the Pass 1 output is to catch coverage gaps before code exists, finding them after code exists costs far more rework.
Pass 2: code-grounded: after code exists
Section titled âPass 2: code-grounded: after code existsâOnce implementation is done, Pass 1âs placeholders get replaced with actual code-grounded values. The context package (see the fourth article) given to AI at this stage must include the actual source code.
[Example Pass 2 context package]Task: Refine Pass 1 scenario TC-UNIT-001 into runnable test codeContext: - Original Pass 1 scenario (TC-UNIT-001 above) - Actual implementation code: registrationController.ts, registrationService.ts - Actual error response shape: { status: 409, code: "TEAM_NAME_DUPLICATE" }Constraints: - Do not delete or narrow the verification items defined in Pass 1 - Use names and formats from the actual implementation, but retain unimplemented AC as failures or Open Items rather than deleting themAcceptance: - All 3 verification items tracked as assertions or Open Items - Target execution result: PASS (track unresolved Open Items separately)When AI reads the code in Pass 2 and fills in the actual status code (409) and actual error shape (TEAM_NAME_DUPLICATE) to concretize the Pass 1 scenario, the result is a runnable test like this.
test('returns 409 and TEAM_NAME_DUPLICATE when registering with a duplicate team name', async () => { await createRegistration({ teamName: 'ExistingTeam' }); const res = await createRegistration({ teamName: 'ExistingTeam' }); expect(res.status).toBe(409); expect(res.body.code).toBe('TEAM_NAME_DUPLICATE'); const count = await countRegistrationsByTeamName('ExistingTeam'); expect(count).toBe(1); // confirm no duplicate was persisted});This test checks the duplicate response and persistence only; the user-facing error contract remains an unresolved Open Item. PASS in the Acceptance example above is a target, not an actual execution result.
What goes wrong without the two-pass split
Section titled âWhat goes wrong without the two-pass splitâIf you generate test code directly from AC without splitting passes, AI plausibly guesses at API response shapes and status codes that donât exist. Tests generated this way compile and even appear to pass at first, but theyâre actually verifying behavior AI imagined, not what the code does. They become powerless tests that fail to catch real bugs later.
Why the SQE review canât be skipped
Section titled âWhy the SQE review canât be skippedâPass 1 and Pass 2 each have a review checkpoint, and they exist for different reasons.
[Pass 1 SQE review â before code exists]- Purpose: catch coverage gaps before code is written- Checks: are any scenarios missing, is priority appropriate
[Pass 2 SQE review â after code exists]- Purpose: confirm the tests actually verify the code's correctness- Checks: are assertions grounded in actual code behavior rather than imagination, has Pass 1's verification scope been narrowedThe faster AI can produce test scenarios and test code, the more important it becomes for a human to confirm what that output is actually verifying.
Next article
Section titled âNext articleâWeâve now covered the artifact chain from requirements all the way to test code. The final article covers the last piece of this chain, where AI can help turn finished code and test results into a PR, and where things like business, security, and release decisions can never be delegated.