关于 qe-test-generation
qe-test-generation 指导质量工程团队依据代码分析和模式生成测试,并支持多种测试框架。它强调先编写可跨实现保留的持久测试,再补充临时测试和线上行为检查。
主要功能
- 01依据代码结构和错误路径生成测试
- 02按模式为目标文件生成测试
- 03依据覆盖缺口补充测试
- 04区分持久、临时和线上测试
适用场景
- 01为新模块生成边界契约测试
- 02为未覆盖分支和错误路径补测
- 03在 Jest、Vitest 或 Pytest 等框架中生成测试
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ proffesor-for-testing/ agentic-qe --skill qe-test-generation安装后在 Claude Code 中输入/qe-test-generation确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
config.jsonevals/qe-test-generation.yamlschemas/output.jsonscripts/validate-config.jsontemplates/test-scaffold-jest.md详细介绍
QE 测试生成
目的
介绍如何使用 v3 的 AI 驱动测试生成功能,包括基于模式的测试合成、多框架支持,以及基于代码分析智能推导测试用例。
优先编写持久测试(核心规则)
当 AI 让代码可以低成本重新生成时,持久资产就是实现被丢弃并重写后依然有效的测试。按持久性层级生成测试,并优先编写持久性最高的测试(ADR-113):
| 层级 | 内容 | 重写后仍有效? | 何时编写 |
|---|---|---|---|
| 持久 | 不变量、契约/模式、基于属性的测试、行为端到端测试——针对模块的公共边界进行规范 | 是 | 始终编写——每个目标至少 1 个 |
| 临时 | 基于示例的单元测试、模拟调用/交互测试(TDD-London 风格) | 否(与实现耦合) | 用于红绿循环;标注后可随时删除 |
| 在线 | 针对真实环境运行的监控/漂移/成本断言 | 持续有效 | 用于已部署行为 |
语言切换启发式: 如果用另一种语言重新实现此模块会使测试失效,那么测试就处于错误的边界。 将测试提升到更高层级——断言可观察到的契约,而不是当前代码碰巧采用的工作方式。
每个生成的目标都必须至少包含一个持久断言(不变量、契约检查或属性)。模拟调用断言(toHaveBeenCalledWith)按定义属于临时测试——绝不能让它们成为目标的唯一测试。为每个生成的测试添加 // @tier durable|ephemeral|live 标记,明确其生命周期。
这些测试会作为预言机进行评估:好的测试在真实代码上通过,在植入了缺陷(变异体)的代码上失败。不会断言任何内容或只测试成功路径的测试无法杀死任何变异体,因此会被拒绝——请参阅 /mutation-testing 和 ADR-113。
激活
- 为新代码生成测试时
- 提高测试覆盖率时
- 在不同框架间迁移测试时
- 应用 TDD 模式时
- 生成边缘用例测试时
快速入门
# Generate unit tests for a file
aqe test generate --file src/services/UserService.ts --framework jest
# Generate tests with coverage target
aqe test generate --scope src/api/ --coverage 90 --type unit
# Generate integration tests
aqe test generate --file src/controllers/AuthController.ts --type integration
# Generate from patterns
aqe test generate --pattern repository --target src/repositories/
Agent 工作流
// Spawn test generation agents — durable-first
Task("Generate durable-first tests", `
Analyze src/services/PaymentService.ts and generate Jest tests in tier order.
1. DURABLE (write these first, >=1 per public method):
- Invariants ("a refund never makes a balance negative")
- Contract/schema checks on inputs and outputs crossing the boundary
- Property-based tests (fast-check) over input ranges, not single examples
2. EPHEMERAL (the red-green loop; tag '// @tier ephemeral'):
- Specific happy-path examples and error paths
- Mock external dependencies ONLY — never let a mock-call assertion be the
only test for a method
Apply the language-swap check: if a Python rewrite of PaymentService would break
the test, move it up to the durable tier.
Output to tests/unit/services/PaymentService.test.ts
`, "qe-test-architect")
// Property + contract generation (first-class, not opt-in)
Task("Generate property and contract tests", `
For src/repositories/, derive:
- Properties: round-trip (write→read returns same), idempotence, ordering invariants
- Contracts: the repository interface schema, enforced on every CRUD result
These survive a storage-engine swap; example-based CRUD tests do not.
`, "qe-property-tester")
测试生成策略
1. 基于代码分析
await testGenerator.analyzeAndGenerate({
source: 'src/services/OrderService.ts',
analysis: {
methods: true,
branches: true,
dependencies: true,
errorPaths: true
},
output: {
framework: 'jest',
style: 'describe-it',
assertions: 'expect'
}
});
2. 基于模式生成
await testGenerator.applyPattern({
pattern: 'service-layer',
targets: ['src/services/*.ts'],
customizations: {
mockStrategy: 'jest.mock',
asyncHandling: 'async-await',
errorAssertion: 'toThrow'
}
});
3. 基于覆盖率生成
await testGenerator.fillCoverageGaps({
coverageReport: 'coverage/lcov.info',
targetCoverage: 90,
prioritize: ['uncovered-branches', 'error-paths'],
maxTests: 50
});
框架支持
| 框架 | 单元 | 集成 | 端到端 | 模拟 |
|---|---|---|---|---|
| Jest | ✅ | ✅ | ⚠️ | jest.mock |
| Vitest | ✅ | ✅ | ⚠️ | vi.mock |
| Mocha | ✅ | ✅ | ❌ | sinon |
| Pytest | ✅ | ✅ | ❌ | pytest-mock |
| JUnit | ✅ | ✅ | ❌ | Mockito |
测试质量检查
quality_checks:
durability: # the primary check (ADR-113)
durable_assertions_per_target: 1 # >=1 invariant/contract/property each
language_swap_safe: true # would survive a reimplementation
tier_tags_present: true # every test tagged durable|ephemeral|live
fault_detection: # do the tests actually catch bugs?
mutation_score_min: 0.6 # kill rate against seeded mutants
no_assertionless_tests: true # reject tests that kill 0 mutants
assertions:
minimum_per_test: 1
meaningful: true
isolation:
no_shared_state: true
proper_setup_teardown: true
naming:
descriptive: true
follows_convention: true
coverage: # necessary but NOT sufficient — see fault_detection
branches: 80
statements: 85
技能组合
- 生成测试后 → 运行
/mutation-testing验证测试质量 - 生成测试前 → 使用
/test-automation-strategy选择框架和模式 - 相关技能 → 使用
/qe-coverage-analysis找出最需要测试的部分
注意事项
- 对于超过 3000 行的文件,Agent 会截断输出——请针对单个模块生成测试,而不是整个目录
- 单独通过单元测试的组件可能完全没有集成连线——请始终为每个模块边界至少生成一个集成测试
- 为新代码库生成测试时,请检查已安装的框架(jest、vitest 或 mocha)——它们的模拟 API 各不相同,Claude 可能会用错
- 完成表演:Agent 可能声称“已生成全面的测试”,却留下存根或硬编码值——接受前务必运行生成的测试
- 使用 QE Agent 前必须先初始化 Fleet:运行
aqe health进行诊断;如果出现 “Fleet not initialized”,则运行aqe init重新初始化
协作
主要 Agent:qe-test-generator、qe-pattern-matcher、qe-test-architect 协调器:qe-test-generation-coordinator 相关技能:qe-coverage-analysis、qe-test-execution