Skip to content

How to Test a Data Pipeline: Unit, Integration and Data Tests

A four-stage pipeline with check marks and a magnifier showing tests at each stage.

A data pipeline can run without errors and still produce wrong numbers. Tests are how you find out before a stakeholder does. The useful question is not whether to test, but which kinds of test catch which kinds of failure.

Unit tests: check the logic

A unit test runs one piece of transformation logic on a tiny, hand-written input and checks the output. If a function standardizes campaign names or converts currencies, feed it a few rows where you know the right answer, including awkward ones such as nulls, duplicates and unexpected formats. These tests are fast, so they can run on every change and tell you exactly which rule broke.

Data tests: check the data itself

Data tests run against real tables and assert things that should always be true. Common ones check that a key column is never null, that a key is unique, that a column only holds allowed values, and that every foreign key points to a row that exists. dbt provides these four as built-in tests. They catch problems that logic tests cannot, because the problem is in the incoming data, not in your code.

Integration tests: check the whole flow

An integration test runs the pipeline end to end on a small, representative sample and checks that the final tables look right. It catches failures between steps, such as a column renamed in one stage and still referenced by its old name in the next. Keep the sample small enough that the test runs in minutes, and include a few known tricky records.

Reconciliation checks: compare with the source

Compare row counts and key totals between the source and the destination at each stage, and investigate any gap you cannot explain. Rows lost to a bad join or a filter are among the most common silent failures, and a simple count comparison exposes them.

Test that reruns are safe

Run the pipeline twice on the same input and confirm that the output does not change and nothing is duplicated. This is the practical test for the property described in idempotent data pipelines, and it matters because real pipelines get retried.

Test the schema and the contract

Check that incoming data has the columns and types you expect, so a change at the source fails loudly at ingestion. This is the enforcement side of data contracts, and it is how you meet the changes described in schema evolution.

Use safe test data

Do not copy production personal data into test fixtures. Use small synthetic or masked samples, following the approaches in PII in data pipelines.

Run tests automatically

Tests only help if they run. Execute unit and integration tests on every code change in your continuous integration setup, and run data tests as part of each pipeline run, so a failing check stops bad data before it reaches reports. In my Olist E-Commerce Analytics Engineering project and Retail Analytics Warehouse project, automated data-quality tests run as part of the dbt build. To see how this fits with tooling, read dbt vs DuckDB.

Testing is not monitoring

Tests check what you thought to check, at the moment they run. They do not notice a source quietly going stale between runs or a slow drift in values. That job belongs to monitoring, covered in data observability vs data quality testing.

A quick checklist

  1. Do key transformation rules have unit tests with awkward inputs?
  2. Do key columns have not-null, unique and relationship tests?
  3. Is there an end-to-end run on a small sample?
  4. Do you compare row counts between source and destination?
  5. Do you run the pipeline twice to check that reruns are safe?
  6. Do tests run automatically on every change and every run?

If you want a pipeline built with tests from the start, see my ETL and ELT engineering service.

FAQ

What should I test first in a data pipeline?
Start with data tests on your most important tables: not-null and unique keys, and relationships between tables. They are cheap to add and catch many real problems.

What is the difference between a unit test and a data test?
A unit test checks your logic on small hand-made inputs. A data test checks real data against rules that should always hold, such as unique keys.

Can tests replace data monitoring?
No. Tests check specific rules when they run, while monitoring watches for freshness, volume and distribution changes between runs.

Related posts

Leave a comment

Your email address will not be published. Required fields are marked *