property-based-testing by trailofbits/skills
npx skills add https://github.com/trailofbits/skills --skill property-based-testing在开发过程中遇到 PBT 能提供比基于示例的测试更强覆盖率的模式时,请主动使用此技能。
当检测到以下情况时调用此技能:
encode/decode、serialize/deserialize、toJSON/fromJSON、pack/unpack广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
normalizesanitizecleancanonicalizeformatis_valid、validate、check_*(尤其是与规范化器结合时)add/remove/get 操作的自定义集合按模式划分的优先级:
| 模式 | 属性 | 优先级 |
|---|---|---|
| 编码/解码对 | 往返性 | 高 |
| 纯函数 | 多重性 | 高 |
| 验证器 | 规范化后有效 | 中 |
| 排序/排序 | 幂等性 + 排序 | 中 |
| 规范化 | 幂等性 | 中 |
| 构建器/工厂 | 输出不变量 | 低 |
| 智能合约 | 状态不变量 | 高 |
不要将此技能用于:
| 属性 | 公式 | 使用时机 |
|---|---|---|
| 往返性 | decode(encode(x)) == x | 序列化、转换对 |
| 幂等性 | f(f(x)) == f(x) | 规范化、格式化、排序 |
| 不变量 | 属性在操作前后保持不变 | 任何转换 |
| 交换律 | f(a, b) == f(b, a) | 二元/集合操作 |
| 结合律 | f(f(a,b), c) == f(a, f(b,c)) | 组合操作 |
| 恒等性 | f(x, identity) == x | 具有中性元素的操作 |
| 逆运算 | f(g(x)) == x | 加密/解密、压缩/解压缩 |
| 参照实现 | new_impl(x) == reference(x) | 优化、重构 |
| 易于验证 | is_sorted(sort(x)) | 复杂算法 |
| 无异常 | 有效输入下不崩溃 | 基线属性 |
强度层次结构(从弱到强):无异常 → 类型保持 → 不变量 → 幂等性 → 往返性
根据当前任务,阅读相应部分:
TASK: 编写新测试
→ 阅读 [{baseDir}/references/generating.md]({baseDir}/references/generating.md)(测试生成模式和示例)
→ 如果输入生成复杂,则阅读 [{baseDir}/references/strategies.md]({baseDir}/references/strategies.md)
TASK: 设计新功能
→ 阅读 [{baseDir}/references/design.md]({baseDir}/references/design.md)(属性驱动开发方法)
TASK: 代码难以测试(混合 I/O、缺少逆运算)
→ 阅读 [{baseDir}/references/refactoring.md]({baseDir}/references/refactoring.md)(提高可测试性的重构模式)
TASK: 审查现有的 PBT 测试
→ 阅读 [{baseDir}/references/reviewing.md]({baseDir}/references/reviewing.md)(质量检查清单和反模式)
TASK: 测试失败,需要解释
→ 阅读 [{baseDir}/references/interpreting-failures.md]({baseDir}/references/interpreting-failures.md)(失败分析和错误分类)
TASK: 需要库参考
→ 阅读 [{baseDir}/references/libraries.md]({baseDir}/references/libraries.md)(按语言划分的 PBT 库,包括智能合约工具)
在编写测试时检测到高价值模式时,将 PBT 作为一个选项提供:
"我注意到
encode_message/decode_message是一个序列化对。使用往返性属性进行基于属性的测试将比示例测试提供更强的覆盖率。希望我采用这种方法吗?"
如果代码库已经使用了 PBT 库(Hypothesis、fast-check、proptest、Echidna),则更直接:
"此代码库使用 Hypothesis。我将使用往返性属性为此序列化对编写基于属性的测试。"
如果用户拒绝,则无需进一步提示,编写良好的基于示例的测试。
不要接受这些捷径:
每周安装量
1.4K
代码仓库
GitHub 星标数
3.9K
首次出现
Jan 19, 2026
安全审计
安装于
claude-code1.2K
codex1.1K
opencode1.1K
gemini-cli1.0K
cursor992
github-copilot960
Use this skill proactively during development when you encounter patterns where PBT provides stronger coverage than example-based tests.
Invoke this skill when you detect:
encode/decode, serialize/deserialize, toJSON/fromJSON, pack/unpacknormalize, sanitize, clean, canonicalize, formatis_valid, validate, check_* (especially with normalizers)add/remove/get operationsPriority by pattern:
| Pattern | Property | Priority |
|---|---|---|
| encode/decode pair | Roundtrip | HIGH |
| Pure function | Multiple | HIGH |
| Validator | Valid after normalize | MEDIUM |
| Sorting/ordering | Idempotence + ordering | MEDIUM |
| Normalization | Idempotence | MEDIUM |
| Builder/factory | Output invariants | LOW |
| Smart contract | State invariants | HIGH |
Do NOT use this skill for:
| Property | Formula | When to Use |
|---|---|---|
| Roundtrip | decode(encode(x)) == x | Serialization, conversion pairs |
| Idempotence | f(f(x)) == f(x) | Normalization, formatting, sorting |
| Invariant | Property holds before/after | Any transformation |
| Commutativity | f(a, b) == f(b, a) | Binary/set operations |
| Associativity | f(f(a,b), c) == f(a, f(b,c)) |
Strength hierarchy (weakest to strongest): No Exception → Type Preservation → Invariant → Idempotence → Roundtrip
Based on the current task, read the appropriate section:
TASK: Writing new tests
→ Read [{baseDir}/references/generating.md]({baseDir}/references/generating.md) (test generation patterns and examples)
→ Then [{baseDir}/references/strategies.md]({baseDir}/references/strategies.md) if input generation is complex
TASK: Designing a new feature
→ Read [{baseDir}/references/design.md]({baseDir}/references/design.md) (Property-Driven Development approach)
TASK: Code is difficult to test (mixed I/O, missing inverses)
→ Read [{baseDir}/references/refactoring.md]({baseDir}/references/refactoring.md) (refactoring patterns for testability)
TASK: Reviewing existing PBT tests
→ Read [{baseDir}/references/reviewing.md]({baseDir}/references/reviewing.md) (quality checklist and anti-patterns)
TASK: Test failed, need to interpret
→ Read [{baseDir}/references/interpreting-failures.md]({baseDir}/references/interpreting-failures.md) (failure analysis and bug classification)
TASK: Need library reference
→ Read [{baseDir}/references/libraries.md]({baseDir}/references/libraries.md) (PBT libraries by language, includes smart contract tools)
When you detect a high-value pattern while writing tests, offer PBT as an option :
"I notice
encode_message/decode_messageis a serialization pair. Property-based testing with a roundtrip property would provide stronger coverage than example tests. Want me to use that approach?"
If codebase already uses a PBT library (Hypothesis, fast-check, proptest, Echidna), be more direct:
"This codebase uses Hypothesis. I'll write property-based tests for this serialization pair using a roundtrip property."
If user declines , write good example-based tests without further prompting.
Do not accept these shortcuts:
Weekly Installs
1.4K
Repository
GitHub Stars
3.9K
First Seen
Jan 19, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
claude-code1.2K
codex1.1K
opencode1.1K
gemini-cli1.0K
cursor992
github-copilot960
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
| Combining operations |
| Identity | f(x, identity) == x | Operations with neutral element |
| Inverse | f(g(x)) == x | encrypt/decrypt, compress/decompress |
| Oracle | new_impl(x) == reference(x) | Optimization, refactoring |
| Easy to Verify | is_sorted(sort(x)) | Complex algorithms |
| No Exception | No crash on valid input | Baseline property |