Skip to content

Tests

Course map · Previous: Sources · Next: Documentation

A test is a query for the bad rows: 0 rows means pass

Generic tests (unique, not_null, accepted_values, relationships) live in the *_schema.yml files. Singular tests are SQL files in tests/ that return the rows that break a rule.

Course videos may show accepted_values and relationships options directly under the test name. dbt 1.10+ expects them under arguments:, as this project does.

Open Run (from oms_dbt_proj)
models/**/*_schema.yml, tests/ dbt test --profiles-dir .

Learn

A test is a select that returns the rows that break a rule. Zero rows means it passes. There are three kinds:

  • Generic tests: reusable, declared on a column in YAML. dbt ships four:
Test Checks
unique every value in the column is unique
not_null no value is null
accepted_values every value is in a given list
relationships every value exists in another model's column
  • Singular tests: one-off SQL files in tests/, for rules no generic test covers.
  • Unit tests: check a model's logic against rows you write. See chapter 3.
dbt test --profiles-dir .                              # every test
dbt test --profiles-dir . --select test_type:generic   # generic only
dbt test --profiles-dir . --select test_type:singular  # singular only
dbt test --profiles-dir . --select dim_customers       # one model's tests

dbt build runs each model's tests right after building it and skips everything downstream of a failure, so bad data never spreads.

Run it in the browser

A test is a query that returns the rows that break a rule. Zero rows means it passes. This is roughly what the unique test on order id runs:

select id, count(*) as n
from raw.jaffle_shop.orders
group by id
having count(*) > 1

Now write the relationships test yourself: return every order whose user_id has no matching id in raw.jaffle_shop.customers. It should pass.

select orders.*
from raw.jaffle_shop.orders as orders

Quiz

A dbt test passes when its query returns…

  • exactly one row
  • zero rows
  • at least one row
Answer

Zero rows. Every test selects the failing rows. No failing rows, no failure.

Which generic test catches an order whose customer_id doesn't exist in the customers model?

  • unique
  • accepted_values
  • relationships
Answer

relationships. It checks referential integrity: every value must exist in another model's column. fct_orders.customer_id has one in finance_schema.yml.

How is dbt build safer than dbt run followed by dbt test?

  • It tests each model right after building it and skips everything downstream of a failure
  • It runs the tests before building anything
  • It only builds models that have tests
Answer

It stops bad data spreading. With run then test, every model is built before any test runs, so downstream models already used the bad data.

Try it

Write a singular test that fails if any customer in dim_customers has a negative number_of_orders.

Hint

A singular test is a .sql file in tests/ that selects the bad rows. Use {{ ref('dim_customers') }}, not the table name.

Answer

Create tests/assert_dim_customers_orders_not_negative.sql:

select * from {{ ref('dim_customers') }}
where number_of_orders < 0
dbt test --profiles-dir . -s assert_dim_customers_orders_not_negative

The test passes: it returns 0 rows.