How to Use dbt test Command for Data Quality Checks
Use the
dbt test command to run tests defined in your dbt project that check data quality and integrity. This command executes tests like uniqueness, not null, and custom SQL tests on your models to catch data issues early.Syntax
The basic syntax of the dbt test command is simple and flexible:
dbt test: Runs all tests in the project.dbt test --models model_name: Runs tests only on specified models.dbt test --select tag:tag_name: Runs tests tagged with a specific tag.
You can also combine options to target specific tests.
bash
dbt test [--models <model_name>] [--select <selector>]
Example
This example shows how to run all tests in a dbt project and how to run tests on a specific model named customers. It assumes you have tests defined in your schema.yml files.
bash
dbt test dbt test --models customers
Output
Running with dbt=1.4.6
Found 3 models, 5 tests, 0 snapshots, 0 analyses, 123 macros, 0 operations, 0 seed files, 0 sources
15:00:00 | Concurrency: 1 threads
15:00:01 | 1 of 5 START test not_null_customers_id....................... [RUN]
15:00:02 | 1 of 5 PASS not_null_customers_id............................ [PASS in 1.0s]
15:00:02 | 2 of 5 START test unique_customers_id....................... [RUN]
15:00:03 | 2 of 5 PASS unique_customers_id............................. [PASS in 1.0s]
...
All tests passed!
Common Pitfalls
Common mistakes when using dbt test include:
- Not defining tests in
schema.ymlfiles, sodbt testhas nothing to run. - Running tests before models are built, causing failures because tables do not exist.
- Using incorrect model names or selectors with
--modelsor--select, resulting in no tests running.
Always run dbt run before dbt test to build models.
bash
## Wrong: Running tests before models exist dbt test ## Right: Build models first, then test dbt run dbt test
Quick Reference
| Command | Description |
|---|---|
| dbt test | Run all tests in the project |
| dbt test --models | Run tests on specific model(s) |
| dbt test --select tag: | Run tests with a specific tag |
| dbt test --exclude | Run tests excluding certain models |
| dbt test --store-failures | Save failing test results to a table |
Key Takeaways
Run
dbt test after building models to check data quality.Define tests in
schema.yml files for dbt test to execute.Use
--models or --select flags to target specific tests.Avoid running tests before models exist to prevent errors.
Use
--store-failures to save failing test details for debugging.