Data quality · Quality Foundations

dbt tests for data quality

Easydbtdbt testschema testssource freshness

Interview question

How do dbt tests help with data quality?

How to answer

The short answer

dbt tests are SQL assertions that run against models and sources. They do not transform data; they return failing rows. Zero failing rows = pass.

Built-in (generic) tests

Usually declared in YAML next to columns:

YAML
models:
  - name: fct_orders
    columns:
      - name: order_id
        tests:
          - unique
          - not_null
      - name: customer_id
        tests:
          - relationships:
              to: ref('dim_customers')
              field: customer_id
      - name: amount
        tests:
          - dbt_utils.accepted_range:
              min_value: 0

Custom / singular tests

A SQL file under tests/ that should return 0 rows, e.g. "no shipped order without a ship date."

Commands

  • dbt test: run tests only
  • dbt build: run models and tests in order so a failed upstream test can block dependents
Output
dbt run   -> build relations
dbt test  -> assert expectations
dbt build -> run + test together (quality gate in the DAG)

Source freshness

dbt source freshness checks whether raw sources updated within warn_after / error_after windows.

Interview tip: Say dbt tests are cheap warehouse SQL checks co-located with models. List unique, not_null, relationships, plus custom SQL and source freshness.