Testing model outputs helps you check if your data models give correct and expected results. It stops errors before they cause problems.
Testing model outputs in dbt
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
dbt
version: 2 models: - name: your_model_name tests: - unique: column_name: id - not_null: column_name: id - accepted_values: column_name: status values: ['active', 'inactive']
Tests are defined in your model's schema.yml file.
Common tests include unique, not_null, and accepted_values.
Examples
customer_id is unique and email is never empty.dbt
models:
- name: customers
tests:
- unique:
column_name: customer_id
- not_null:
column_name: emailorder_status only has allowed values.dbt
models:
- name: orders
tests:
- accepted_values:
column_name: order_status
values: ['pending', 'shipped', 'delivered']Sample Program
This example shows a sales model with tests to ensure sale_id is unique and not null, sale_amount is not null, and sale_status only contains allowed values.
dbt
version: 2 models: - name: sales description: "Sales data model" columns: - name: sale_id tests: - unique - not_null - name: sale_amount tests: - not_null - name: sale_status tests: - accepted_values: values: ['completed', 'pending', 'cancelled']
Important Notes
Tests help catch data problems early.
Use descriptive test names to understand failures quickly.
Run tests often during development and before deployment.
Summary
Testing model outputs ensures your data is accurate and reliable.
Define tests in your model's schema.yml file using simple syntax.
Common tests check uniqueness, null values, and allowed values.
Practice
1. What is the main purpose of testing model outputs in dbt?
easy
Solution
Step 1: Understand the goal of testing in dbt
Testing checks if the data produced by models is correct and trustworthy.Step 2: Identify the main benefit of testing outputs
Accurate and reliable data helps users make good decisions and trust reports.Final Answer:
To ensure the data is accurate and reliable -> Option AQuick Check:
Testing = Accurate data [OK]
Hint: Testing checks data correctness, not speed or deletion [OK]
Common Mistakes:
- Thinking tests speed up loading
- Confusing testing with table creation
- Assuming tests delete data
2. Which of the following is the correct syntax to define a uniqueness test on a column
user_id in a dbt model's schema.yml file?easy
Solution
Step 1: Check the correct key for model name
The key to specify the model isname, notmodel.Step 2: Verify column and test syntax
Each column usesnameand tests are listed undertestsas a list.Final Answer:
- name: users columns: - name: user_id tests: - unique -> Option DQuick Check:
Correct schema.yml syntax = - name: users columns: - name: user_id tests: - unique [OK]
Hint: Use 'name' for model and column, 'tests' as list [OK]
Common Mistakes:
- Using 'model' instead of 'name' for model
- Writing 'test' instead of 'tests'
- Omitting 'name' for column
3. Given this dbt test defined in
schema.yml for the model orders:
- name: orders
columns:
- name: order_id
tests:
- unique
- not_null
What will happen if the orders table has two rows with the same order_id and one row with order_id as NULL when you run dbt test?medium
Solution
Step 1: Understand the tests applied
The tests areuniqueandnot_nullonorder_id.Step 2: Analyze the data issues
Two rows have the sameorder_id(violates uniqueness) and one row has NULLorder_id(violates not_null).Final Answer:
The test will fail because of duplicate and NULL values in order_id -> Option AQuick Check:
Duplicates + NULLs = test fail [OK]
Hint: Both unique and not_null must pass for success [OK]
Common Mistakes:
- Assuming NULLs are allowed in unique tests
- Thinking only one test failure causes pass
- Ignoring NULL violation in not_null test
4. You wrote this test in your
schema.yml file:
- name: customers
columns:
- name: email
tests:
- unique
- not_null
But when you run dbt test, you get an error saying Invalid test configuration. What is the likely cause?medium
Solution
Step 1: Check the structure of schema.yml
Tests must be defined under themodels:key inschema.yml.Step 2: Identify missing
The snippet misses themodels:keymodels:root key, causing invalid configuration.Final Answer:
The model name should be undermodelskey inschema.yml-> Option CQuick Check:
Missing 'models:' key = config error [OK]
Hint: Always start schema.yml tests under 'models:' key [OK]
Common Mistakes:
- Removing dashes from test names
- Incorrect indentation of tests list
- Not placing model under 'models:'
5. You want to test that the
status column in your transactions model only contains the values 'pending', 'completed', or 'failed'. Which test definition in schema.yml correctly enforces this?hard
Solution
Step 1: Identify the correct test for allowed values
Theaccepted_valuestest checks if column values are in a list.Step 2: Check correct syntax for accepted_values
The test requires a dictionary with keyvalueslisting allowed values.Final Answer:
- name: transactions columns: - name: status tests: - accepted_values: values: ['pending', 'completed', 'failed'] -> Option BQuick Check:
accepted_values with 'values' key = - name: transactions columns: - name: status tests: - accepted_values: values: ['pending', 'completed', 'failed'] [OK]
Hint: Use accepted_values with 'values' list for allowed values [OK]
Common Mistakes:
- Using unique or not_null instead of accepted_values
- Omitting 'values:' key under accepted_values
- Using wrong test name like values_in
