dbt - Analytics Engineering Certification Study Guide

    5: Implementing dbt tests

    This chapter covers the testing surface that prevents bad data and broken models from reaching production. It walks through the three test classes (generic, singular, and custom generic), the four built-in assumption tests, the two dominant package test catalogs (`dbt_utils` and `dbt_expectations`), severity and `store_failures` configuration, and the workflow integration that joins tests into the DAG via `dbt build` and CI Jobs.

    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 with dbt_utils and dbt_expectations package tests.
    • Configure severity, warn_if, error_if, store_failures, store_failures_as, limit, and where to tune test behavior.
    • Compose tests into a deployable workflow using dbt build DAG ordering, CI Jobs, slim CI with state:modified+, and defer-to-prod patterns.

    Executive Summary

    • Every dbt test compiles to a SELECT query 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 .sql files in tests/, and custom generic tests are user-defined macros that follow the test_<name>(model, column_name, ...) signature.
    • Severity controls whether a non-zero result halts the run (error) or only emits a warning (warn); warn_if and error_if add row-count thresholds, and store_failures persists the failing rows to a warehouse table for post-run investigation.
    • dbt build runs 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 with state:modified+, and defer-to-prod promotion patterns.

    Assumptions

    • The reader has authored generic tests in a schema.yml (or properties.yml) file before and has run dbt test from 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 SELECT syntax used by store_failures.
    • All YAML uses the data_tests: key introduced in dbt 1.8; the deprecated tests: 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.

    Sections in this chapter

    1. Free
    2. Free with account
    3. Free with account
    4. Free with account
    5. Free with account