pythonintermediate
Property-Based Testing for Data Functions
Use Hypothesis to automatically generate edge-case test data for data transformation functions.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonintermediate
Data Quality Testing with Expectations
Define and run data quality expectations for automated validation in data pipelines.
Best for: Automated data quality gates in pipelines
#data-quality#testing
sqlintermediate
SQL Data Quality Checks and Assertions
Reusable SQL queries for data quality: null checks, uniqueness, referential integrity, and freshness.
Best for: Automated data quality gates in ETL pipelines
#sql#data-quality
sqlbeginner
dbt Source Freshness and Testing
Configure dbt source freshness checks and schema tests to validate upstream data pipelines.
Best for: Ensuring upstream data sources are fresh
#dbt#testing
pythonadvanced
Great Expectations Data Quality Suite
Define and run a Great Expectations validation suite to catch data quality issues early.
Best for: CI data validation
#great-expectations#data-quality