Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Running tests with dbt test
📖 Scenario: You are working on a data project using dbt (data build tool). You want to make sure your data models are correct by running tests that check for missing values and uniqueness.
🎯 Goal: Learn how to set up simple tests in dbt and run them using the dbt test command to validate your data models.
📋 What You'll Learn
Create a dbt model file with sample data
Add schema tests for not_null and unique constraints
Run dbt test to execute the tests
View the test results output
💡 Why This Matters
🌍 Real World
Data analysts and engineers use dbt tests to ensure their data models are accurate and reliable before using them for reports or dashboards.
💼 Career
Knowing how to run and interpret dbt tests is essential for roles in data engineering, analytics engineering, and data quality assurance.
Progress0 / 4 steps
1
Create a dbt model with sample data
Create a dbt model file called models/sample_model.sql with the following SQL code exactly: select 1 as id, 'Alice' as name union all select 2, 'Bob' union all select 3, 'Charlie'
dbt
Hint
Write a SQL query that returns three rows with columns id and name.
2
Add schema tests for not_null and unique
In the models/schema.yml file, add tests for the sample_model model to check that the id column is unique and not null, and the name column is not null. Use the exact keys models, name, columns, tests, and test names unique and not_null.
dbt
Hint
Use YAML format to define tests under models for your model and columns.
3
Run dbt test to execute the tests
Run the command dbt test in your terminal to execute the tests defined in models/schema.yml for the sample_model.
dbt
Hint
Use your terminal or command line to run dbt test.
4
View the test results output
After running dbt test, print the test results summary that shows how many tests passed and failed. For example, print All tests passed! if no tests failed.
dbt
Hint
Simply print the message All tests passed! to show success.
Practice
(1/5)
1. What is the main purpose of running dbt test in a dbt project?
easy
A. To deploy the dbt project to production
B. To build new data models from raw data
C. To check data quality and find errors in your data models
D. To generate documentation for your data models
Solution
Step 1: Understand the role of dbt test
dbt test runs tests defined in your project to check data quality and catch errors.
Step 2: Differentiate from other dbt commands
Building models is done with dbt run, deployment is outside dbt test, and documentation is generated with dbt docs.
Final Answer:
To check data quality and find errors in your data models -> Option C
Quick Check:
dbt test = data quality checks [OK]
Hint: Remember: test = check data quality, run = build models [OK]
Common Mistakes:
Confusing dbt test with dbt run
Thinking dbt test deploys models
Assuming dbt test generates docs
2. Which of the following is the correct command to run tests only on a specific model named customers?
easy
A. dbt test --select customers
B. dbt run --models customers
C. dbt test --models customers
D. dbt test --only customers
Solution
Step 1: Identify the correct flag for running tests on specific models
The flag --select is used with dbt test to specify which models to test.
Step 2: Check other options for correctness
dbt run builds models, not tests. --models is not a valid flag for dbt test. --only is not a valid flag.
Final Answer:
dbt test --select customers -> Option A
Quick Check:
Use --select to target tests [OK]
Hint: Use --select flag to run tests on specific models [OK]
A. It checks that order_id is unique and order_date has no null values
B. It checks that order_id has no null values and order_date is unique
C. It checks that both order_id and order_date are unique
D. It checks that both order_id and order_date have no null values
Solution
Step 1: Read the test types for each column
The test unique applies to order_id, ensuring no duplicates. The test not_null applies to order_date, ensuring no missing values.
Step 2: Match tests to their meaning
unique means no duplicates; not_null means no nulls. So the checks are: order_id is unique and order_date has no null values.
Final Answer:
It checks that order_id is unique and order_date has no null values -> Option A
Quick Check:
unique = no duplicates, not_null = no nulls [OK]
Hint: unique = no duplicates, not_null = no nulls [OK]
Common Mistakes:
Mixing up unique and not_null tests
Assuming both columns have the same test
Ignoring the column_name key in test definitions
4. You run dbt test but get an error: Compilation Error: Could not find test 'uniquee'. What is the likely cause?
medium
A. The test passed with no errors
B. The model name is incorrect
C. The database connection is missing
D. A typo in the test name in schema.yml
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 in schema.yml cause 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 D
Quick Check:
Compilation errors often mean typos [OK]
Hint: Check spelling of test names in schema.yml [OK]
Common Mistakes:
Ignoring typo errors and rerunning blindly
Assuming connection issues cause compilation errors
Confusing model name errors with test name errors
5. You want to ensure that the 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?