Skip to main content

About unit tests property

Unit tests validate your SQL modeling logic on a small set of static inputs before you materialize your full model in production. They support a test-driven development approach, improving both the efficiency of developers and reliability of code.

To run only your unit tests, use the command: dbt test --select test_type:unit

Before you begin

  • We currently only support unit testing SQL models.
  • We currently only support adding unit tests to models in your current project.
  • If your model has multiple versions, by default the unit test will run on all versions of your model. Read unit testing versioned models for more information.
  • Unit tests must be defined in a YML file in your models/ directory.
  • If you want to unit test a model that depends on an ephemeral model, you must use format: sql for that input.

unit_tests:
- name: <test-name> # this is the unique name of the test
model: <model-name>
versions: #optional
include: <list-of-versions-to-include> #optional
exclude: <list-of-versions-to-exclude> #optional
config:
meta: {dictionary}
tags: <string> | [<string>]
given:
- input: <ref_or_source_call> # optional for seeds
format: dict | csv | sql
# either define rows inline or name of fixture
rows: {dictionary} | <string>
fixture: <fixture-name> # sql or csv
- input: ... # declare additional inputs
expect:
format: dict | csv | sql
# either define rows inline of rows or name of fixture
rows: {dictionary} | <string>
fixture: <fixture-name> # sql or csv
overrides: # optional: configuration for the dbt execution environment
macros:
is_incremental: true | false
dbt_utils.current_timestamp: <string>
# ... any other jinja function from https://docs.getdbt.com/reference/dbt-jinja-functions
# ... any other context property
vars: {dictionary}
env_vars: {dictionary}
- name: <test-name> ... # declare additional unit tests

Examples


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}
expect: # the expected output given the inputs above
rows:
- {email: cool@example.com, is_valid_email_address: true}
- {email: cool@unknown.com, is_valid_email_address: false}
- {email: badgmail.com, is_valid_email_address: false}
- {email: missingdot@gmailcom, is_valid_email_address: false}


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')
format: csv
rows: |
tld
example.com
gmail.com
expect: # the expected output given the inputs above
format: csv
fixture: valid_email_address_fixture_output


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')
format: sql
rows: |
select 'example.com' as tld union all
select 'gmail.com' as tld
expect: # the expected output given the inputs above
format: sql
fixture: valid_email_address_fixture_output

0