pairwise-test-coverage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pairwise Test Coverage

成对测试覆盖

Combinatorial testing that covers all factor pairs in near-minimal test cases.
When to use: Multi-factor systems where exhaustive testing is impractical, state machines, retry/recovery logic, configuration matrices, compatibility testing, any code with 3+ interacting parameters.
When not to use: Single-factor tests (just test each value), two-factor systems (test all combinations directly), UI snapshot tests, type-only changes.
通过接近最少的测试用例覆盖所有因子对的组合测试。
适用场景:穷举测试不切实际的多因子系统、状态机、重试/恢复逻辑、配置矩阵、兼容性测试,以及任何包含3个及以上交互参数的代码。
不适用场景:单因子测试(只需测试每个值即可)、双因子系统(直接测试所有组合)、UI快照测试、仅类型变更的场景。

Core Philosophy

核心理念

Exhaustive testing doesn't scale. If a system has 4 factors with 3 values each, that's 81 test cases. Pairwise testing covers all pair interactions in ~12 cases -- an 85% reduction with near-complete defect detection.
穷举测试无法扩展。如果一个系统有4个因子,每个因子有3个取值,那需要81个测试用例。而成对测试仅需约12个用例就能覆盖所有因子对交互——测试用例减少85%,同时缺陷检测率接近完整覆盖。

Rationalizations (Do Not Skip)

误区纠正(请勿跳过)

RationalizationWhy It's WrongRequired Action
"We'll test the important combinations"Unexpected factor interactions go untested without systematic coverageGenerate the pairwise matrix
"81 test cases is fine"81 cases means 81 things to maintain and debug when they failUse pairwise to get 12
"The test passes, so the code works"Test must FAIL before the fix to prove it catches the bugValidate detection first

错误观点错误原因正确做法
“我们只测试重要的组合”缺乏系统性覆盖会导致未预料到的因子交互无法被测试生成成对矩阵
“81个测试用例没问题”81个用例意味着维护成本高,且失败时需要调试81个内容使用成对测试将用例减至12个
“测试通过了,所以代码没问题”测试必须在修复前失败,才能证明它能发现bug先验证缺陷检测能力

Quick Reference

快速参考

TechniqueUse WhenKey Pattern
Pairwise MatrixMultiple factors with discrete values
it.each(cases)
table-driven
Property-BasedValue ranges, type safety invariants
fc.assert(fc.property(...))
Model-BasedState machine transitionsTransition table tests
Fault InjectionStorage/network failuresMock rejection scenarios
Contract TestsSchema validation at boundaries
Schema.safeParse()
assertions
技术适用场景核心模式
成对矩阵多因子且取值离散的场景
it.each(cases)
表格驱动测试
基于属性的测试取值范围、类型安全不变量
fc.assert(fc.property(...))
基于模型的测试状态机转换转换表测试
故障注入存储/网络故障场景模拟拒绝场景
契约测试边界处的 schema 校验
Schema.safeParse()
断言

Included Utilities

内置工具

typescript
// Pairwise matrix generator (zero dependencies)
import { generatePairwiseMatrix, formatAsMarkdownTable } from './pairwise.ts';

// Pairwise test case helpers
import { createPairwiseTestCases, generateTestCaseName } from './test-fixtures.ts';
typescript
// 成对矩阵生成器(零依赖)
import { generatePairwiseMatrix, formatAsMarkdownTable } from './pairwise.ts';

// 成对测试用例辅助工具
import { createPairwiseTestCases, generateTestCaseName } from './test-fixtures.ts';

Performance and Limits

性能与限制

The greedy algorithm never enumerates the Cartesian product. Pair count grows as O(factors² × values²).
FactorsValuesCartesianPairwise CasesTime
3327~10<1ms
8465,536~46~2ms
8816,777,216~100~10ms
Hard limits (throws if exceeded): max 20 factors, max 50 values per factor. Beyond these, pair count exceeds ~475K (C(20,2) × 50²) and in-memory generation becomes impractical.
贪心算法不会枚举笛卡尔积。因子对数量的增长复杂度为 O(factors² × values²)。
因子数量每个因子的取值数笛卡尔积数量成对测试用例数耗时
3327~10<1ms
8465,536~46~2ms
8816,777,216~100~10ms
硬限制(超出则抛出错误):最多20个因子,每个因子最多50个取值。超出此范围后,因子对数量会超过约47.5万(C(20,2) × 50²),内存内生成将变得不切实际。

Violation Rules

违规规则

SlugRuleSeverity
missing_pairwise_coverage
Multi-factor code changes need pairwise testsmust-fail
bug_detection_not_validated
Tests must fail before fix, pass aftermust-fail
标识规则严重程度
missing_pairwise_coverage
多因子代码变更需要成对测试必须失败
bug_detection_not_validated
测试必须在修复前失败、修复后通过必须失败

Definition of Done

完成标准

  • Factors documented in test file header
  • Pairwise matrix as
    it.each
    test cases
  • Tests fail before fix, pass after
  • 测试文件头部已记录因子信息
  • 成对矩阵以
    it.each
    测试用例形式存在
  • 测试在修复前失败、修复后通过

Companion Skills

配套技能

This skill provides combinatorial test matrix generation, not test design guidance. For broader methodology:
  • Search
    combinatorial testing
    on skills.sh for constraint handling, higher-strength covering arrays, and test oracle strategies
  • Guard truth tables with 5+ boolean inputs use pairwise for coverage — use model-based-testing for state machine transition matrices and guard truth table generation
  • Zod schemas with 5+ optional fields use pairwise for compound state coverage — use zod-contract-testing for schema boundary validation and compound state matrices

本技能提供组合测试矩阵生成能力,但不包含测试设计指导。如需更全面的方法论:
  • skills.sh 上搜索
    combinatorial testing
    ,获取约束处理、更高强度覆盖数组和测试预言机策略相关内容
  • 包含5个及以上布尔输入的守卫真值表,使用成对测试实现覆盖——对于状态机转换矩阵和守卫真值表生成,使用 model-based-testing
  • 包含5个及以上可选字段的 Zod 模式,使用成对测试实现复合状态覆盖——对于 schema 边界校验和复合状态矩阵,使用 zod-contract-testing

Details

详细内容

See references for:
  • workflow.md: Step-by-step implementation guide
  • violations.md: Full violation rules with detection patterns
  • examples.md: 6 testing technique examples (pairwise, property-based, model-based, fault injection, contract, observability)
参考以下文件获取详情:
  • workflow.md:分步实现指南
  • violations.md:完整的违规规则及检测模式
  • examples.md:6种测试技术示例(成对测试、基于属性的测试、基于模型的测试、故障注入、契约测试、可观测性测试)