Testing dbt models helps you check if your data transformations work correctly. It finds mistakes early so your data is reliable.
Unit testing dbt models
version: 2 models: - name: your_model_name tests: - unique: column_name: id - not_null: column_name: id - relationships: to: ref('other_model') field: id
This syntax is written in YAML inside your dbt project under a schema.yml file.
Tests like unique and not_null check basic data quality.
customers model has unique and non-null values in its primary key.models:
- name: customers
tests:
- unique:
column_name: customer_id
- not_null:
column_name: customer_id
customer_id in orders exists in the customers model.models:
- name: orders
tests:
- relationships:
to: ref('customers')
field: customer_id
category column in products only contains allowed values.models:
- name: products
tests:
- accepted_values:
column_name: category
values: ['electronics', 'furniture', 'clothing']
This schema.yml file defines tests for the sales model. It checks that sale_id is unique and not null, and that customer_id matches an existing customer.
version: 2 models: - name: sales columns: - name: sale_id tests: - unique - not_null - name: customer_id tests: - relationships: to: ref('customers') field: customer_id
dbt tests run automatically with dbt test command.
You can write custom tests if built-in ones don't cover your needs.
Keep tests small and focused on one thing for easier debugging.
Testing in dbt checks your data models for correctness and quality.
Tests are defined in YAML files using simple syntax like unique and not_null.
Running dbt test helps catch errors early and keeps data trustworthy.
Practice
Solution
Step 1: Understand unit testing in dbt
Unit testing in dbt is designed to verify that data models behave as expected and contain correct data.Step 2: Identify the main goal
The main goal is to check correctness and quality of data models, not to create or deploy them.Final Answer:
To check data models for correctness and quality -> Option AQuick Check:
Unit testing = correctness and quality check [OK]
- Confusing testing with model creation
- Thinking tests deploy models
- Assuming tests speed up SQL writing
Solution
Step 1: Recall YAML test syntax in dbt
Tests are listed as a YAML list under the 'tests' key, each with a dash and test name.Step 2: Match correct indentation and list format
tests: - unique: column_name shows 'tests:' followed by a list item '- unique: column_name', which is correct YAML syntax for dbt tests.Final Answer:
tests: - unique: column_name -> Option AQuick Check:
YAML lists use dash (-) before items [OK]
- Missing dash (-) before test name
- Incorrect indentation
- Placing test name and column incorrectly
columns:
- name: user_id
tests:
- unique
- not_nullWhat will
dbt test check for the user_id column?Solution
Step 1: Read the tests listed for user_id
The YAML shows two tests: 'unique' and 'not_null' for the user_id column.Step 2: Understand what each test does
'unique' ensures no duplicate values; 'not_null' ensures no missing values.Final Answer:
It checks that user_id values are unique and not null -> Option BQuick Check:
unique + not_null = no duplicates, no nulls [OK]
- Thinking tests check opposite conditions
- Assuming only one test runs
- Confusing unique with not_null
columns:
- name: order_id
tests:
unique
not_nullBut when you run
dbt test, it fails with a syntax error. What is the likely cause?Solution
Step 1: Check YAML list syntax for tests
Each test under 'tests' must be a list item starting with a dash (-).Step 2: Identify missing dashes
The snippet shows 'unique' and 'not_null' without dashes, causing YAML syntax error.Final Answer:
Missing dash (-) before 'unique' and 'not_null' tests -> Option DQuick Check:
YAML lists need dash (-) before each item [OK]
- Omitting dash (-) before tests
- Wrong indentation levels
- Confusing 'tests' with 'test' key
email column in your users model is always unique and never empty. Which YAML test configuration correctly applies these tests and how would you run them?Solution
Step 1: Write correct YAML test syntax
The tests must be listed as a YAML list with dashes under 'tests' for the 'email' column, as shown in Define in YAML:columns: - name: email tests: - unique - not_null
Then rundbt test.Step 2: Choose the correct dbt command to run tests
To execute tests, you must rundbt test, notdbt run. So this configuration withdbt testis correct.Final Answer:
Define tests with dashes and run dbt test -> Option CQuick Check:
YAML list + dbt test command = correct testing [OK]
- Running 'dbt run' instead of 'dbt test'
- Incorrect YAML list syntax
- Misnaming column key
Ready to prove it?
Pass the test and this pattern moves unknown
