Skip to main content

16. Property-Based Testing with Hypothesis

Date: 2025-10-13

Status

Accepted

Category

Testing & Quality

Context

Traditional unit tests check specific examples:
Edge cases often missed:
  • Negative numbers
  • Zero values
  • Very large numbers
  • Unicode in strings

Decision

Use Hypothesis for property-based testing to discover edge cases automatically.

Example

Hypothesis generates 100+ test cases including edge cases.

Consequences

Positive

  • Edge Case Discovery: Finds bugs unit tests miss
  • Higher Confidence: Tests properties, not examples
  • Less Test Code: One property test replaces many examples
  • Regression Prevention: Generated cases saved on failure

Negative

  • Slower Tests: 100 cases per test vs 1
  • Learning Curve: Property thinking vs example thinking
  • Flaky Tests: Random generation can cause intermittent failures

Usage

27+ property tests covering:
  • LLM factory fallback logic
  • Authentication providers
  • Session management
  • Role mapping

References

  • Tests: tests/property/
  • Dependency: pyproject.toml:66 - hypothesis>=6.100.0