dbt offers three classes of tests, and each class trades expressiveness for reusability in a different way. Generic tests are macros that take a model and (usually) a column_name plus keyword arguments, then return a SELECT statement; they are applied through YAML configuration, often against dozens of columns, and they form the dominant share of any production project's test surface. Singular tests are standalone .sql files in tests/ that express a single, scenario-specific assertion: the kind of business rule that does not generalize cleanly across columns or models. Custom generic tests are user-authored macros stored in tests/generic/ (preferred since dbt 1.0) or macros/ (still supported), and they exist to take a one-off singular test and lift it into a reusable shape once you see it written more than twice. Choosing between the three is the first decision in any testing strategy, and the choice is more about the assertion's reuse profile than its complexity.
The mental model that unifies all three is the same: every test, regardless of class, compiles to a SELECT statement that the adapter runs against the warehouse. The test passes when the query returns zero rows and fails when it returns one or more. There is no second compiled form, no special pass/fail protocol, no out-of-band side channel: a test in dbt is just a query with an inverted pass condition. This is why you can read the compiled SQL in target/compiled/<project>/tests/ and reproduce a failing test in any SQL editor by running that exact query and inspecting the rows it returns. It is also why the --store-failures flag works the way it does: dbt persists those returned rows into an audit schema, because they are exactly the rows that broke the assertion.
Generic tests applied through YAML
A generic test is invoked by adding it under a data_tests: key inside a columns: or models: block in YAML. dbt ships with four built-in generic tests (unique, not_null, accepted_values, relationships) and resolves them through the standard macro lookup path: first the local project's tests/generic/ directory, then any installed package's macros/ (or tests/generic/) directory. The four built-ins live inside the dbt namespace and need no package prefix; package tests do require a prefix (dbt_utils.equal_rowcount).
version: 2
models:
- name: stg_payments
description: One row per payment received from the upstream payments source.
columns:
- name: payment_id
description: Surrogate key for the payment.
data_tests:
- unique
- not_null
- name: payment_method
description: Payment instrument used at checkout.
data_tests:
- not_null
- accepted_values:
values: ['credit_card', 'coupon', 'bank_transfer', 'gift_card']
- name: order_id
data_tests:
- relationships:
to: ref('stg_orders')
field: order_id
A YAML test definition becomes a node in the manifest with a name of the form <test_name>_<model>_<column>_<arg_hash>. The node has its own DAG dependencies (a relationships test depends on both the model under test and the referenced model), runs in a DAG-aware order under dbt build, and writes its own row to run_results.json with a timing and a status of pass, warn, fail, or error. The error status is reserved for compile or execution failures (the warehouse rejected the query); fail means the query ran and returned at least one row.
Singular tests as standalone SQL files
A singular test is any .sql file in the tests/ directory whose body returns the rows that violate an assertion. There is no macro signature, no column_name argument, no YAML invocation: the file is the test, and the filename (without extension) is the test's name. The compiled SQL is what runs; the file passes when zero rows come back.
-- tests/assert_payment_amount_never_negative.sql
-- Every payment must be a non-negative amount.
-- This assertion does not generalize across columns, so a singular test is the right shape.
with payments as (
select * from {{ ref('stg_payments') }}
)
select
payment_id,
amount_cents
from payments
where amount_cents < 0
Singular tests are the right home for assertions that quote two or more models, mix business logic with column checks, or apply only to a specific scenario. A test that checks "the daily revenue total in fct_orders matches the daily revenue total in fct_payments within a tolerance of one cent" is a singular test: it joins two marts, applies a tolerance, and does not naturally fit a column_name shape. The temptation to push such assertions into a generic test for "reusability" is the most common authoring mistake on this surface; the resulting macro takes a string of SQL as an argument and reads worse than the singular test would have.
Custom generic tests as authored macros
When a singular test reads cleanly but appears more than twice across the project, lift it to a custom generic test. The shape is a macro named test_<name> (or, since dbt 1.0, a macro named <name> placed in tests/generic/) that accepts model and an optional column_name, plus any keyword arguments the assertion needs. The macro body returns a SELECT statement that yields the failing rows.
-- tests/generic/test_positive_value.sql
{% test positive_value(model, column_name) %}
select
{{ column_name }} as failing_value
from {{ model }}
where {{ column_name }} <= 0
{% endtest %}
Once the macro is on disk, the test is invoked from YAML exactly like a built-in:
version: 2
models:
- name: stg_payments
columns:
- name: amount_cents
data_tests:
- positive_value
The macro lookup path matters here. If the same test name exists in the local project's tests/generic/ directory and in an installed package, the local project wins. This is the standard override pattern: copy a package test into tests/generic/, edit the body, and dbt picks up the local version without any further configuration. The reverse case (a package overriding a built-in) is rejected at parse time and produces a compilation error naming the conflict.
💡 Exam Trap: YAML defines a test under the
data_tests:key in dbt 1.8 and later, but the deprecatedtests:alias still compiles. A schema file that mixes the two keys at different levels parses cleanly but is a maintenance hazard. Items that quote a YAML block usingtests:are testing whether you recognize the alias, not whether you should still author it.
⚠️ Anti-Pattern: Folding a multi-model assertion into a custom generic test by passing raw SQL as a
conditionargument. The resulting macro reads as a thin wrapper around{{ condition }}and obscures the assertion. Author it as a singular test intests/instead; lift it to a custom generic only after the same shape appears a third time with only the table names changing.
🎯 Scenario: A reviewer rejects a
tests/assert_no_orphan_payments.sqlsingular test that joinsstg_paymentstostg_ordersand selects the orphan rows, claiming a genericrelationshipstest would replace it. They are right: the assertion is exactly whatrelationshipsis for, and a YAML invocation ofrelationships: { to: ref('stg_orders'), field: order_id }is the canonical shape. The author confused "this is custom SQL" with "this needs a custom test"; the assertion does generalize, so the generic test is the correct home.
Test selection and the compiled artifact
dbt test runs all tests by default, dbt test --select <selector> filters to a subset, and --exclude <selector> removes nodes from a selection. The selector grammar mirrors the model selector grammar: stg_payments matches every test attached to that model, tag:critical matches every test carrying the critical tag, test_type:singular matches only singular tests, and state:modified+ matches tests on modified models plus their downstream nodes. Combining selectors with , (intersect) and (union) lets you carve precisely the slice you want.
Reading the compiled test is the single most useful debugging move on this surface. dbt writes the compiled SQL to target/compiled/<project>/tests/<test_node>.sql, and the file is the exact query the adapter executed. When a test fails inscrutably, open the compiled file, copy it into a SQL editor connected to the same warehouse, and run it. The rows that come back are the rows that broke the assertion, every time. This is also why the store_failures configuration is so useful in CI: it persists those exact rows for a reviewer to inspect without anyone needing to recompile by hand.
# Run every test on a single model and its downstream descendants
dbt test --select stg_payments+
# Run only the singular tests in the project
dbt test --select test_type:singular
# Run tests that changed compared to a deferred manifest, plus their immediate downstream
dbt test --select state:modified+ --defer --state ./prod-artifacts
# Inspect the compiled SQL of a failed test
cat target/compiled/jaffle_shop/models/staging/stg_payments.yml/unique_stg_payments_payment_id.sql
📊 Limit: A custom generic test macro must accept
modelas the first positional argument; if it also operates on a column,column_nameis the second positional argument. dbt resolves additional arguments by keyword; the parser rejects a macro that omitsmodelwith a clear error naming the missing argument, so the failure mode is loud rather than silent.
Decision Anchor: which test class to author
Choose a generic test when the assertion takes a column_name and any number of optional thresholds, and you expect to apply it to more than two columns or models. Choose a singular test when the assertion is scenario-specific, joins multiple models, or encodes a business rule that does not naturally accept a column_name. Choose a custom generic test when the same singular test has appeared three or more times with only the table or column changing; the third copy is the signal that reuse has earned the macro shape.
