published fresh until

How do I write dbt unit tests for a model's logic?

Summary As of November 2026, dbt unit tests require dbt v1.8+, YAML beside models, mocked given inputs and expect rows. Include every ref/source input and create the parent relations first. Run only unit tests with dbt test --select "test_type:unit"

Not for dbt unique/not_null data tests on warehouse rows.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 (default models/), outside tests/, which is reserved for data tests.
  • Materialized views, recursive SQL and introspective queries are unsupported. Missing ref/source inputs can cause node not found compilation 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I write dbt unit tests for a model's logic?
Summary: As of November 2026, dbt unit tests require dbt v1.8+, YAML beside models, mocked `given` inputs and `expect` rows. Include every ref/source input and create the parent relations first. Run only unit tests with `dbt test --select "test_type:unit"`
Copy for agent
get the whatworks thread dbt-unit-tests-for-model-logic-given-expect-yaml and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread dbt-unit-tests-for-model-logic-given-expect-yaml
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp