AI agent test generation workflow
Use AI agents to generate focused unit, integration and Playwright tests from behavior and risk instead of snapshots.

Put it into practice
What you will produce
A focused set of tests for normal, boundary and failure behavior with deterministic fixtures and no production dependencies.
- 01
Write the behavior contract
Describe observable input, output, side effect, and failure behavior before generating cases.
Expected result: Tests target a contract rather than implementation details.
- 02
Choose meaningful cases
Select normal, boundary, and failure cases from the contract using deterministic local fixtures.
Expected result: The suite covers distinct behavior without production dependencies.
- 03
Inspect assertion strength
Check that assertions catch plausible regressions instead of mirroring internals or snapshots.
Expected result: Each test can detect a contract-level change.
- 04
Mutation-check the key behavior
Temporarily alter the relevant result locally, confirm the test fails, restore, and rerun.
Expected result: The key assertion catches the targeted regression.
Prompt an agent to derive tests from behavior
Behavior contract:
- Given: [valid input and starting state]
- When: [user-visible action]
- Then: [observable result]
- Side effects: [writes, network calls, events, or none]
- Failure behavior: [invalid input, missing access, dependency failure]
Ask the agent to:
1. Propose normal, boundary, and failure cases before editing.
2. Map each case to one contract statement.
3. Use deterministic local fixtures.
4. Explain which plausible regression each assertion catches.
5. Add tests only; do not change production code.Replace the bracketed text with one behavior from the repository. Review the proposed cases against the contract, then ask for the smallest missing test. A green suite is not evidence for an untested condition; deliberately break the key behavior locally and confirm the assertion fails before restoring it.
Acceptance checks
- Tests target a contract rather than implementation details.
- Each test can detect a contract-level change.
- The key assertion catches the targeted regression.
Limits and trade-offs
More tests do not automatically mean better coverage. Prefer assertions on observable behavior and avoid brittle snapshots.
Official sources
Generate useful tests
Related guides
Building AI agents that actually work
Most 'agents' are brittle pipelines dressed up with AI. Here's the anatomy of a reliable agent, three patterns that hold up in production, and when to use n8n vs code.
Everyone is building agents. Most of them break on the third run. One common failure mode is asking the model to handle too many responsibilities without clear checks. Architecture, model behavior and external dependencies can all contribute to failures. Here's what I've learned building agents that actually run week after week. The anatomy of a reliable agent Every agent that works has three parts, and only one of them should involve AI. Trigger something deterministic starts the agent. A schedule, a webhook, a file drop, a form submission. Not "whenever the AI decides it's time." Context the agent gathers what it needs before calling the AI. The document, the data, the user preferences, the constraints. The AI call happens with full context, not halfway through discovery. Action after the AI produces output, something deterministic happens with it. It gets saved, sent, logged, or published. The action step doesn't retry or…
Read the guideClaude Code Subagents: Parallel Agents
Learn when Claude Code subagents help, how to split parallel work, and how to configure a focused custom subagent without creating conflicts.
Subagents are useful when a task can be split cleanly: one agent searches the authentication code, another traces the database path, and the main conversation compares what they found. They add little to "read this file" or to work where every step depends on the same context. Short version: delegate independent research or a separate review when the results are worth the extra context and usage. Give each agent a narrow scope, the files or area to inspect, and a specific report format. Keep dependent work in the main conversation. Claude Code agents vs subagents Claude Code's documentation calls delegated workers subagents and uses Agent for the tool that starts them. When people search for Claude Code agents in this context, they usually mean subagents: workers that handle a delegated task in their own context and return a result to the main conversation. Claude Code's subagent documentation uses this terminology.…
Read the guide