pythonintermediate

Property-Based Testing for Data Functions

Use Hypothesis to automatically generate edge-case test data for data transformation functions.

python
from hypothesis import given, settings
from hypothesis import strategies as st
import pandas as pd

def normalise(s: pd.Series) -> pd.Series:
    mn, mx = s.min(), s.max()
    if mx == mn:
        return pd.Series([0.0] * len(s))
    return (s - mn) / (mx - mn)

@given(st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=2, max_size=100))
@settings(max_examples=500)
def test_normalise_bounds(values):
    result = normalise(pd.Series(values))
    assert result.min() >= 0.0
    assert result.max() <= 1.0

test_normalise_bounds()
print('All property tests passed')

Use Cases

  • data function testing
  • edge case discovery
  • pipeline quality

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.