Articles · Data engineering · · 7 min read

dbt Tests and Data Contracts: Where Checks Belong

Tests and contracts solve different problems at different boundaries. Put the check in the wrong place and you buy alert fatigue, not confidence.

By Matthieu de La Mettrie · Founder, Skopura

A platform owner inherits two kinds of quality machinery. One is a pile of not_null and unique tests that accumulated in the models/ directory because somebody was nervous about a column. The other is a vague agreement with a source system team that they will tell you before they rename something. Neither is doing the job people think it is doing.

The distinction matters because the two mechanisms fail differently, are fixed by different people, and cost different amounts to maintain. Collapsing them into “data quality” is how you end up with a test suite that runs for an hour, fires every morning, and gets muted.

What is the actual difference between a contract and a test?

A contract binds two parties at a boundary. It says: this dataset will arrive with these columns, these types, this grain, this nullability, and no later than this point in the cycle. It has a producer who is accountable for meeting it and a consumer who is accountable for not depending on anything outside it. When a contract breaks, the fix is upstream of you, and the question is whose change caused it.

A test is an assertion you run against data you already have. It encodes an invariant you believe should hold after a transformation. When a test fails, the fix is usually yours — either the transform is wrong or the belief was wrong.

ContractTest
BindsTwo teams across a boundaryA model to its author's intent
FiresWhen the shape of arriving data changesWhen transformed output violates an invariant
Failure meansSomeone changed something without telling youLogic or assumption is wrong
Fixed byThe producer, or a negotiated amendmentThe model owner
Changes throughVersioning and a deprecation windowA pull request
Cost of absenceSilent breakage that surfaces in a reportWrong numbers that look plausible

If you write a contract term as a dbt test on a staging model, you have not created an agreement. You have created an alarm that rings in your own office about someone else’s decision. That is still better than nothing, but do not mistake it for an interface.

Which boundary does each check belong to?

There are three boundaries worth defending, and the checks at each have different shapes.

ingestion edge

staging

model edge

consumption edge

contract checks

transformation tests

consumption checks

Source system

Raw landing

Conformed models

Marts

Semantic layer and reports

Shape, type, freshness, grain

Invariants, referential integrity, reconciliation

Metric agreement, dimension coverage

At the ingestion edge, check shape and arrival, not meaning. Columns present, types as declared, grain as declared, partition arrived. Do not assert business rules here — you will be asserting them against data you did not create, and the first legitimate upstream change will make you look like the boy who cried wolf.

At the model edge, check invariants your transformation is responsible for producing. Did the join fan out? Does every fact row resolve to a dimension member? Does the sum of the allocated parts equal the unallocated total? These are the tests that earn their runtime, because they catch the class of bug that produces a number rather than an error.

At the consumption edge, check agreement. A semantic layer centralises metric definitions so separate reports agree — but only for the reports that go through it. Anything that reads the mart directly and recomputes the measure is outside that guarantee, and the check worth having is one that compares the two.

When should a failing check stop the run?

This is the decision that most test suites never make explicitly, which is why everything is a warning and nothing is read.

Should this check block the build?

Block. Fail the model and everything downstream of it; a stale correct table beats a fresh wrong one.

The quarantine path is the one teams skip, and it is the one that keeps a pipeline usable. Blocking a whole mart because a handful of rows have a null account code punishes every consumer for a problem affecting one of them. Quarantining gives you a record of what was dropped, which is also the only honest way to answer “why is the total lower than last week”.

Why do tests pass while the numbers are wrong?

Because the test was written by reading the transformation, not the requirement. If the model computes revenue net of a particular adjustment and the test asserts that revenue equals the same expression, the test will pass for every value the model can possibly produce. It is a tautology with a green tick next to it.

The tell is that the test and the model were written in the same sitting by the same person. The remedy is not more tests; it is a different source of truth for the assertion — a reconciliation against the source system’s own reported total, a row count that must match an independently produced control file, a relationship that must hold regardless of how the measure is defined.

The second common failure is a test that asserts the current state of the data rather than an intended invariant. Someone saw that a column had no nulls, added not_null, and now a legitimate business change breaks the build. Every check you add should answer: what would be true about the world if this failed? If the answer is “nothing in particular, the data just changed”, it is not an invariant.

What does a schema change actually cost at each boundary?

Additive changes upstream should cost nothing. If a new column breaks your staging model, you are selecting with a wildcard somewhere, and the contract is not doing the work — your parser is.

Breaking changes are where the versioning discipline pays. A contract with a version and a deprecation window lets the producer ship while you migrate. A contract without one means every upstream change is a negotiation conducted during an incident.

A staging model declaring the shape it depends onyaml
123456789101112131415161718192021
models:
- name: stg_orders
  config:
    contract:
      enforced: true
  columns:
    - name: order_id
      data_type: varchar
      constraints:
        - type: not_null
    - name: customer_id
      data_type: varchar
      constraints:
        - type: not_null
    - name: order_ts
      data_type: timestamp
    - name: order_total
      data_type: decimal(18,2)
  tests:
    - dbt_utils.unique_combination_of_columns:
        combination_of_columns: [order_id]

Declaring the contract on the model is not the same as having one with the producer, but it does something useful: it turns an implicit dependency into a reviewable artefact. When the enforced shape changes, the change shows up in a diff rather than in a dashboard.

What runs on every build, and what runs occasionally?

Test suites grow until they are slower than the transformations they guard, at which point someone starts running them with a selector that excludes the slow ones, and the slow ones quietly stop running at all.

Split by cost and by class. Invariants that are cheap on the incremental slice — uniqueness within the partition, not-null, referential integrity against the dimensions touched — run every build, scoped to the rows the build actually wrote. Incremental refresh processes only changed partitions rather than a full dataset, and your tests should respect the same scope. Testing the entire fact table on every run is paying a full-scan price for a partition-sized change.

Full-table reconciliations, cross-system agreement checks, and distribution comparisons run on a schedule, not on the critical path. They answer a different question — has something drifted — and drift does not need to block a deployment.

How do you retire a check?

Deliberately, and with the same review as adding one. Every test has an owner and a reason, and if neither can be stated, the test is noise that will eventually be muted along with something that mattered.

The practical habit: when a check fires and the resolution is “that was fine, the data legitimately changed”, that is not a false positive to be waved away. It is evidence that the assertion encoded the wrong thing. Either tighten it to the real invariant or delete it. A suite that only ever grows is a suite nobody trusts, and an untrusted suite is worse than none — it costs runtime and provides the feeling of coverage without the fact of it.

dbt · data-contracts · data-quality · platform-engineering

Share on LinkedIn ↗

Work with Skopura

Facing this bottleneck in your own estate?

Skopura builds production-grade lakehouse pipelines, evaluation harnesses, and provides fractional data leadership. Thirty-minute call, no deck.

Book a 30-min call →Email directly

Further reading

Data engineering · 9 min

Idempotent Pipelines and Backfills You Can Rerun

Backfills fail quietly when a pipeline isn't idempotent. Window parameters, write strategies, isolation, and the downstream refresh everyone forgets.

Data engineering · 8 min

Warehouse Cost Attribution in Snowflake and Fabric

Compute bills arrive as one number. Building a cost fact at run grain, choosing an allocation rule, and catching drift while the total stays flat.