Running tests with dbt test helps you check your data for mistakes or problems automatically. It makes sure your data is clean and reliable.
Running tests with dbt test
Start learning this pattern below
Jump into concepts and practice - no test required
dbt test [--select <models>] [--exclude <models>] [--store-failures] [--data] [--schema]
dbt test runs tests defined in your project on your data models.
You can select specific models to test or exclude some using flags.
dbt test
customers model.dbt test --select customers
orders.dbt test --exclude orders
dbt test --store-failures
This example shows how to define simple tests to check that the email column in the customers model is never empty and has unique values. Then, running dbt test --select customers checks these rules.
1. Define a test in your model's schema.yml file: version: 2 models: - name: customers columns: - name: email tests: - not_null - unique 2. Run the test in terminal: $ dbt test --select customers
Tests can be schema tests (check structure like uniqueness) or data tests (custom SQL queries).
Use --store-failures to save failing rows for easier debugging.
Running tests regularly helps keep your data trustworthy.
dbt test runs checks on your data models to find errors.
You can run tests on all or specific models using flags.
Defining tests in schema.yml files helps automate data quality checks.
Practice
dbt test in a dbt project?Solution
Step 1: Understand the role of
dbt testdbt testruns tests defined in your project to check data quality and catch errors.Step 2: Differentiate from other dbt commands
Building models is done withdbt run, deployment is outsidedbt test, and documentation is generated withdbt docs.Final Answer:
To check data quality and find errors in your data models -> Option CQuick Check:
dbt test = data quality checks [OK]
- Confusing
dbt testwithdbt run - Thinking
dbt testdeploys models - Assuming
dbt testgenerates docs
customers?Solution
Step 1: Identify the correct flag for running tests on specific models
The flag--selectis used withdbt testto specify which models to test.Step 2: Check other options for correctness
dbt runbuilds models, not tests.--modelsis not a valid flag fordbt test.--onlyis not a valid flag.Final Answer:
dbt test --select customers -> Option AQuick Check:
Use --select to target tests [OK]
- Using
dbt runinstead ofdbt test - Using invalid flags like
--modelsor--only - Confusing
--selectwith other flags
schema.yml test definition:models:
- name: orders
tests:
- unique:
column_name: order_id
- not_null:
column_name: order_dateWhat will
dbt test check for the orders model?Solution
Step 1: Read the test types for each column
The testuniqueapplies toorder_id, ensuring no duplicates. The testnot_nullapplies toorder_date, ensuring no missing values.Step 2: Match tests to their meaning
uniquemeans no duplicates;not_nullmeans no nulls. So the checks are:order_idis unique andorder_datehas no null values.Final Answer:
It checks thatorder_idis unique andorder_datehas no null values -> Option AQuick Check:
unique = no duplicates, not_null = no nulls [OK]
- Mixing up unique and not_null tests
- Assuming both columns have the same test
- Ignoring the column_name key in test definitions
dbt test but get an error: Compilation Error: Could not find test 'uniquee'. What is the likely cause?Solution
Step 1: Analyze the error message
The error says it cannot find test 'uniquee', which looks like a misspelled test name.Step 2: Identify common causes of compilation errors
Typos in test names inschema.ymlcause dbt to fail to find the test. Model name or connection errors produce different messages.Final Answer:
A typo in the test name in schema.yml -> Option DQuick Check:
Compilation errors often mean typos [OK]
- Ignoring typo errors and rerunning blindly
- Assuming connection issues cause compilation errors
- Confusing model name errors with test name errors
email column in your users model is unique and not null. You also want to run tests only on this model. Which schema.yml snippet and command combination is correct?Solution
Step 1: Identify correct test syntax in
Tests on columns are defined underschema.ymlcolumnswithnameandtestslist.models: - name: users columns: - name: email tests: - unique - not_null
Command:dbt test --select usersshows the correct format.Step 2: Choose the correct command to run tests on the
usersmodeldbt test --select usersruns tests only on the users model.models: - name: users columns: - name: email tests: - unique - not_null
Command:dbt test --select usersuses this command correctly.Final Answer:
Option B with column-level tests and dbt test --select users -> Option BQuick Check:
Column tests + --select flag = correct [OK]
- Defining tests directly under model without column_name (invalid syntax)
- Using
dbt runinstead ofdbt test - Using incorrect flags like
--models
