Write test cases and automation scripts with AI
Skills used in this guide (3)
写新功能、修 bug 或重构时使用:强制测试驱动开发,单元、集成与端到端测试合计覆盖率 80% 以上。
npx skills add affaan-m/ECC -gPlaywright 端到端测试模式:Page Object Model、配置、CI/CD 接入、产物管理与 flaky 测试治理。
npx skills add affaan-m/ECC -g测试驱动开发。当用户想测试优先地构建功能或修 bug、提到红-绿-重构,或想要集成测试时使用。
npx skills add mattpocock/skills -g
The biggest value of testing isn't "high coverage" — it's "catching the paths that actually break". The point of AI-generated tests: give it the implemented code or a clear behavior contract, then have it add edge cases. Don't let it invent the thing under test; a test that passes on a fake object proves nothing.
Who this is for / not for
For:
- Need unit tests / edge cases for existing functions / interfaces
- Want TDD but don't know which test to write first
- Want end-to-end (E2E) tests for key user paths
Not for:
- Code whose logic isn't decided yet (define behavior first)
- Asking AI to decide "what's correct" (you define correctness)
- Treating coverage as a KPI (green ≠ correct)
Before you start
- Code under test: the function / module / interface definition.
- Behavior contract: input/output boundaries, error branches, expectations.
- Framework: unit (Vitest/Jest/pytest), E2E (Playwright).
Steps
1. Install the skill
tdd-workflow is a test-driven workflow skill. Copy its folder into your client's skills directory (same as any Agent Skill):
| Client | Directory |
|---|---|
| Claude Code | ~/.claude/skills/ |
| Codex | ~/.codex/skills/ |
| Cursor | ~/.cursor/skills/ |
| Trae | ~/.trae/skills/ |
| CodeBuddy | ~/.codebuddy/skills/ |
| Tongyi Lingma | ~/.lingma/skills/ |
2. Give behavior, then let AI write tests
From this function, write Vitest tests:
- cover: normal input, empty, out-of-range, wrong type (one each)
- one assertion per test, name states the expectation
- don't change the implementation, only add the test file
List the 3 riskiest edges first, then write.3. TDD: red before green
Using tdd-workflow: first write a failing test (red) for "cart checkout",
describing the expected behavior; after I implement, make it pass (green),
then propose one safe refactor.4. Add E2E for key paths
Use e2e-testing for Playwright on the 2-3 highest-cost paths, not clicking everything:
Write one Playwright E2E: login through successful checkout.
Assert only key states (success toast, order id generated), not every element.Pitfalls
| Pit | Symptom | Fix |
|---|---|---|
| Fake object | AI invents nonexistent fn | Give real code, don't let it invent |
| Only happy path | Edges missed | Force empty/range/error coverage |
| Too many asserts | Don't know what failed | One behavior per test |
| Green = correct | Asserted wrong way | You define "correct" |
| E2E too wide | Brittle, slow | Only key paths |
FAQ
No framework yet?
Yes — have AI use your project's; if none, pick one first.
Trust AI tests as-is?
Run once to confirm they actually failed (red), then go green; avoid "fake green".
Must test first in TDD?
Recommended. Red-green lets behavior be defined before implementation.
How much E2E?
Only the highest-cost paths; clicking all is brittle and slow.
Low coverage?
Add edges and error branches first; more useful than normal-path padding.
Related guides
Continue across these scenes:
相关源码
- affaan-m/ECC · tdd-workflow source
- affaan-m/ECC · e2e-testing source
- mattpocock/skills · tdd source
- Agent Skill open format spec
一句话结论
结论:测试的价值在"抓住真会出事的路径"——给 AI 真实代码或行为约定,让它补边界用例,别让它凭空造被测对象。
References
Related Scenes
Guard code quality with AI (translated)
AI writes fast, but the quality gate matters more. Community consensus in three: reverse TDD (AI writes tests, then minimal impl), zero-warning gate (`--max-warnings=0`, warnings silently become the new baseline), review as a gate (every change through the same gate). Make AI output verifiable and g
Get an AI code review worth reading
The classic failure mode is twenty "suggestions" where eighteen are style preferences. A useful review contains only two kinds of information: what will break, and what will confuse the next reader. So the job is not to make AI say more. It is to forbid everything else.