tessl i github:dbt-labs/dbt-agent-skills --skill adding-dbt-unit-testUse when adding unit tests for a dbt model or practicing test-driven development (TDD) in dbt
In software programming, unit tests validate small portions of your functional code, and they work much the same way in dbt. dbt unit tests allow you to validate your SQL modeling logic on a small set of static inputs before you materialize your full model in production. dbt unit tests enable test-driven development, benefiting developer efficiency and code reliability.
Unit tests allow enforcing that all the unit tests for a model pass before it is materialized (i.e. dbt won't materialize the model in the database if any of its unit tests do not pass).
You should unit test a model:
More examples:
case when statements when there are many whensCases we don't recommend creating unit tests for:
min(), etc.dbt unit test uses a trio of the model, given inputs, and expected outputs (Model-Inputs-Outputs):
model - when building this modelgiven inputs - given a set of source, seeds, and models as preconditionsexpect output - then expect this row content of the model as a postconditionSelf explanatory -- the title says it all!
format if different than the default (YAML dict).
formats for unit tests" section below to determine which format to use.Tip: Use dbt show to explore existing data from upstream models or sources. This helps you understand realistic input structures. However, always sanitize the sample data to remove any sensitive or PII information before using it in your unit test fixtures.
# Preview upstream model data
dbt show --select upstream_model --limit 5format if different than the default (YAML dict).
formats for unit tests" section below to determine which format to use.Suppose you have this model:
-- models/hello_world.sql
select 'world' as helloMinimal unit test for that model:
# models/_properties.yml
unit_tests:
- name: test_hello_world
# Always only one transformation to test
model: hello_world
# No inputs needed this time!
# Most unit tests will have inputs -- see the "real world example" section below
given: []
# Expected output can have zero to many rows
expect:
rows:
- {hello: world}Run the unit tests, build the model, and run the data tests for the hello_world model:
dbt build --select hello_worldThis saves on warehouse spend as the model will only be materialized and move on to the data tests if the unit tests pass successfully.
Or only run the unit tests without building the model or running the data tests:
dbt test --select "hello_world,test_type:unit"Or choose a specific unit test by name:
dbt test --select test_is_valid_email_addressdbt Labs strongly recommends only running unit tests in development or CI environments. Since the inputs of the unit tests are static, there's no need to use additional compute cycles running them in production. Use them when doing development for a test-driven approach and CI to ensure changes don't break them.
Use the --resource-type flag --exclude-resource-type or the DBT_EXCLUDE_RESOURCE_TYPES environment variable to exclude unit tests from your production builds and save compute.
unit_tests:
- name: test_order_items_count_drink_items_with_zero_drinks
description: >
Scenario: Order without any drinks
When the `order_items_summary` table is built
Given an order with nothing but 1 food item
Then the count of drink items is 0
# Model
model: order_items_summary
# Inputs
given:
- input: ref('order_items')
rows:
- {
order_id: 76,
order_item_id: 3,
is_drink_item: false,
}
- input: ref('stg_orders')
rows:
- { order_id: 76 }
# Output
expect:
rows:
- {
order_id: 76,
count_drink_items: 0,
}For more examples of unit tests, see references/examples.md
materialized view materialization.expect output for final state of the database table after inserting/merging for incremental models.expect output for what will be merged/inserted for incremental models.model-paths directory (models/ by default)test-paths directory (tests/fixtures by default)ref or source model references in the unit test configuration as inputs to avoid "node not found" errors during compilation.format: sql for the ephemeral model input.join logicUse inputs in your unit tests to reference a specific model or source for the test:
input:, use a string that represents a ref or source call:
ref('my_model') or ref('my_model', v='2') or ref('dougs_project', 'users')source('source_schema', 'source_name')rows: []
ref or source dependency, but its values are irrelevant to this particular unit test. Just beware if the model has a join on that input that would cause rows to drop out!models/schema.yml
unit_tests:
- name: test_is_valid_email_address # this is the unique name of the test
model: dim_customers # name of the model I'm unit testing
given: # the mock data for your inputs
- input: ref('stg_customers')
rows:
- {email: cool@example.com, email_top_level_domain: example.com}
- {email: cool@unknown.com, email_top_level_domain: unknown.com}
- {email: badgmail.com, email_top_level_domain: gmail.com}
- {email: missingdot@gmailcom, email_top_level_domain: gmail.com}
- input: ref('top_level_email_domains')
rows:
- {tld: example.com}
- {tld: gmail.com}
- input: ref('irrelevant_dependency') # dependency that we need to acknowlege, but does not need any data
rows: []
...formats for unit testsdbt supports three formats for mock data within unit tests:
dict (default): Inline YAML dictionary values.csv: Inline CSV values or a CSV file.sql: Inline SQL query or a SQL file.To see examples of each of the formats, see references/examples.md
formatdict format by default, but fall back to another format as-needed.sql format when testing a model that depends on an ephemeral modelsql format when unit testing a column whose data type is not supported by the dict or csv formats.csv or sql formats when using a fixture file. Default to csv, but fallback to sql if any of the column data types are not supported by the csv format.sql format is the least readable and requires suppling mock data for all columns, so prefer other formats when possible. But it is also the most flexible, and should be used as the fallback in scenarios where dict or csv won't work.Notes:
sql format you must supply mock data for all columns whereas dict and csv may supply only a subset.sql format allows you to unit test a model that depends on an ephemeral model -- dict and csv can't be used in that case.The dict format only supports inline YAML mock data, but you can also use csv or sql either inline or in a separate fixture file. Store your fixture files in a fixtures subdirectory in any of your test-paths. For example, tests/fixtures/my_unit_test_fixture.sql.
When using the dict or csv format, you only have to define the mock data for the columns relevant to you. This enables you to write succinct and specific unit tests. For the sql format all columns need to be defined.
There are platform-specific details required if implementing on (Redshift, BigQuery, etc). Read the caveats file for your database (if it exists):
Unit tests are designed to test for the expected values, not for the data types themselves. dbt takes the value you provide and attempts to cast it to the data type as inferred from the input and output models.
How you specify input and expected values in your unit test YAML definitions are largely consistent across data warehouses, with some variation for more complex data types.
Read the data types file for your database:
By default, all specified unit tests are enabled and will be included according to the --select flag.
To disable a unit test from being executed, set:
config:
enabled: falseThis is helpful if a unit test is incorrectly failing and it needs to be disabled until it is fixed.
When a unit test fails, there will be a log message of "actual differs from expected", and it will show a "data diff" between the two:
actual differs from expected:
@@ ,email ,is_valid_email_address
→ ,cool@example.com,True→False
,cool@unknown.com,FalseThere are two main possibilities when a unit test fails:
It takes expert judgement to determine one from the other.
--empty flagThe direct parents of the model that you’re unit testing need to exist in the warehouse before you can execute the unit test. The run and build commands supports the --empty flag for building schema-only dry runs. The --empty flag limits the refs and sources to zero rows. dbt will still execute the model SQL against the target data warehouse but will avoid expensive reads of input data. This validates dependencies and ensures your models will build properly.
Use the --empty flag to build an empty version of the models to save warehouse spend.
dbt run --select "stg_customers top_level_email_domains" --empty| Mistake | Fix |
|---|---|
| Testing simple SQL using built-in functions | Only unit test complex logic: regex, date math, window functions, multi-condition case statements |
| Mocking all columns in input data | Only include columns relevant to the test case |
Using sql format when dict works | Prefer dict (most readable), fall back to csv or sql only when needed |
Missing input for a ref or source | Include all model dependencies to avoid "node not found" errors |
| Testing Python models or snapshots | Unit tests only support SQL models |
There are similar concepts that dbt's model, given, expect lines up with (Hoare triple, Arrange-Act-Assert, Gherkin, What's in a Story?, etc):
| dbt unit test | Description | Hoare triple | Arrange-Act-Assert | Gherkin | What's in a Story? |
|---|---|---|---|---|---|
model | when running the command for this model | Command | Act | When | Event |
given | given these test inputs as preconditions | Precondition | Arrange | Given | Givens |
expect | then expect this output as a postcondition | Postcondition | Assert | Then | Outcome |
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.