Unit Testing & Integration Testing Data Pipelines (pytest)
Test transforms and I/O boundaries with pytest fixtures, mocks, and integration checks against real engines.
In plain words
Pure rule, schema contract, or external boundary. Next, deterministic input including edge records. Finally, repeat a run and expect no logical duplication.
Think of it like…
Unit tests inspect one camera lens; integration tests take the lens, wiring, recorder, and clock into the field and record an actual frame.
Where you’ve seen it
A pytest parametrization feeds null currency, duplicate order_id, and a late event into normalize_orders; an integration test executes the resulting MERGE twice against test Postgres and expects the same count.
Words you’ll hear
- pytest
- Pytest's de facto test runner with fixtures, parametrization, markers, and rich assertion introspection.
- Unit Tests
- Fast tests of a single transformation or decision with dependencies replaced by narrow fakes.
- Integration
- Tests that combine real components-database, file reader, container, or broker-to catch protocol and state errors.
- Operational boundary
- Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition. Freeze clocks, seed tiny representative fixtures, assert exact rows and schemas, test late and duplicate records, and exercise one narrow real Postgres/object-store/Kafka boundary in CI.
Visual diagram
Theory & deep dive
pytest: the working mechanism
Unit tests validate pure transformations quickly, contract tests lock producer schemas, and integration tests run a minimal real engine or connector to expose boundary behavior mocks cannot reproduce.
Pure rule, schema contract, or external boundary. Deterministic input including edge records, then rows, types, counts, and side effects; this exposes the state governed by Integration.
A pytest parametrization feeds null currency, duplicate order_id, and a late event into normalize_orders; an integration test executes the resulting MERGE twice against test Postgres and expects the same count.
Unit Testing & Integration Testing Data Pipelines: failure mode and production response
Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition.
Freeze clocks, seed tiny representative fixtures, assert exact rows and schemas, test late and duplicate records, and exercise one narrow real Postgres/object-store/Kafka boundary in CI. Track Unit duration as "typically seconds for the full fast suite", and make Fixture coverage visible to the dataset owner.
How it works, step by step
Step 1 of 41. Classify
Pure rule, schema contract, or external boundary. Unit tests validate pure transformations quickly, contract tests lock producer schemas, and integration tests run a minimal real engine or connector to expose boundary behavior mocks cannot reproduce.
- Unit duration
- typically seconds for the full fast suite
- Unit Tests
- Fast tests of a single transformation or decision with dependencies replaced by narrow fakes.
When to use it & trade-offs
What it is
Unit tests validate pure transformations quickly, contract tests lock producer schemas, and integration tests run a minimal real engine or connector to expose boundary behavior mocks cannot reproduce. A pytest parametrization feeds null currency, duplicate order_id, and a late event into normalize_orders; an integration test executes the resulting MERGE twice against test Postgres and expects the same count.
Why it matters: Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition. Freeze clocks, seed tiny representative fixtures, assert exact rows and schemas, test late and duplicate records, and exercise one narrow real Postgres/object-store/Kafka boundary in CI.
When to reach for it
- Test transforms and I/O boundaries with pytest fixtures, mocks, and integration checks against real engines.
- Choose pytest because it provides this capability: Pytest's de facto test runner with fixtures, parametrization, markers, and rich assertion introspection.
- Keep evidence for Unit duration at "typically seconds for the full fast suite" so the last production stage-Idempotence - repeat a run and expect no logical duplication-remains testable.
When to avoid or be careful
- Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition.
- Do not conflate Unit Tests with pytest. The second concept contributes different behavior: Fast tests of a single transformation or decision with dependencies replaced by narrow fakes.
- Release only after this guardrail is in place: Freeze clocks, seed tiny representative fixtures, assert exact rows and schemas, test late and duplicate records, and exercise one narrow real Postgres/object-store/Kafka boundary in CI.
pytest / Unit Tests / Integration - roles, limits, and operating cost
Strengths: Pytest's de facto test runner with fixtures, parametrization, markers, and rich assertion introspection. Unit tests validate pure transformations quickly, contract tests lock producer schemas, and integration tests run a minimal real engine or connector to expose boundary behavior mocks cannot reproduce.
Watch out: Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition.
Strengths: Fast tests of a single transformation or decision with dependencies replaced by narrow fakes. In this workflow it supports: Assert - rows, types, counts, and side effects.
Watch out: The operating cost is real: null, duplicate, late, invalid, and boundary values.
Strengths: Tests that combine real components-database, file reader, container, or broker-to catch protocol and state errors. Its output can be checked with Idempotent assertion.
Watch out: Operating guardrail: Freeze clocks, seed tiny representative fixtures, assert exact rows and schemas, test late and duplicate records, and exercise one narrow real Postgres/object-store/Kafka boundary in CI.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for pytest, and how would Unit duration reveal it?”
- “Why is Unit Tests distinct from this behavior: tests that combine real components-database, file reader, container, or broker-to catch protocol and state errors?”
- Production clue: Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition.
- Design probe: Freeze clocks, seed tiny representative fixtures, assert exact rows and schemas, test late and duplicate records, and exercise one narrow real Postgres/object-store/Kafka boundary in CI.
Benchmarks & rules of thumb
- Unit duration
- typically seconds for the full fast suite
- Fixture coverage
- null, duplicate, late, invalid, and boundary values
- Idempotent assertion
- run twice and preserve the expected business-key count
Common mistakes & follow-ups
Common interview mistakes
- Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition.
- Reporting success from a job exit code without checking Unit duration and Fixture coverage.
- Treating this example as complete without replay, rollback, or access boundaries: A pytest parametrization feeds null currency, duplicate
order_id, and a late event intonormalize_orders; an integration test executes the resulting MERGE twice against test Postgres and expects the same count.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: Pytest's de facto test runner with fixtures, parametrization, markers, and rich assertion introspection.
- What does the recovery plan do when this occurs? Mocking every dependency can prove only that your mocks were called, while tests using unordered file globs or current time become flaky and pass without validating a production partition.
- When is pytest insufficient for Fixture coverage, and what does adding Unit Tests cost in Unit duration?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Unit Testing & Integration Testing Data Pipelines (pytest) with data engineer interview Q&A and hands-on practice.