property-based-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Property-Based Testing

基于属性的测试

Expert knowledge for property-based testing - automatically generating test cases to verify code properties rather than testing specific examples.
关于基于属性的测试的专业知识——自动生成测试用例以验证代码属性,而非测试特定示例。

When to Use This Skill

何时使用此技能

Use this skill when...Use another skill instead when...
Testing mathematical properties (commutative, associative)Writing specific example-based unit tests
Testing encode/decode roundtripsSetting up test runner configuration
Finding edge cases automaticallyDoing E2E browser testing
Validating data transformations and invariantsAnalyzing test quality or smells
Testing API contracts with generated dataRunning mutation testing
适合使用此技能的场景...适合使用其他技能的场景...
测试数学属性(交换律、结合律)编写基于特定示例的单元测试
测试编码/解码往返流程配置测试运行器
自动发现边界情况进行端到端浏览器测试
验证数据转换与不变量分析测试质量或测试坏味道
使用生成的数据测试API契约运行变异测试

Core Expertise

核心专业知识

Property-Based Testing Concept
  • Traditional testing: Test specific examples
  • Property-based testing: Test properties that should hold for all inputs
  • Generators: Automatically create diverse test inputs
  • Shrinking: Minimize failing cases to simplest example
  • Coverage: Explore edge cases humans might miss
When to Use Property-Based Testing
  • Mathematical operations (commutative, associative properties)
  • Encoders/decoders (roundtrip properties)
  • Parsers and serializers
  • Data transformations
  • API contracts
  • Invariants and constraints
基于属性的测试概念
  • 传统测试:测试特定示例
  • 基于属性的测试:测试对所有输入都应成立的属性
  • 生成器:自动创建多样化的测试输入
  • 简化(Shrinking):将失败案例简化为最简单的示例
  • 覆盖范围:探索人类可能遗漏的边界情况
何时使用基于属性的测试
  • 数学运算(交换律、结合律等属性)
  • 编码器/解码器(往返属性)
  • 解析器与序列化器
  • 数据转换
  • API契约
  • 不变量与约束

TypeScript/JavaScript (fast-check)

TypeScript/JavaScript(fast-check)

Installation

安装

bash
undefined
bash
undefined

Using Bun

Using Bun

bun add -d fast-check
bun add -d fast-check

Using npm

Using npm

npm install -D fast-check
undefined
npm install -D fast-check
undefined

Basic Example

基础示例

typescript
import { test } from 'vitest'
import * as fc from 'fast-check'

// Property-based test
test('reverse twice returns original - property based', () => {
  fc.assert(
    fc.property(
      fc.array(fc.integer()), // Generate random arrays of integers
      (arr) => {
        expect(reverse(reverse(arr))).toEqual(arr)
      }
    )
  )
})
// fast-check automatically generates 100s of test cases!
typescript
import { test } from 'vitest'
import * as fc from 'fast-check'

// Property-based test
test('reverse twice returns original - property based', () => {
  fc.assert(
    fc.property(
      fc.array(fc.integer()), // Generate random arrays of integers
      (arr) => {
        expect(reverse(reverse(arr))).toEqual(arr)
      }
    )
  )
})
// fast-check automatically generates 100s of test cases!

Key Generators (Quick Reference)

常用生成器速查

GeneratorDescription
fc.integer()
Any integer (with optional min/max)
fc.nat()
Natural numbers (>= 0)
fc.float()
/
fc.double()
Floating-point numbers
fc.string()
Any string (with optional length)
fc.emailAddress()
Email format strings
fc.array(arb)
Arrays of arbitrary type
fc.record({...})
Object with typed fields
fc.boolean()
Boolean values
fc.constantFrom(...)
Pick from options
fc.tuple(...)
Fixed-size tuples
fc.oneof(...)
Union types
fc.option(arb)
Value or null
fc.date()
Date objects
生成器描述
fc.integer()
任意整数(可指定最小值/最大值)
fc.nat()
自然数(>= 0)
fc.float()
/
fc.double()
浮点数
fc.string()
任意字符串(可指定长度)
fc.emailAddress()
邮箱格式字符串
fc.array(arb)
任意类型的数组
fc.record({...})
带类型字段的对象
fc.boolean()
布尔值
fc.constantFrom(...)
从选项中选择
fc.tuple(...)
固定大小的元组
fc.oneof(...)
联合类型
fc.option(arb)
值或null
fc.date()
日期对象

Common Properties to Test

可测试的常见属性

PropertyPatternExample
Roundtrip
f(g(x)) = x
encode/decode, serialize/parse
Idempotence
f(f(x)) = f(x)
sort, normalize, format
Commutativity
f(a,b) = f(b,a)
add, merge, union
Associativity
f(f(a,b),c) = f(a,f(b,c))
add, concat
Identity
f(x, id) = x
multiply by 1, add 0
Inverse
f(g(x)) = x
encrypt/decrypt
属性模式示例
往返
f(g(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, id) = x
乘以1、加0
逆运算
f(g(x)) = x
加密/解密

Configuration

配置

typescript
fc.assert(property, {
  numRuns: 1000,      // Run 1000 tests (default: 100)
  seed: 42,           // Reproducible tests
  endOnFailure: true, // Stop after first failure
})
typescript
fc.assert(property, {
  numRuns: 1000,      // Run 1000 tests (default: 100)
  seed: 42,           // Reproducible tests
  endOnFailure: true, // Stop after first failure
})

Preconditions

前置条件

typescript
fc.pre(b !== 0) // Skip cases where b is 0
typescript
fc.pre(b !== 0) // Skip cases where b is 0

Python (Hypothesis)

Python(Hypothesis)

Installation

安装

bash
undefined
bash
undefined

Using uv

Using uv

uv add --dev hypothesis
uv add --dev hypothesis

Using pip

Using pip

pip install hypothesis
undefined
pip install hypothesis
undefined

Basic Example

基础示例

python
from hypothesis import given, strategies as st

@given(st.lists(st.integers()))
def test_reverse_twice_property(arr):
    assert reverse(reverse(arr)) == arr
    # Hypothesis automatically generates 100s of test cases!
python
from hypothesis import given, strategies as st

@given(st.lists(st.integers()))
def test_reverse_twice_property(arr):
    assert reverse(reverse(arr)) == arr
    # Hypothesis automatically generates 100s of test cases!

Key Strategies (Quick Reference)

常用策略速查

StrategyDescription
st.integers()
Any integer (with optional bounds)
st.floats()
Floating-point numbers
st.text()
Any string (with optional size)
st.binary()
Byte strings
st.lists(strat)
Lists of given strategy
st.sets(strat)
Unique value sets
st.dictionaries(k, v)
Dictionaries
st.booleans()
Boolean values
st.sampled_from(...)
Pick from options
st.tuples(...)
Fixed-size tuples
st.one_of(...)
Union types
st.dates()
/
st.datetimes()
Date/time values
st.builds(Class, ...)
Build objects from strategies
策略描述
st.integers()
任意整数(可指定范围)
st.floats()
浮点数
st.text()
任意字符串(可指定大小)
st.binary()
字节字符串
st.lists(strat)
基于给定策略的列表
st.sets(strat)
唯一值集合
st.dictionaries(k, v)
字典
st.booleans()
布尔值
st.sampled_from(...)
从选项中选择
st.tuples(...)
固定大小的元组
st.one_of(...)
联合类型
st.dates()
/
st.datetimes()
日期/时间值
st.builds(Class, ...)
从策略构建对象

Configuration

配置

python
from hypothesis import given, settings, strategies as st

@settings(max_examples=1000, deadline=None)
@given(st.lists(st.integers()))
def test_with_custom_settings(arr):
    assert sort(arr) == sorted(arr)
python
from hypothesis import given, settings, strategies as st

@settings(max_examples=1000, deadline=None)
@given(st.lists(st.integers()))
def test_with_custom_settings(arr):
    assert sort(arr) == sorted(arr)

Assumptions

假设条件

python
from hypothesis import assume
assume(b != 0)  # Skip cases where b is 0
python
from hypothesis import assume
assume(b != 0)  # Skip cases where b is 0

Stateful Testing

有状态测试

Hypothesis supports stateful testing via
RuleBasedStateMachine
for testing sequences of operations against invariants.
Hypothesis支持通过
RuleBasedStateMachine
进行有状态测试,用于针对不变量测试操作序列。

Agentic Optimizations

智能优化

ContextCommand
Quick TS test
bunx vitest --dots --bail=1 --grep 'property'
Quick Python test
uv run pytest -x -q --tb=short -k 'property'
CI TS test
bunx vitest run --reporter=junit --grep 'property'
CI Python test
uv run pytest --hypothesis-show-statistics -q
Reproducible
fc.assert(prop, { seed: 42 })
or
@settings(derandomize=True)
Fast iteration
fc.assert(prop, { numRuns: 50 })
or
@settings(max_examples=50)
For detailed examples, advanced patterns, and best practices, see REFERENCE.md.
场景命令
快速TS测试
bunx vitest --dots --bail=1 --grep 'property'
快速Python测试
uv run pytest -x -q --tb=short -k 'property'
CI环境TS测试
bunx vitest run --reporter=junit --grep 'property'
CI环境Python测试
uv run pytest --hypothesis-show-statistics -q
可复现测试
fc.assert(prop, { seed: 42 })
@settings(derandomize=True)
快速迭代
fc.assert(prop, { numRuns: 50 })
@settings(max_examples=50)
有关详细示例、高级模式和最佳实践,请参阅REFERENCE.md

See Also

另请参阅

  • vitest-testing
    - Unit testing framework
  • python-testing
    - Python pytest testing
  • test-quality-analysis
    - Detecting test smells
  • mutation-testing
    - Validate test effectiveness
  • vitest-testing
    - 单元测试框架
  • python-testing
    - Python pytest测试
  • test-quality-analysis
    - 检测测试坏味道
  • mutation-testing
    - 验证测试有效性

References

参考资料