How to Run Tests in dbt: Simple Guide
To run tests in
dbt, use the command dbt test in your project directory. This command executes all tests defined in your models and schema files, helping you catch data issues early.Syntax
The basic command to run tests in dbt is dbt test. You can run all tests or specify particular models or tags.
dbt test: Runs all tests in the project.dbt test --models model_name: Runs tests only for the specified model.dbt test --select tag:tag_name: Runs tests for models with a specific tag.
bash
dbt test dbt test --models my_model dbt test --select tag:important
Example
This example shows how to run all tests in a dbt project and the expected output. It demonstrates running dbt test in the terminal.
bash
dbt test
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 (target='dev')
15:00:00 |
15:00:01 | 1 of 5 START test not_null_my_model_id....................... [RUN]
15:00:02 | 1 of 5 PASS not_null_my_model_id............................ [PASS in 1.0s]
15:00:02 | 2 of 5 START test unique_my_model_email.................... [RUN]
15:00:03 | 2 of 5 PASS unique_my_model_email......................... [PASS in 1.0s]
15:00:03 | 3 of 5 START test relationships_my_model_to_users.......... [RUN]
15:00:04 | 3 of 5 PASS relationships_my_model_to_users............... [PASS in 1.0s]
15:00:04 | 4 of 5 START test accepted_values_my_model_status........... [RUN]
15:00:05 | 4 of 5 PASS accepted_values_my_model_status.............. [PASS in 1.0s]
15:00:05 | 5 of 5 START test unique_my_model_order_id................ [RUN]
15:00:06 | 5 of 5 PASS unique_my_model_order_id..................... [PASS in 1.0s]
15:00:06 |
15:00:06 | Finished running 5 tests in 6.0s.
15:00:06 |
15:00:06 | Completed successfully
Common Pitfalls
Common mistakes when running tests in dbt include:
- Not defining tests in
schema.ymlfiles, sodbt testhas nothing to run. - Running tests outside the dbt project directory, causing errors.
- Forgetting to run
dbt runbefore tests, so models are not built yet. - Using incorrect model names or tags with the
--modelsor--selectflags.
bash
Wrong: dbt test --models wrong_model_name Right: dbt test --models correct_model_name
Quick Reference
| Command | Description |
|---|---|
| dbt test | Run all tests in the project |
| dbt test --models model_name | Run tests for a specific model |
| dbt test --select tag:tag_name | Run tests for models with a specific tag |
| dbt run | Build models before testing |
| dbt test --exclude model_name | Run tests excluding a model |
Key Takeaways
Run all tests in dbt with the simple command
dbt test.Define tests in
schema.yml files to enable testing.Use
--models or --select flags to target specific tests.Always run
dbt run before testing to build models.Run tests inside your dbt project directory to avoid errors.