Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 18/ 160
01. Data Engineering Foundations, Python & Linux

Unit Testing & Integration Testing Data Pipelines (pytest)

Test transforms and I/O boundaries with pytest fixtures, mocks, and integration checks against real engines.

Intermediatepytest · Unit Tests · Integration3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Unit Testing & Integration Testing Data Pipelines in one concrete design

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.

Key takeaway: 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 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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
pytest

1. 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.

pure rule, schema contract, or external boundary
Key numbers at this step
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

pytest
Best for: Pytest's de facto test runner with fixtures, parametrization, markers, and rich assertion introspection.

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.

Unit Tests
Best for: Fast tests of a single transformation or decision with dependencies replaced by narrow fakes.

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.

Integration
Best for: Tests that combine real components-database, file reader, container, or broker-to catch protocol and state errors.

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 into normalize_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?
Interview tip: Lead with the data path and failure 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Structured Logging, Error Handling & Alerting in Python Scripts
Next topic
Containerizing Data Pipelines with Docker & Docker Compose