Testing and Contracts
11. What data tests do you put on a mart, and how do you control their severity?
The four built-in generic tests are unique, not_null, accepted_values and relationships. Since dbt 1.8 they are declared under data_tests: to distinguish them from unit tests.
On a mart, test the primary key for uniqueness and nulls at the declared grain, foreign keys with relationships, and status columns with accepted_values.
models:
- name: fct_orders
columns:
- name: order_id
data_tests:
- unique
- not_null
- name: customer_id
data_tests:
- not_null:
config:
severity: error
warn_if: ">0"
error_if: ">100"
where: "ordered_at >= current_date - 7"
The where config limits a test to recent rows, which keeps tests cheap on large tables. store_failures writes failing rows to a table so someone can inspect them.
A strong candidate distinguishes tests that should block a deploy from tests that should only alert, and does not leave hundreds of warnings that nobody reads.
12. What is a unit test in dbt, and how is it different from a data test?
A data test runs against real warehouse data after a model builds and asks "is this data valid?"
A unit test, available since dbt 1.8, feeds static input rows to a model's SQL and compares the output to expected rows, asking "is this logic correct?" before the model is built.
unit_tests:
- name: test_is_valid_email_address
model: dim_customers
given:
- input: ref('stg_customers')
rows:
- {email: cool@example.com, email_top_level_domain: example.com}
- {email: badgmail.com, email_top_level_domain: gmail.com}
- input: ref('top_level_email_domains')
rows:
- {tld: example.com}
- {tld: gmail.com}
expect:
rows:
- {email: cool@example.com, is_valid_email_address: true}
- {email: badgmail.com, is_valid_email_address: false}
Per the unit test docs, you only list the columns the test needs, every ref and source must appear as an input, and unit tests must live in YAML under models/. dbt Labs recommends running them in development and CI only.
Limitations worth knowing: SQL models only, no materialized views, no recursive SQL and no introspective queries. Use them for regex, date math, window functions and case statements with edge cases.
13. How do you unit test the incremental branch of a model?
Override is_incremental and supply the current table contents as the this input. The expected rows are what the run would insert or merge, not the final table.
unit_tests:
- name: orders_incremental_mode
model: fct_orders
overrides:
macros:
is_incremental: true
given:
- input: ref('stg_orders')
rows:
- {order_id: 1, updated_at: 2026-11-01}
- {order_id: 2, updated_at: 2026-11-05}
- input: this
rows:
- {order_id: 1, updated_at: 2026-11-01}
expect:
rows:
- {order_id: 2, updated_at: 2026-11-05}
A good candidate writes a second test with is_incremental: false to cover the first build.
The value here is catching the filter bugs from question 8 before they ship: off-by-one comparisons, wrong timestamp column, or a lookback window that does not apply.
14. What do model contracts and versions give you?
A contract (contract: {enforced: true}) makes dbt check before building that the model returns exactly the declared columns and data types, and it applies supported constraints such as not_null and primary_key in the DDL.
A downstream team can rely on the shape of the table.
models:
- name: dim_customers
config:
contract:
enforced: true
columns:
- name: customer_id
data_type: int
constraints:
- type: not_null
- type: primary_key
- name: email
data_type: varchar
Versions handle breaking changes: you publish dim_customers v2 next to v1, consumers migrate on their schedule with ref('dim_customers', v=2), and v1 gets a deprecation date. dbt 1.12 added the opt-in latest_version_pointer_enabled_by_default flag, which creates a pointer view for the latest version.
A strong candidate notes that most warehouses do not enforce primary_key constraints, so the unique test is still needed.
15. A test fails in production at 6 a.m. What happens next?
The answer reveals how the candidate runs a project. The steps a strong candidate describes:
- Decide blast radius. With
dbt build, a failing test skips downstream models, so dashboards show yesterday's data instead of wrong data. That is usually the right failure mode.
- Look at the failing rows through
store_failures or the compiled test SQL in target/.
- Classify the cause: an upstream source changed, a late or duplicated load, or a model bug.
- Fix and rerun with
dbt retry, which reruns from the point of failure using the previous run's results.
- Prevent repeats: add a source freshness check, a unit test, or a test on the staging model closer to the cause.
A weak answer lowers the test's severity to warn to make the job green.