Generic, Singular, and Custom Generic Data Tests
dbt Analytics Engineering · Lesson 14 of 17 · 1:48:51
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the dbt Analytics Engineering Certification exam. In this lesson: the four generic data tests, singular tests, custom generic tests, and test configs (severity, error_if, warn_if, where, store_failures, and limit). Includes three original practice questions, with Orchardline Meal Kits, a fictional meal-kit company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Two problems nobody caught
Orchardline's box status column suddenly holds a value that nobody expected, and a pricing bug produced negative totals for three days before anyone noticed. Data tests would have caught both problems on the first night. This lesson covers generic tests, singular tests, custom generic tests, and the configs that make tests match your assumptions about the data.
Generic tests
A data test is a select query that returns the failing rows: if it returns zero rows, the test passes. dbt ships with four generic tests, which you add in YAML, under the data tests key of a column: unique, not null, accepted values, and relationships. Accepted values takes a list of values, and relationships checks that each value exists in another model, given with to and field: in dbt one point eleven, these go under an arguments key. Generic tests work on models, and also on sources, seeds, and snapshots, so you can test your assumptions about raw data, right where it enters the project. When a source test fails, a wrong assumption about the raw data shows up before the models that depend on it.
Singular tests
A singular test is a single SQL file in the tests folder, with one select statement that returns the failing rows. It is a one off assertion, like a delivery that arrives before its order, and it can use ref and source, just like a model. The file name becomes the test name, and dbt test runs it automatically: you do not reference it in YAML, though you can add a description in a YAML file in the tests folder. Leave out the semicolon at the end of the query, since it can make the test fail. When you find yourself writing the same singular test again, with only a column or a model name changing, turn it into a custom generic test.
Custom generic tests
A custom generic test is a test block, written in a SQL file in the generic folder under tests, or in the macros folder. It accepts the standard arguments, model, and column name when it works on a column, plus any arguments of your own. Its query returns the failing rows, like a singular test, but with the model and the column templated in, so you can apply it in YAML to any column of any model, source, seed, or snapshot. Packages, like dbt utils, already provide many generic tests, so check them before writing your own.
Test configs
Tests have their own configs: severity is error by default, or warn, and error if and warn if set thresholds on the number of failures, like a warning above ten, and an error above a thousand. The where config limits the rows that a test checks, with a SQL condition. Store failures saves the failing rows in a table, in an audit schema, so you can query them after the run, and limit caps how many failing rows are returned. You set these configs on one test in YAML, in the config block of a singular or custom generic test, or for many tests in dbt project dot yml, and the most specific setting wins. And a warning never fails the run, unless you promote warnings to errors, with the warn error flag.
Recap
Let's recap. A data test is a query that returns failing rows, and passes when it returns none. The four generic tests are unique, not null, accepted values, and relationships, on models, sources, seeds, and snapshots. A singular test is one SQL file in the tests folder, for a one off assertion. A custom generic test is a reusable test block, with model and column name as arguments. Severity, error if, warn if, where, and store failures tune each test, and the most specific setting wins.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Orchardline wants every order ID in the deliveries model to point to an existing order in the orders model. Which generic test fits?
- unique
- not_null
- relationships
- accepted_values
Show the answer
relationships
Relationships checks that each order ID in deliveries exists in the orders model, with to set to the orders model, and field set to its key. “unique” is wrong, since unique only looks for duplicates. “not_null” is wrong, since not null only looks for missing values. And “accepted_values” is wrong, since accepted values compares the column with a fixed list.
The team has five singular tests, and each one checks that a different amount column is never negative. What is the better design?
- Keep the five files, since singular tests cannot be shared
- Write one custom generic test, and apply it in YAML
- Merge the five checks into one singular test with a union
- Add a not_null test to each of the amount columns
Show the answer
Write one custom generic test, and apply it in YAML
The same query with a different column is exactly what a custom generic test is for, defined once, and applied to each column in YAML. “Keep the five files, since singular tests cannot be shared” would only keep repeating the same logic in five files. “Merge the five checks into one singular test with a union” is wrong, since one combined query hides which column fails. And “Add a not_null test to each of the amount columns” is wrong, since not null checks for missing values, not negative ones.
A duplicate check on the subscriptions model should warn on a few duplicates, but fail above one hundred. Which config fits?
- severity: warn, error_if: ">100"
- store_failures: true, limit: 100
- severity: error
- error_if: ">100"
Show the answer
error_if: ">100"
With the default error severity, dbt checks error if first, and a smaller number of failures falls back to warn if, which warns on any failure. “severity: warn, error_if: ">100"” is wrong, since a warn severity skips error if entirely, so the test can never fail. “store_failures: true, limit: 100” is wrong, since store failures and limit only save failing rows. And “severity: error” is wrong, since a single duplicate would then fail the run.