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.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 16 views 0 copies

Requires third-party packages — install first
pip install pytest

Python code

32 lines
Python 3.9+
import 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

stdout
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

  1. Use parameterized fixtures with `@pytest.mark.parametrize` to test multiple row shapes
  2. 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

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Data pipelines & processing

Related tutorials and quizzes for this topic.