Learning Objectives
By the end of this chapter, you will be able to:
- Author and apply generic, singular, and custom generic tests against models, sources, seeds, and snapshots.
- Test the four foundational assumptions (
unique,not_null,accepted_values,relationships) and extend coverage withdbt_utilsanddbt_expectationspackage tests. - Configure
severity,warn_if,error_if,store_failures,store_failures_as,limit, andwhereto tune test behavior. - Compose tests into a deployable workflow using
dbt buildDAG ordering, CI Jobs, slim CI withstate:modified+, and defer-to-prod patterns.
Executive Summary
- Every dbt test compiles to a
SELECTquery that the adapter runs against the warehouse; the test passes when the query returns zero rows and fails (or warns) when it returns one or more. - Generic tests are reusable across many columns through YAML configuration, singular tests are one-off
.sqlfiles intests/, and custom generic tests are user-defined macros that follow thetest_<name>(model, column_name, ...)signature. - Severity controls whether a non-zero result halts the run (
error) or only emits a warning (warn);warn_ifanderror_ifadd row-count thresholds, andstore_failurespersists the failing rows to a warehouse table for post-run investigation. dbt buildruns models, tests, snapshots, and seeds in DAG order, gating downstream nodes on the success of upstream tests; this ordering is the foundation of CI Jobs, slim CI withstate:modified+, and defer-to-prod promotion patterns.
Assumptions
- The reader has authored generic tests in a
schema.yml(orproperties.yml) file before and has rundbt testfrom the CLI or the dbt Cloud IDE. - The reader works on a project with at least one source declaration, at least one staging model, and at least one mart model, and uses a warehouse adapter that supports the
CREATE TABLE AS SELECTsyntax used bystore_failures. - All YAML uses the
data_tests:key introduced in dbt 1.8; the deprecatedtests:alias is acknowledged where relevant but not used in authored examples. - Sample code targets a Snowflake, BigQuery, or Databricks adapter unless an example specifically calls out a different engine.
