dbt
Intermediate 14 min read dbt

dbt Tests

Every model ships with its contract

dbt tests are assertions on your models and sources — uniqueness, not-null, accepted values, and relationships. They run with every build and fail loudly on bad data.

dbttestsuniquenessnot nullaccepted valuesrelationshipsassertions
Before we start

What you will learn

  • Write generic tests in YAML for columns
  • Write a singular test with raw SQL
  • Interpret a test failure and decide between fix data or fix model
Introduction

What is this?

dbt tests are assertions run after building models. Generic tests are declared in YAML: unique, not_null, accepted_values, relationships against another table. Singular tests are hand-written SQL that must return zero rows. A failing test blocks the promotion of a green build.

Motivation

Why do you need it?

  • Tests convert a 'request' for clean data into a checked contract.
  • Failures point at the exact offending rows, fixing is fast.
  • Tests run the same in dev, CI, and prod, so trust is portable.
Think about it

Mental model

Imagine

A QA checklist on the assembly line

Think of

A dbt test on each model

Each part ships only after its checks pass; you do not discover a missing bolt after the car is sold.

Mechanics

How it works

  • Declare generic tests in the model's YAML: unique, not_null, accepted_values, relationships.
  • Write singular tests as SELECTs that must return no rows.
  • dbt test compiles each test to SQL (COUNT over the failing set).
  • Failures are stored, reported, and, in CI, gate the promotion.

Tests execute after builds and gate data into production.

Build
Test
Gate
Ship
Real world

In production

  • A unique test on order_id catches pipeline retries duplicating loads.
  • An accepted_values test on a status enum catches every new code before dashboards break.
  • Relationship tests verify every order row resolves to a known customer.
Guidelines

Best practices

  • Test the contract, not the whole table: keys, money, status enums.
  • Run tests on every build and in CI, and page on failures in production.
  • Name tests and give failing ones descriptions so the alert is actionable at 3am.
Watch out

Common mistakes

  • Declaring a uniqueness test on a column that is not unique at the model grain.
  • Hand-writing a singular test for something a generic test already covers.
  • Letting a long-failing test fester instead of pruning or fixing it.
When things break

Debugging tips

  • A unique failure on 0.01% of rows is nearly always a retry duplicate — inspect them before rewriting the pipeline.
  • If a relationship test fails after a join change, the join multiplied rows: fix the grain, not the test.
Interviews

Interview checkpoint

Practice

Hands-on lab

Test a Mart with Generic and Singular Tests
35 min · intermediate
In progress
Objectives
  • Declare unique, not-null, and accepted-values tests in YAML
  • Write a singular test as a failing-row SELECT
  • Run tests and read the failure report

Test a Mart with Generic and Singular Tests

35 min · intermediate

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Generic dbt tests are declared in

2

A singular test must return

Go deeper

Official documentation