dbt model contracts vs data tests: what each catches and where each runs in CI
Contracts guard the shape, data tests guard the values, unit tests guard the logic
In the dbt model contracts vs data tests question, the two do different jobs. A model contract checks the shape of a model (column names, data types, constraints) before dbt builds it. A data test checks the values in the data after the model is built. dbt's unit tests, added in dbt Core 1.8, cover a third job: checking your SQL logic against mock rows before real data is involved.
Key Takeaways
- •A contract fails a build when a model's columns or data types differ from what its YAML declares. It never looks at row values.
- •A data test queries the materialized table and fails on bad values: nulls, duplicates, orphaned keys, unexpected categories.
- •A unit test feeds fixed input rows to a model's SQL and compares the output to expected rows. It tests your logic, not your data.
- •Contracts and unit tests belong in every pull request. Data tests belong in PR builds on a slim slice and in every production run.
- •Contracts are the only one of the three that also detects breaking changes to a public model's interface when you compare against a prior state.
dbt model contracts vs data tests: what each one actually checks
The confusion comes from both features sounding like quality checks. They sit at different points in the build and look at different things.
A contract is switched on with contract: {enforced: true} in the model config. Once it is on, you must declare every column with a data_type. Before dbt creates the table, it compares the columns and types your SQL would return with the YAML. If a column is missing, extra, or the wrong type, the build fails and the model is never created. You can also attach constraints such as not_null, primary_key and unique to columns. The dbt docs state that how much of this the warehouse enforces depends on the platform, and some platforms enforce only not_null. Check the constraint support table for your warehouse before relying on a constraint as a guarantee.
A data test is a SELECT that returns the rows that violate a rule. Generic tests such as unique, not_null, accepted_values and relationships are configured in YAML, and singular tests are SQL files. They run after the model is materialized, against real data. A test passes when it returns zero rows.
A unit test is closer to software unit testing. You declare mock input rows for the model's refs and sources, plus the rows you expect back. dbt runs the model's SQL on those inputs only. In dbt build, unit tests run before the model builds, so a failing unit test stops the model and everything downstream.
| Question | Model contract | Data test | Unit test |
|---|---|---|---|
| What does it check? | Column names, data types, declared constraints | Values in the built table | Model logic on mock inputs |
| When does it run? | At build time, before the table is created | After the model is built | Before the model builds, on mock data |
| Needs real data? | No | Yes | No |
| Typical catch | A renamed column, a changed type, a dropped column | Duplicate keys, nulls, orphaned foreign keys | A wrong CASE branch, a bad join condition, a date edge case |
| Where it should run | Every PR and every production run | PR (slim) and every production run | Development and CI; dbt recommends against production |
What a contract catches that a data test cannot
Take a model whose YAML declares order_total as numeric and customer_id as bigint. Someone edits the SQL and casts order_total to a string. A not_null test on that column passes, because the strings are not null. The contract fails the build, because the type no longer matches.
The same is true for a dropped or renamed column. A data test on a column that no longer exists errors out, but only after the table has already been rebuilt without it and possibly after downstream models have started. The contract fails earlier, at the model that made the change, and the previous table stays in place.
Contracts also feed breaking-change detection. When a model has an enforced contract, dbt compares the new version with the previous state (the manifest of a prior run) and flags changes such as removed columns or changed data types as breaking. The dbt docs on model contracts describe this alongside model versions, which is how you ship a breaking change on purpose without breaking consumers. Neither data tests nor unit tests do this. This makes contracts the right tool for any model other teams, dashboards or ML features depend on.
What a data test catches that a contract cannot
A contract has no opinion about values. If an upstream source starts sending duplicate order_id values, the column name and type are still correct, so the contract passes. A unique test fails. The same goes for a relationships test that finds order lines pointing at customers that do not exist, or an accepted_values test that sees a new status code.
Data tests are also the only one of the three that watches production data as it changes. A contract is evaluated every time the model builds, but it only knows about columns and types. If a pipeline runs fine while the data drifts, data tests are what raise the alarm. These are the cases behind questions like "Why did row counts drop 40% overnight?", which a shape check can never answer.
Where unit tests fit between the two
Unit tests answer a question neither of the others can: is my transformation correct? If a model applies a discount rule, you can write three input rows, one for each branch of the rule, and assert the output. That test passes or fails the same way regardless of what is in your warehouse today, so it does not flake when data changes.
The dbt unit tests docs recommend running them in development and CI, not production. In production the logic has already been validated, and the mock-input tests add cost without telling you anything about today's data. In production you can exclude them with --exclude-resource-type unit_test.
Where each one runs in CI
This is our opinion on a sensible split, based on how the three features behave. Adjust it to your project size.
- •Pull request, fast stage: run
dbt build --select state:modified+ --defer --state <prod-artifacts>so only changed models and their children build. Contracts are checked as each changed model builds, and the comparison against the production manifest is where breaking-change detection happens. - •Pull request, logic stage: run
dbt test --select "test_type:unit"for the models you changed. These need no warehouse data, so they are cheap and deterministic. - •Pull request, data stage: the
state:modified+build also runs data tests against the CI schema. Keep these to the models you touched so the run stays short. - •Production run: use
dbt build --exclude-resource-type unit_test. Contracts apply on every build, and data tests run against live data after each model.
The failure mode we see most is running every check everywhere: full data tests on every PR, unit tests in production, no state comparison. CI gets slow, people start ignoring failures, and the check that would have caught the real problem is buried in noise. Scoping each check to the point where it carries information is what keeps CI fast enough to trust.
Avoiding duplicate checks
Once a model has an enforced contract, a not_null or unique constraint declared in the contract can replace the matching generic test only if your warehouse actually enforces that constraint. Where it does not, the constraint is metadata and you still need the data test. If you duplicate them anyway, you pay for the same query twice on every run. Decide per warehouse which constraints are enforced, then delete the redundant tests deliberately rather than by guesswork.
Contracts also cost you something. Every column has to be declared with a type, so wide models take real YAML to maintain. Our view is that contracts earn that cost on models other people consume (marts, public models, anything in a mesh) and are usually not worth it on intermediate models.
Running these checks from Claude Code
Most of this setup is mechanical: declaring column types, generating YAML for contracts, adding unit-test fixtures, and wiring the selectors into CI. That is the kind of work coding agents handle well when they can see the project. Our Pipeline Agent works with dbt alongside Airflow, Dagster, Prefect and Fivetran, and as an MCP server it is available inside Claude Code and Cursor, so a request like "What's the blast radius if I drop this column?" can be answered while you are editing the model.
We covered how our agents share context across pipelines, schema and quality in other posts on this site. The relevant point here is that a contract failure and a data test failure are different signals. Whoever (or whatever) reviews a pull request should see which one fired, because one means the interface changed and the other means the data did.
Frequently Asked Questions
Do dbt model contracts replace data tests? No. A contract checks column names, data types and declared constraints before the model is built. It does not check values, so duplicates, orphaned keys and unexpected categories still need data tests. The two overlap only where your warehouse enforces a constraint such as not_null.
Do model contracts run when I run dbt test? Contracts are enforced when the model is built, not as a separate test. A build with dbt run or dbt build performs the contract check before the table is created. dbt test runs your data and unit tests.
Should unit tests run in production? The dbt unit tests docs recommend running them in development and CI only. In production, exclude them with --exclude-resource-type unit_test, since they test logic on mock data rather than the data you have.
Which dbt check catches a breaking column change? An enforced contract. When you compare against a prior state, dbt flags changes such as a removed column or a changed data type on a contracted model as breaking, and model versions let you release the change deliberately.
Do I need contracts on every model? We would not add them everywhere. Declaring every column and type is real maintenance work, so we put contracts on models that other teams, dashboards or downstream systems rely on, and leave intermediate models without them.