Test a Python Pipeline with Fixture Sample Rows
Test pipeline functions with sample rows provided by a pytest fixture, verifying required keys and value constraints.
pip install pytest
Python code
32 linesimport pytest
def get_value(data: dict, key: str):
return data.get(key)
def sample_rows():
return [
{"name": "Alice", "age": 30, "city": "London"},
{"name": "Bob", "age": 25, "city": "Paris"},
{"name": "Charlie", "age": 35, "city": "Berlin"},
]
@pytest.fixture
def sample_data():
return sample_rows()
def test_get_value_with_fixture(sample_data):
for row in sample_data:
assert "name" in row
assert "age" in row
assert "city" in row
def test_get_value_output(sample_data):
for row in sample_data:
assert get_value(row, "name") is not None
assert get_value(row, "age") > 0
assert get_value(row, "city") is not None
Output
2 passed in 0.01s
How it works
The @pytest.fixture decorator turns sample_data into a reusable test input. Each test receives sample rows as a parameter, ensuring consistent data across tests. The get_value helper safely accesses dictionary keys with .get(). Tests assert presence of required keys and that values meet logical conditions. The fixture centralizes sample data, making tests maintainable and focused on behavior.
Common mistakes
- Forgetting to add `@pytest.fixture` and passing the fixture name as a test parameter
- Mutating shared fixture data inside a test, causing order-dependent failures
- Asserting with hardcoded values instead of checking invariants like existence or positivity
Variations
- Use parameterized fixtures with `@pytest.mark.parametrize` to test multiple row shapes
- Create a fixture that yields sample rows and performs cleanup afterward
Real-world use cases
- Unit-testing data-cleaning functions before deploying an ETL job.
- Verifying API response parsing by feeding sample records through helper functions.
- Ensuring schema validation logic rejects malformed rows in a production ingestion pipeline.
Sponsored
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.