Write test cases and automation scripts with AI

Updated 2026-11-26 20 分钟(含跑通) Difficulty ★★☆☆☆Author Skills 编辑部

Skills used in this guide (3)

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

  1. Code under test: the function / module / interface definition.
  2. Behavior contract: input/output boundaries, error branches, expectations.
  3. 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):

ClientDirectory
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

text
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

text
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:

text
Write one Playwright E2E: login through successful checkout.
Assert only key states (success toast, order id generated), not every element.

Pitfalls

PitSymptomFix
Fake objectAI invents nonexistent fnGive real code, don't let it invent
Only happy pathEdges missedForce empty/range/error coverage
Too many assertsDon't know what failedOne behavior per test
Green = correctAsserted wrong wayYou define "correct"
E2E too wideBrittle, slowOnly 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:

相关源码

一句话结论

结论:测试的价值在"抓住真会出事的路径"——给 AI 真实代码或行为约定,让它补边界用例,别让它凭空造被测对象。

References

Related Scenes