Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
dbt Labs · dbt Analytics Engineering

Unit Tests, and Tests in the Workflow

dbt Analytics Engineering · Lesson 15 of 17 · 1:54:49

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the dbt Analytics Engineering Certification exam. In this lesson: unit tests in YAML with given and expect, input formats and fixtures, when to unit test, and where data tests and unit tests fit in development, CI, and production. Includes three original practice questions, with Orchardline Meal Kits, a fictional meal-kit company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

The weekend deliveries

Orchardline's delivery day logic uses date math and a window function, and last month, a change to it broke weekend deliveries, without any data test noticing. The team wants to check that logic on a few handmade rows, before the model is even built. This lesson covers unit tests, how to write them, when to add them, and where each kind of test belongs in the workflow.

Unit tests

A data test checks the data after a model is built, while a unit test checks the model's SQL logic, on a small set of static inputs, before the model is materialized. You give dbt mock rows for each input, and the rows you expect the model to return, and dbt compares the actual result with the expected one. Unit tests work on the SQL models of your current project, but not on materialized views, recursive SQL, or models that need introspective queries. If the model has several versions, the unit test runs on all of them by default. And when a unit test fails, dbt shows where the actual rows differ from the expected rows.

Writing a unit test

Unit tests are defined in YAML, in the models folder, never in the tests folder, which is kept for data tests. Each one names the model, then lists, under given, each input, with ref or source, and its mock rows. Under expect, you list the rows that the model should return, and you only need the columns that matter for the test. Rows can be written inline, as a dictionary, as CSV, or in SQL, or kept in fixture files, in a fixtures folder under the tests folder. Every ref and source of the model must appear as an input, and its direct parents must exist in the warehouse: building them with the empty flag is a cheap way to get there.

When to unit test

Add unit tests where the SQL is tricky: regular expressions, date math, window functions, or a case when statement with many branches. Also cover logic that had bugs before, edge cases not yet seen in your data, and models that you are about to refactor. Critical models deserve them too, like public or contracted models, or models that feed an exposure. But do not unit test functions of the warehouse itself, like min or max, since the warehouse already tests them.

Tests in the workflow

In development, write unit tests while you change the logic, and run dbt build on the model and everything downstream, which runs its unit tests, builds the model, then runs its data tests. In CI, dbt build on the modified models runs the same tests, before any merge. In production, keep the data tests, on sources and models, since the real data changes every day, but skip unit tests: their inputs are static, so running them again only costs compute. Exclude them with the exclude resource type option, or its environment variable, and run only unit tests with the test type unit selector. So unit tests protect the logic, data tests protect the data, and each one runs where it adds value.

Recap

Let's recap. A unit test checks a model's SQL logic on static mock rows, before the model is built. It is defined in YAML under the models folder, with the model, its given inputs, and the expected rows. Every ref and source must be an input, and the direct parents must exist, even if empty. Unit test tricky logic, past bugs, edge cases, and critical models. And run unit tests in development and CI, while production keeps the data tests.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A model computes its revenue column with a long case when, and the team wants to check the result on six tricky rows, before any build. What should they write?

    • A unit test with six given rows and their expected rows
    • An accepted_values test listing every revenue amount
    • A singular test that queries the built model after a run
    • A not_null test on revenue, with severity set to warn
    Show the answer

    A unit test with six given rows and their expected rows

    A unit test runs the model's logic on static rows, and compares the result with the expected rows, before the model is built. “An accepted_values test listing every revenue amount” is wrong, since accepted values checks the built data against a list. “A singular test that queries the built model after a run” is wrong, since a singular test needs the model to be built first. And “A not_null test on revenue, with severity set to warn” is wrong, since not null only looks for missing values.

  2. Compiling a new unit test fails with node not found: the model also refs a calendar seed that the test does not mention. What fixes it?

    • Remove the join to the seed from the model’s SQL
    • Move the unit test YAML into the tests folder
    • Run dbt seed before every unit test run
    • Add the seed as another input under given
    Show the answer

    Add the seed as another input under given

    Every ref and source of the model must be listed as an input, with its own mock rows, even when the test only needs a few. “Remove the join to the seed from the model’s SQL” would only change the logic that you want to test. “Move the unit test YAML into the tests folder” is wrong, since unit tests must be defined under the models folder. And “Run dbt seed before every unit test run” is wrong, since loading the seed does not make it an input of the test.

  3. Orchardline’s production job runs dbt build every night, and the team notices that its forty unit tests run there too. What should they do?

    • Delete the unit tests, since data tests replace them
    • Keep them, since production needs every test
    • Exclude the unit_test resource type from that job
    • Turn them into singular tests run in production
    Show the answer

    Exclude the unit_test resource type from that job

    Unit tests use static inputs, so running them in production only costs compute, and they belong in development and CI. “Delete the unit tests, since data tests replace them” is wrong, since data tests cannot check the logic on chosen edge cases. “Keep them, since production needs every test” is wrong, since the inputs of a unit test never change between runs. And “Turn them into singular tests run in production” is wrong, since singular tests check data, not logic on mock rows.