ai-test-generation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<objective>
LLMs will happily emit fifty plausible-looking tests that assert nothing, target endpoints that do not exist, and duplicate each other. This skill is a staged pipeline that forces structured intermediates — assumptions, coverage matrix, oracle definitions — out of the model BEFORE any test code, so what you get is traceable, reviewable, and grounded in the real codebase instead of ad-hoc generated noise.
Before starting: Check for in the project root. It carries tech stack, test frameworks, naming conventions, selector strategy, and known risk areas that dramatically improve generated test quality.
</objective>
.agents/qa-project-context.md<objective>
大语言模型(LLM)会随意生成50个看似合理但实则毫无断言、指向不存在的端点且彼此重复的测试。本技能是一个分阶段流程,要求模型在生成任何测试代码之前先输出结构化中间产物——假设、覆盖矩阵、验证机制定义,这样你得到的测试是可追溯、可审核且基于真实代码库的,而非随意生成的冗余内容。
开始前准备: 检查项目根目录下的文件。它包含技术栈、测试框架、命名规范、选择器策略以及已知风险区域,能显著提升生成测试的质量。
</objective>
.agents/qa-project-context.mdQuick Route
快速指南
The pipeline is the same for every input; only the Step 1 extraction emphasis changes. Jump to the matching row, then run Steps 2-7 unchanged.
| Input type | Step 1 extracts | Watch for |
|---|---|---|
| PRD / feature spec | Entities, business rules, acceptance criteria, NFRs, stated assumptions | Implicit requirements inferred from "seamless"/"fast" language |
| User story + AC | Each AC → ≥1 happy + ≥1 negative scenario | ACs that hide multiple behaviors in one line |
Code diff ( | New/changed code paths, modified conditionals, removed behavior | Regression scope: test the changed paths, not the whole module |
| Bug report | Repro steps, expected vs actual, environment | Write a test asserting expected — fails now, passes after fix |
| OpenAPI / GraphQL SDL | Endpoints, schemas, required fields, enums, auth | Validation, auth-failure, and edge cases per endpoint, not just 200s |
Playwright projects also pick an agent integration mode — see Discovery Q2.
无论输入类型如何,流程都是相同的;仅第1步的提取重点会变化。跳转到匹配的行,然后按原样执行第2-7步。
| 输入类型 | 第1步提取内容 | 注意事项 |
|---|---|---|
| PRD / 功能规格 | 实体、业务规则、验收标准、非功能性需求(NFR)、明确说明的假设 | 从“无缝”“快速”等表述中推断出的隐含需求 |
| 用户故事 + 验收标准(AC) | 每个AC对应≥1个正向场景 + ≥1个负向场景 | 单个AC中隐藏多种行为的情况 |
代码差异( | 新增/修改的代码路径、修改的条件判断、移除的功能 | 回归测试范围:仅测试变更的路径,而非整个模块 |
| Bug报告 | 复现步骤、预期与实际结果、环境信息 | 编写断言预期结果的测试——当前会失败,修复后会通过 |
| OpenAPI / GraphQL SDL | 端点、Schema、必填字段、枚举、认证 | 每个端点的验证、认证失败及边缘场景,而非仅200成功响应 |
Playwright项目还需选择代理集成模式——请查看发现环节问题2。
Discovery Questions
发现环节问题
Check first — if it exists, use it and skip anything already answered there. Then clarify:
.agents/qa-project-context.md-
What is the input source? PRD / spec, user story + AC, code diff, bug report, or API schema. Determines Step 1 extraction emphasis (see Quick Route). For an LLM/AI feature spec, stop — generate eval datasets in, not Playwright specs.
ai-system-testing -
What is the target test framework, and (for Playwright) which agent integration mode?
- E2E: Playwright (preferred), Cypress. Unit: Jest, Vitest, pytest. API: Playwright , Supertest, requests.
APIRequestContext - Playwright CLI + agents (recommended for Claude Code / Codex / Cursor): scaffolds planner/generator/healer agents into
npx playwright init-agents --loop=claudeas markdown. They are interactive dev tools that produce standard Playwright tests which run unchanged in CI. Token-efficient; runs inside the agent's loop..claude/agents/ - Playwright MCP (): higher overhead, right when the agent must drive a live browser interactively over a long session.
npx @playwright/mcp@latest - Neither — hand-write tests using AI as a scratch-pad helper.
- E2E: Playwright (preferred), Cypress. Unit: Jest, Vitest, pytest. API: Playwright
-
What project context is available? Existing test patterns, Page Objects / helpers, data factories / fixtures, CI constraints (timeout, parallelism). More context = less cleanup.
-
What is the review workflow? Full pipeline → human review → merge (default); scenarios only → human writes code; or code → human refines iteratively.
-
What domain knowledge is needed? Regulated industry (healthcare, finance) compliance, domain invariants (money never negative, appointments cannot overlap), known risk areas from past incidents.
先检查文件——如果存在,直接使用并跳过已涵盖的问题。然后明确以下内容:
.agents/qa-project-context.md-
输入来源是什么? PRD/规格文档、用户故事+验收标准、代码差异、Bug报告或API Schema。这决定了第1步的提取重点(见快速指南)。如果是LLM/AI功能规格文档,请停止——请在中生成评估数据集,而非Playwright规格。
ai-system-testing -
目标测试框架是什么?(针对Playwright)采用哪种代理集成模式?
- 端到端(E2E): Playwright(首选)、Cypress。单元测试: Jest、Vitest、pytest。API测试: Playwright 、Supertest、requests。
APIRequestContext - Playwright CLI + 代理(推荐用于Claude Code/Codex/Cursor):会在
npx playwright init-agents --loop=claude目录中生成规划器/生成器/修复器代理的Markdown文件。它们是交互式开发工具,可生成标准Playwright测试,在CI中无需修改即可运行。令牌效率高;在代理循环内运行。.claude/agents/ - Playwright MCP():开销较高,适用于代理必须在长时间会话中交互式驱动真实浏览器的场景。
npx @playwright/mcp@latest - 都不选——手动编写测试,将AI用作辅助工具。
- 端到端(E2E): Playwright(首选)、Cypress。单元测试: Jest、Vitest、pytest。API测试: Playwright
-
有哪些可用的项目上下文? 现有测试模式、页面对象/辅助工具、数据工厂/夹具、CI约束(超时、并行性)。上下文越丰富,后续清理工作越少。
-
审核工作流是什么? 完整流程→人工审核→合并(默认);仅生成场景→人工编写代码;或生成代码→人工迭代优化。
-
需要哪些领域知识? 受监管行业(医疗、金融)的合规要求、领域不变量(金额不能为负、预约不能重叠)、过往事件中的已知风险区域。
Core Principles
核心原则
-
Pipeline before code. Never generate test code before establishing what to test, why, and how to verify it. The seven-step pipeline exists to prevent premature code generation that targets the wrong things.
-
Structured intermediates are the product. The assumptions document, coverage matrix, and oracle definitions are more valuable than the test code itself. They are reviewable, traceable, and reusable.
-
Separate what from how. Scenario generation (what to test) and oracle design (how to verify) are distinct cognitive tasks. Mixing them produces scenarios biased toward what is easy to assert, with assertions tacked on as afterthoughts.
-
AI generates the first draft; a human reviews and refines. Never ship AI-generated tests without human review. The AI accelerates — it does not replace judgment.
-
Context is everything. Feed the LLM your conventions, existing patterns, selector strategy, and data setup. The more context, the less cleanup.
-
Quality over quantity. Each test has a maintenance cost. Focus on critical paths, complex logic, and known risk areas — not test count.
-
先流程后代码。在确定测试内容、原因以及验证方式之前,绝不要生成测试代码。七步流程的存在是为了避免过早生成针对错误目标的代码。
-
结构化中间产物是核心产出。假设文档、覆盖矩阵和验证机制定义比测试代码本身更有价值。它们可审核、可追溯且可复用。
-
分离测试内容与验证方式。场景生成(测试什么)和验证机制设计(如何验证)是不同的认知任务。将两者混合会导致场景偏向于容易断言的内容,断言则沦为事后补充。
-
AI生成初稿,人工审核优化。绝不要直接发布AI生成的测试。AI只是加速流程——无法替代人工判断。
-
上下文决定一切。向LLM提供你的规范、现有模式、选择器策略和数据设置。上下文越丰富,后续清理工作越少。
-
质量优先于数量。每个测试都有维护成本。专注于关键路径、复杂逻辑和已知风险区域——而非测试数量。
The Pipeline
执行流程
Mandatory workflow — agents MUST follow this order:
Step 1: Extract → Requirements, entities, business rules from input
Step 2: Analyze → Risks, invariants, edge cases, ambiguities
Step 3: Map → Coverage matrix (requirement → scenario → priority)
Step 4: Generate → Candidate scenarios (happy + boundary + negative + security + a11y)
Step 5: Design → Assertions and oracles SEPARATELY from scenarios
Step 6: Code → Test code (only after all above exist)
Step 7: Review → Human review with traceability back to sourceFull prompt templates for every step (extraction, risk analysis, scenario, oracle, code) live in . Below is the shape of each step's output.
references/prompt-patterns.md强制工作流——代理必须遵循此顺序:
Step 1: 提取 → 从输入中提取需求、实体、业务规则
Step 2: 分析 → 风险、不变量、边缘场景、模糊点
Step 3: 映射 → 覆盖矩阵(需求→场景→优先级)
Step 4: 生成 → 候选场景(正向→边界→负向→安全→无障碍)
Step 5: 设计 → 单独于场景的断言和验证机制
Step 6: 编码 → 测试代码(仅在上述所有步骤完成后生成)
Step 7: 审核 → 人工审核,可追溯至原始输入每个步骤(提取、风险分析、场景、验证机制、编码)的完整提示模板位于中。以下是每个步骤输出的格式说明。
references/prompt-patterns.mdStep 1: Extract Requirements and Entities
Step 1: 提取需求与实体
Parse the input into structured elements: Entities (with roles/states/attributes), Business Rules (numbered), Explicit Requirements (, stated in source), and Implicit Requirements (, inferred — flag every one for human confirmation). Separating explicit from inferred is the rule that prevents testing assumptions as if they were specifications.
[REQ-N][IMP-N]将输入解析为结构化元素:实体(包含角色/状态/属性)、业务规则(编号)、明确需求(,来自输入原文)和隐含需求(,推断得出——需标记所有隐含需求供人工确认)。区分明确需求与推断需求是防止将假设当作规格进行测试的关键规则。
[REQ-N][IMP-N]Step 2: Risk Analysis and Invariants
Step 2: 风险分析与不变量
Derive what can go wrong, what must always be true, and where the source is silent.
- Risks — table of (e.g. race condition on stock decrement, email delay > 30s).
Risk | Likelihood | Impact | Source Requirement - Invariants (must ALWAYS hold) — ,
stock >= 0,order total = sum(items) + tax + shipping.user sees only their own orders - Ambiguities (need human answers) — "Does free shipping apply before or after discount codes?" Capture these explicitly; do not silently pick one.
- Edge cases derived from risks — two users buy the last item, payment succeeds but email service is down.
推导可能出现的问题、必须始终成立的规则以及输入未明确说明的内容。
- 风险 — 表格格式:(例如:库存递减时的竞态条件、邮件延迟超过30秒)。
风险 | 可能性 | 影响 | 来源需求 - 不变量(必须始终成立) — 、
库存 >= 0、订单总额 = 商品总和 + 税费 + 运费。用户仅能查看自己的订单 - 模糊点(需人工解答) — “免运费是在折扣码之前还是之后适用?” 需明确记录,不要自行选择。
- 从风险推导的边缘场景 — 两位用户购买最后一件商品、支付成功但邮件服务故障。
Step 3: Coverage Matrix
Step 3: 覆盖矩阵
The single most important artifact — it prevents both gaps and duplicates. Map every requirement to scenarios with category, priority, and oracle type:
| Requirement | Scenario | Category | Priority | Oracle Type |
|---|---|---|---|---|
| REQ-1 | Add single item to empty cart | Happy path | P0 | State: cart count = 1 |
| REQ-1 | Add out-of-stock item | Negative | P0 | UI: error message, cart unchanged |
| REQ-2 | Complete checkout with valid card | Happy path | P0 | State: order created, stock decremented |
| REQ-2 | Two users checkout last item | Race condition | P1 | One succeeds, one gets stock error |
| INV-1 | Stock never goes negative | Invariant | P0 | Data: stock >= 0 after any operation |
After building it, verify: every requirement has ≥1 happy and ≥1 negative scenario; every invariant has a direct test; every Step-2 risk has a scenario; no two rows test the same thing.
这是最重要的产物——它能防止测试遗漏和重复。将每个需求映射到场景,包含类别、优先级和验证机制类型:
| 需求 | 场景 | 类别 | 优先级 | 验证机制类型 |
|---|---|---|---|---|
| REQ-1 | 向空购物车添加单个商品 | 正向路径 | P0 | 状态:购物车数量 = 1 |
| REQ-1 | 添加缺货商品 | 负向场景 | P0 | UI:显示错误消息,购物车无变化 |
| REQ-2 | 使用有效卡片完成结账 | 正向路径 | P0 | 状态:订单已创建,库存已递减 |
| REQ-2 | 两位用户结账最后一件商品 | 竞态条件 | P1 | 一位成功,一位收到库存不足错误 |
| INV-1 | 库存绝不会为负 | 不变量 | P0 | 数据:任何操作后库存 >= 0 |
构建完成后,验证:每个需求至少有1个正向和1个负向场景;每个不变量都有对应的测试;每个Step-2的风险都有对应的场景;没有两行测试相同的内容。
Step 4: Generate Candidate Scenarios
Step 4: 生成候选场景
For each matrix row, write the full scenario in Given/When/Then with explicit test-data requirements (). Cover these categories systematically:
Given: user with 99 items in cart (max 100); When: adds one more; Then: count = 100| Category | Description |
|---|---|
| Happy path | The user does exactly what the feature is designed for |
| Boundary | Edge of valid input ranges — use the BOUNDARIES framework ( |
| Negative | Invalid inputs, unauthorized actions |
| Security | Auth bypass, injection, privilege escalation |
| Accessibility | Screen reader, keyboard-only, contrast |
| State transition | Valid and invalid moves between states |
| Concurrency | Two users acting simultaneously |
针对矩阵中的每一行,用Given/When/Then格式编写完整场景,并明确测试数据要求()。系统覆盖以下类别:
Given: 购物车中有99件商品(上限100);When: 再添加一件;Then: 数量 = 100| 类别 | 描述 |
|---|---|
| 正向路径 | 用户完全按照功能设计进行操作 |
| 边界场景 | 有效输入范围的边缘——使用BOUNDARIES框架( |
| 负向场景 | 无效输入、未授权操作 |
| 安全场景 | 认证绕过、注入攻击、权限提升 |
| 无障碍场景 | 屏幕阅读器、纯键盘操作、对比度 |
| 状态转换 | 有效和无效的状态切换 |
| 并发场景 | 两位用户同时操作 |
Step 5: Design Assertions and Oracles
Step 5: 设计断言与验证机制
Deliberately separate from Step 4. Scenarios describe behavior; oracles describe how to verify it. For each scenario, define oracles across categories — a single assertion is rarely enough to prove a behavior:
| Oracle category | Asserts | Example |
|---|---|---|
| UI state | Visible text / element state | |
| Data | Persisted state via API/DB | |
| Negative | What should NOT happen | no error toast; no navigation away |
| Side effect | Async/external outcomes | analytics |
Oracle quality rules: assert business outcomes not implementation details; use the most specific assertion available (, not ); include negative assertions; verify data integrity, not just UI; assert accessibility (focus management, live-region announcements).
toHaveText('$29.99')toBeTruthy()务必与Step 4分离。场景描述行为;验证机制描述如何验证行为。针对每个场景,跨类别定义验证机制——单个断言通常不足以证明行为正确:
| 验证机制类别 | 断言内容 | 示例 |
|---|---|---|
| UI状态 | 可见文本/元素状态 | |
| 数据 | 通过API/DB持久化的状态 | |
| 负向验证 | 不应发生的情况 | 无错误提示框;未跳转页面 |
| 副作用 | 异步/外部结果 | 触发了 |
验证机制质量规则: 断言业务结果而非实现细节;使用最具体的断言(,而非);包含负向断言;验证数据完整性,而非仅UI;断言无障碍性(焦点管理、实时区域通知)。
toHaveText('$29.99')toBeTruthy()Step 6: Generate Test Code
Step 6: 生成测试代码
Only after Steps 1-5 produce reviewed artifacts. Code is a mechanical translation of scenarios + oracles into framework syntax, with traceability comments linking back to the requirement and scenario:
typescript
/**
* Scenario: SC-001 — Add single item to empty cart
* Requirement: REQ-1 (User can add items to cart)
* Priority: P0
*/
test('add single item to empty cart', async ({ page, testProduct }) => {
await page.goto(`/products/${testProduct.id}`); // Given
await page.getByRole('button', { name: 'Add to cart' }).click(); // When
await expect(page.getByTestId('cart-badge')).toHaveText('1'); // Then
await expect(page.getByTestId('error-toast')).not.toBeVisible(); // Negative oracle
});Code generation rules: match project conventions (from ); reuse existing Page Objects, fixtures, and data factories; include traceability comments (, ); follow the project's selector strategy; put setup/teardown in fixtures, not inline.
qa-project-context.mdScenario: SC-XXXRequirement: REQ-XX仅在Step 1-5产出审核通过的产物后执行。代码是场景+验证机制到框架语法的机械转换,包含可追溯至需求和场景的注释:
typescript
/**
* Scenario: SC-001 — Add single item to empty cart
* Requirement: REQ-1 (User can add items to cart)
* Priority: P0
*/
test('add single item to empty cart', async ({ page, testProduct }) => {
await page.goto(`/products/${testProduct.id}`); // Given
await page.getByRole('button', { name: 'Add to cart' }).click(); // When
await expect(page.getByTestId('cart-badge')).toHaveText('1'); // Then
await expect(page.getByTestId('error-toast')).not.toBeVisible(); // Negative oracle
});代码生成规则: 匹配项目规范(来自);复用现有页面对象、夹具和数据工厂;包含可追溯注释(、);遵循项目的选择器策略;将设置/清理逻辑放在夹具中,而非内联编写。
qa-project-context.mdScenario: SC-XXXRequirement: REQ-XXStep 7: Human Review
Step 7: 人工审核
Not optional — a mandatory pipeline step. This reviews the tests this pipeline just generated, before they merge. (To audit a pre-existing suite you did not just generate, use instead.) Run every generated test against this checklist:
ai-qa-review- Traces to requirement: test → scenario → coverage row → requirement is followable.
- Tests behavior, not implementation: survives a harmless refactor.
- Correct abstraction level: right test type (unit vs integration vs E2E).
- Test naming and readability: the test name states the behavior; a reader sees intent without decoding the body.
- Test isolation / no shared state: the test creates and cleans up its own data, holds no order dependency on sibling tests, and passes when run alone or in any order.
- Realistic test data: plausible, diverse, using .
example.com - Meaningful assertions: matches the oracle definition; specific, not .
toBeTruthy() - Matches project conventions: naming, structure, selector strategy.
- No flakiness risks: no hardcoded timeouts, race conditions, or order dependence.
- Edge cases included: goes beyond the happy path.
- Assumptions validated: Step-2 ambiguities were resolved before coding.
Review outcome per test: KEEP (merge as-is) · MODIFY (fix listed issues, then merge) · REJECT (wrong requirement, wrong abstraction, hallucinated API) · DEFER (blocked on ambiguity).
非可选——这是流程中的强制步骤。审核本流程刚生成的测试,然后再合并。(若要审核非本次生成的现有测试套件,请使用。)针对每个生成的测试,对照以下清单检查:
ai-qa-review- 可追溯至需求: 测试→场景→覆盖矩阵行→需求的链路清晰可查。
- 测试行为而非实现: 在无破坏性重构时仍能通过。
- 正确的抽象级别: 测试类型正确(单元测试vs集成测试vs端到端测试)。
- 测试命名与可读性: 测试名称明确说明行为;读者无需解读代码主体即可理解意图。
- 测试隔离/无共享状态: 测试创建并清理自己的数据,与其他测试无顺序依赖,单独运行或任意顺序运行均可通过。
- 真实的测试数据: 合理、多样,使用相关数据。
example.com - 有意义的断言: 匹配验证机制定义;具体明确,而非。
toBeTruthy() - 符合项目规范: 命名、结构、选择器策略均匹配。
- 无不稳定风险: 无硬编码超时、竞态条件或顺序依赖。
- 包含边缘场景: 不止覆盖正向路径。
- 假设已验证: Step-2中的模糊点已在编码前解决。
每个测试的审核结果: 保留(直接合并)· 修改(修复列出的问题后合并)· 拒绝(需求错误、抽象错误、幻觉API)· 推迟(因模糊点受阻)。
Guardrails
防护规则
Hard rules. Agents MUST follow them.
- Code before coverage is forbidden. Never emit test code before Steps 1-3 (requirements, risk analysis with documented assumptions, coverage matrix) exist. If an agent skips to code: STOP, go back.
- Assert outcomes, not implementation. , not
expect(screen.getByRole('progressbar')).toBeVisible().expect(component.state.isLoading).toBe(true), notexpect(page.getByTestId('cart-badge')).toHaveText('1').expect(store.dispatch).toHaveBeenCalledWith(...) - Scenarios (Step 4) before oracles (Step 5), always. Scenario = WHAT happens; oracle = HOW to verify. Mixing them biases scenarios toward easy assertions.
- Always produce the intermediates — assumptions document, uncovered ambiguities, oracle candidates, and the traceability chain — even in abbreviated form.
Flag these when detected:
- Hallucinated APIs — endpoints, selectors, methods, or imports that do not exist in the codebase. Verify mechanically (see Verification) before human review.
- Duplicate scenarios — same behavior, trivially different data. Consolidate or parametrize.
- Low-value assertions — ,
expect(response).toBeTruthy().expect(page).toHaveURL(/.*/) - Missing negative cases — if every scenario is a happy path, the coverage matrix is incomplete.
- Unrealistic test data — ,
test@test.com,John Doe. Use diverse, plausible data onpassword123.example.com
硬性规则。代理必须遵守。
- 禁止先编码后覆盖。在Step 1-3(需求、带文档化假设的风险分析、覆盖矩阵)完成之前,绝不能生成测试代码。如果代理跳过直接编码:停止,返回重新执行。
- 断言结果而非实现。,而非
expect(screen.getByRole('progressbar')).toBeVisible()。expect(component.state.isLoading).toBe(true),而非expect(page.getByTestId('cart-badge')).toHaveText('1')。expect(store.dispatch).toHaveBeenCalledWith(...) - 始终先场景(Step4)后验证机制(Step5)。场景=发生了什么;验证机制=如何验证。混合两者会导致场景偏向于容易断言的内容。
- 始终生成中间产物——假设文档、未解决的模糊点、候选验证机制以及可追溯链路,即使是简化形式也需生成。
检测到以下情况时需标记:
- 幻觉API——代码库中不存在的端点、选择器、方法或导入。在人工审核前需通过机械方式验证(见验证环节)。
- 重复场景——行为相同,仅数据略有不同。需合并或参数化。
- 低价值断言——、
expect(response).toBeTruthy()。expect(page).toHaveURL(/.*/) - 缺少负向场景——如果所有场景都是正向路径,则覆盖矩阵不完整。
- 不真实的测试数据——、
test@test.com、John Doe。使用password123上多样、合理的数据。example.com
Model selection per step
各步骤模型选择
Route by difficulty, not habit. Use a cheap model for mechanical extraction (Step 1) and the coverage-matrix bookkeeping (Step 3) — Haiku 4.5 or Sonnet 4.6 are plenty. Escalate to Opus 4.8 for oracle design (Step 5) and hallucination-sensitive code generation (Step 6), where a wrong inference is expensive; reach for Fable 5 only on genuinely hard reasoning (subtle invariants, regulated-domain logic). Running the strongest model on every step is wasteful; running the cheapest on Step 6 produces fabricated APIs.
根据难度选择模型,而非习惯。机械提取(Step1)和覆盖矩阵记录(Step3)使用低成本模型即可——Haiku 4.5或Sonnet 4.6足够。验证机制设计(Step5)和对幻觉敏感的代码生成(Step6)需升级到Opus 4.8,错误推断的代价很高;仅在真正复杂的推理(微妙的不变量、受监管领域逻辑)时才使用Fable 5。每个步骤都用最强模型是浪费;在Step6使用最便宜模型会生成伪造API。
Verification
验证环节
Convert the "hallucinated APIs" warning into a mechanical gate. After Step 6, before human review:
- Resolve imports / types. TypeScript: — fabricated imports and wrong signatures fail here. Python:
npx tsc --noEmitorpython -m pyflakes <files>.ruff check - Grep generated selectors/endpoints against the codebase. Confirm every id and every API path the test calls actually exists in source:
getByTestId('...')bashgrep -roE "getByTestId\('([^']+)'\)" generated/ | sed -E "s/.*'([^']+)'.*/\1/" | sort -u \ | while read id; do grep -rq "$id" src/ || echo "MISSING testid: $id"; done - Run the suite once. Tests that reference nonexistent routes/selectors fail fast; quarantine those before review rather than reviewing dead code.
Any line or error is a hallucination to fix before a human spends review time.
MISSINGtsc将“幻觉API”警告转化为机械校验关卡。在Step6之后、人工审核之前:
- 解析导入/类型。TypeScript:——伪造的导入和错误的签名会在此处失败。Python:
npx tsc --noEmit或python -m pyflakes <files>。ruff check - 在代码库中搜索生成的选择器/端点。确认测试中调用的每个和API路径确实存在于源代码中:
getByTestId('...')bashgrep -roE "getByTestId\('([^']+)'\)" generated/ | sed -E "s/.*'([^']+)'.*/\1/" | sort -u \ | while read id; do grep -rq "$id" src/ || echo "MISSING testid: $id"; done - 运行一次测试套件。引用不存在的路由/选择器的测试会快速失败;在审核前隔离这些测试,而非审核无效代码。
任何行或错误都是需要在人工审核前修复的幻觉问题。
MISSINGtscAnti-Patterns
反模式
- Skipping to code. The most common failure: an agent gets a PRD and immediately writes tests. Without the coverage matrix it misses scenarios and duplicates others. The pipeline exists to prevent this.
- Asserting implementation instead of behavior. breaks on any refactor. Assert
expect(component.state.isLoading).toBe(true)— what the user observes.expect(screen.getByRole('progressbar')).toBeVisible() - Mixing scenarios and assertions. Writing "test this thing and check this value" as one step. Separate what to test from how to verify it.
- No project context in the prompt. Without conventions and existing patterns, you get generic tests. exists for exactly this.
qa-project-context.md - Over-generating. AI will write 50 tests for a simple function. Each carries maintenance cost. Use the coverage matrix to bound generation to meaningful scenarios.
- Copy-paste without understanding. If you cannot explain what a generated test does and why, do not merge it. Tests you do not understand become tests you cannot debug.
- Shipping without review. Step 7 is not optional. AI tests routinely contain hallucinated APIs, wrong selectors, incorrect business logic, and flakiness only human review catches.
- Ignoring the feedback loop. When AI tests catch real bugs, note the prompt patterns that worked; when they false-positive, note what went wrong. Build a project-specific library of what works.
- 直接跳转到编码。最常见的错误:代理拿到PRD后立即编写测试。没有覆盖矩阵会导致场景遗漏和重复。流程的存在就是为了防止这种情况。
- 断言实现而非行为。会在任何重构时失效。应断言
expect(component.state.isLoading).toBe(true)——用户能观察到的内容。expect(screen.getByRole('progressbar')).toBeVisible() - 混合场景与断言。将“测试这个内容并检查这个值”写成一步。需将测试什么与如何验证分离。
- 提示中不包含项目上下文。没有规范和现有模式,会得到通用测试。正是为此而生。
qa-project-context.md - 过度生成。AI会为简单函数编写50个测试。每个测试都有维护成本。使用覆盖矩阵限制生成有意义的场景。
- 复制粘贴而不理解。如果你无法解释生成的测试做了什么以及为什么,不要合并。你不理解的测试会成为无法调试的测试。
- 未审核就发布。Step7不是可选步骤。AI生成的测试通常包含幻觉API、错误选择器、不正确的业务逻辑以及只有人工审核才能发现的不稳定问题。
- 忽略反馈循环。当AI测试发现真实Bug时,记录有效的提示模式;当出现误报时,记录问题所在。构建项目特定的有效模式库。
Done When
完成标准
- All seven artifacts exist: requirements document, risk & invariants, coverage matrix, scenario set, oracle definitions, test code, and review notes with a KEEP/MODIFY/REJECT/DEFER decision per test.
- The coverage matrix was produced and reviewed before any test code file was written.
- Verification passed: (or language equivalent) exits 0 and the selector/endpoint grep reports zero
tsc --noEmitlines.MISSING - Each generated test has a recorded human review decision; no test is marked KEEP without one.
- The suite's CI job exits 0 (green).
- Reproducibility metadata recorded: the exact model ID (e.g. ,
claude-opus-4-8,claude-sonnet-4-6), input source hash, and the version of any skill / CLI / MCP server invoked.claude-haiku-4-5-20251001
- 存在所有七个产物:需求文档、风险与不变量、覆盖矩阵、场景集、验证机制定义、测试代码以及每个测试带有保留/修改/拒绝/推迟决策的审核记录。
- 在编写任何测试代码文件之前,已生成并审核覆盖矩阵。
- 验证通过:(或对应语言的等效命令)返回0,选择器/端点搜索报告无
tsc --noEmit行。MISSING - 每个生成的测试都有记录在案的人工审核决策;没有测试在无决策的情况下被标记为保留。
- 测试套件的CI任务返回0(绿色)。
- 记录可复现性元数据:精确的模型ID(例如、
claude-opus-4-8、claude-sonnet-4-6)、输入源哈希以及调用的任何技能/CLI/MCP服务器的版本。claude-haiku-4-5-20251001
Related Skills
相关技能
- qa-project-context — Set up the context file that makes AI test generation dramatically better. Configure this first.
- playwright-automation — Deep Playwright patterns (POM, fixtures, CI), plus the Test Agents (, scaffolded into
init-agents --loop=claude) and.claude/agents/modes chosen in Discovery Q2. Generated tests live inside this framework.@playwright/mcp - unit-testing — Jest, Vitest, pytest patterns for unit-level generated tests.
- api-testing — Endpoint test patterns for tests generated from OpenAPI specs.
- test-strategy — Decide what to test and at which level before generating.
- test-reliability — Make generated tests reliable: flake classification, healing, video receipts.
- ai-system-testing — When the input is an LLM feature spec, generate eval datasets here (Promptfoo, DeepEval, Ragas, Braintrust) instead of Playwright specs.
- ai-qa-review — Audit a pre-existing suite you did not just generate (test smells, testability). Step 7 here only reviews this pipeline's own output.
- ai-bug-triage — When generated tests find bugs, classify and report them through the triage pipeline.
- qa-project-context — 设置上下文文件,大幅提升AI测试生成质量。请先配置此技能。
- playwright-automation — 深入的Playwright模式(POM、夹具、CI),以及发现环节问题2中选择的Test Agents(,生成到
init-agents --loop=claude)和.claude/agents/模式。生成的测试在此框架内运行。@playwright/mcp - unit-testing — 针对单元级生成测试的Jest、Vitest、pytest模式。
- api-testing — 针对从OpenAPI规格生成的测试的端点测试模式。
- test-strategy — 在生成测试前决定测试什么以及在哪个层级测试。
- test-reliability — 提升生成测试的可靠性:不稳定测试分类、修复、视频记录。
- ai-system-testing — 当输入是LLM功能规格文档时,请在此生成评估数据集(Promptfoo、DeepEval、Ragas、Braintrust),而非Playwright规格。
- ai-qa-review — 审核非本次生成的现有测试套件(测试异味、可测试性)。本流程的Step7仅审核本流程的输出。
- ai-bug-triage — 当生成的测试发现Bug时,通过分类流程进行分类和报告。
Reference Files (in references/
)
references/参考文件(位于references/
)
references/- prompt-patterns.md — Full prompt library aligned to the seven steps: extraction, risk analysis, scenario generation, oracle design, and code generation prompts, plus the BOUNDARIES edge-case framework used in Step 4.
- prompt-patterns.md — 与七步流程对齐的完整提示库:提取、风险分析、场景生成、验证机制设计和代码生成提示,以及Step4中使用的BOUNDARIES边缘场景框架。