Loading...
Loading...
Compare original and translation side by side
| Property | Formula | When to Use |
|---|---|---|
| Roundtrip | | Serialization, conversion pairs |
| Idempotence | | Normalization, formatting, sorting |
| Invariant | Property holds before/after | Any transformation |
| Commutativity | | Binary/set operations |
| Associativity | | Combining operations |
| Identity | | Operations with neutral element |
| Inverse | | encrypt/decrypt, compress/decompress |
| Oracle | | Optimization, refactoring |
| Easy to Verify | | Complex algorithms |
| No Exception | No crash on valid input | Baseline (weakest) |
No Exception -> Type Preservation -> Invariant -> Idempotence -> Roundtrip| 属性 | 公式 | 适用场景 |
|---|---|---|
| 往返一致性(Roundtrip) | | 序列化、转换对 |
| 幂等性(Idempotence) | | 规范化、格式化、排序 |
| 不变性(Invariant) | 属性在转换前后保持成立 | 任何转换操作 |
| 交换性(Commutativity) | | 二元/集合操作 |
| 结合性(Associativity) | | 组合操作 |
| 恒等性(Identity) | | 包含中性元素的操作 |
| 逆运算(Inverse) | | 加密/解密、压缩/解压缩 |
| 参照验证(Oracle) | | 优化、重构 |
| 易验证性(Easy to Verify) | | 复杂算法 |
| 无异常(No Exception) | 合法输入下无崩溃 | 基线测试(最弱要求) |
No Exception -> Type Preservation -> Invariant -> Idempotence -> Roundtrip| Pattern | Property | Priority |
|---|---|---|
| Roundtrip | HIGH |
| Roundtrip | HIGH |
| Pure functions with clear contracts | Multiple | HIGH |
| Idempotence | MEDIUM |
| Valid after normalize | MEDIUM |
| Sorting, ordering, comparators | Idempotence + ordering | MEDIUM |
| Custom collections (add/remove/get) | Invariants | MEDIUM |
| Builder/factory patterns | Output invariants | LOW |
| 模式 | 属性 | 优先级 |
|---|---|---|
| 往返一致性 | 高 |
| 往返一致性 | 高 |
| 具有明确契约的纯函数 | 多个属性 | 高 |
| 幂等性 | 中 |
带规范化器的 | 规范化后验证通过 | 中 |
| 排序、排序规则、比较器 | 幂等性 + 排序性 | 中 |
| 自定义集合(添加/删除/获取) | 不变性 | 中 |
| 构建器/工厂模式 | 输出不变性 | 低 |
| Language | Library | Import |
|---|---|---|
| Python | Hypothesis | |
| TypeScript/JS | fast-check | |
| Rust | proptest | |
| Go | rapid | |
| Java | jqwik | |
| Haskell | QuickCheck | |
@ed3d-research-agents:internet-researcher| 语言 | 库 | 导入语句 |
|---|---|---|
| Python | Hypothesis | |
| TypeScript/JS | fast-check | |
| Rust | proptest | |
| Go | rapid | |
| Java | jqwik | |
| Haskell | QuickCheck | |
@ed3d-research-agents:internet-researcherassume()# GOOD
st.integers(min_value=1, max_value=100)
# BAD - high rejection rate
st.integers().filter(lambda x: 1 <= x <= 100)st.lists(st.integers(), max_size=100)
st.text(max_size=1000)st.integers(min_value=0, max_value=150) # Real ages, not arbitrary intsvalid_users = st.builds(User, ...)
@given(valid_users)
def test_one(user): ...
@given(valid_users)
def test_two(user): ...assume()# GOOD
st.integers(min_value=1, max_value=100)
# BAD - 高拒绝率
st.integers().filter(lambda x: 1 <= x <= 100)st.lists(st.integers(), max_size=100)
st.text(max_size=1000)st.integers(min_value=0, max_value=150) # 真实年龄范围,而非任意整数valid_users = st.builds(User, ...)
@given(valid_users)
def test_one(user): ...
@given(valid_users)
def test_two(user): ...undefinedundefinedundefinedundefinedassume()@exampleassume()@exampleassert sorted(xs) == sorted(xs)assume()assert add(a, b) == a + b@example([])@example([1])assume()assert sorted(xs) == sorted(xs)assume()add(a,b)a+bassert add(a, b) == a + b@example([])@example([1])assume()| Mistake | Fix |
|---|---|
| Testing mock behavior | Test real behavior |
| Reimplementing function in test | Use algebraic properties |
| Filtering with assume() | Build constraints into strategy |
| No edge case examples | Add @example decorators |
| One property only | Add multiple properties (length, ordering, etc.) |
| 错误 | 修复方案 |
|---|---|
| 测试Mock行为 | 测试真实行为 |
| 在测试中重实现函数逻辑 | 使用代数属性 |
用 | 将约束内置到策略中 |
| 无边缘情况示例 | 添加 |
| 仅测试单个属性 | 添加多个属性(长度、排序性等) |