What this thread does
Do first: For dbt v1.8 or later, define unit_tests in a .yml file under models/ (or your configured model-paths).
Short answer
Unit tests compare SQL-model logic against static inputs before model materialization. They support SQL models in the current project. The reference’s structure uses model, given inputs, and expect output. Unit-test reference.
What to do
1. Add a test under unit_tests with a unique name and the target model. This compact form uses the guide’s email-model example:
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}]}, {input: "ref('top_level_email_domains')", rows: [{tld: example.com}]}]
expect: {rows: [{email: cool@example.com, is_valid_email_address: true}]}
2. Adapt the model name and rows to your model’s inputs and expected result; include every ref and source dependency as an input. Alias tables when testing joins. 3. format accepts dict, csv, or sql; dict is the default; sql requires mock data for all columns. dict/csv fixtures only need relevant columns. Separate fixture files belong under a test-path fixtures subdirectory, for example tests/fixtures/my_unit_test_fixture.sql. 4. Parent relations must already exist. The guide’s example creates empty parents with dbt run --select "stg_customers top_level_email_domains" --empty. 5. Run dbt test --select "test_type:unit"; the guide’s model-specific example is dbt test --select "dim_customers,test_type:unit". Unit-test guide.
Limits and open questions
- Keep unit-test YAML under
model-paths(defaultmodels/), outsidetests/, which is reserved for data tests. - Materialized views, recursive SQL and introspective queries are unsupported. Missing ref/source inputs can cause
node not foundcompilation errors. - Ephemeral inputs require
format: sql; BigQuery STRUCT inputs must include all fields. - Incremental unit tests check query output destined for insertion/merging, not whether the framework merged it correctly.
- dbt Labs recommends development/CI execution rather than production for these static-input tests.
Not for: dbt unique/not_null data tests on warehouse rows.
Sources
Checked
2026-11-17: Read dbt Unit tests guide, dbt unit_tests property reference, dbt mock-data formats; no command was run.
Started from a post by @dbt Labs documentation, packaged by the editor. Original post