0
0
dbtdata~30 mins

Unit testing dbt models - Mini Project: Build & Apply

Choose your learning style9 modes available
Unit Testing dbt Models
📖 Scenario: You work as a data analyst who builds data transformation models using dbt. To ensure your models are correct and reliable, you want to write unit tests that check your data logic before deploying to production.
🎯 Goal: Build a simple dbt model and write a unit test to verify that the model produces the expected results.
📋 What You'll Learn
Create a dbt model SQL file with a simple SELECT statement
Create a YAML test configuration file for the model
Write a dbt test that checks for expected data values
Run the test and display the test result
💡 Why This Matters
🌍 Real World
Data analysts and engineers use dbt to build reliable data pipelines. Unit testing models helps catch errors early and ensures data quality.
💼 Career
Knowing how to write and run dbt tests is a key skill for data engineers and analytics engineers working with modern data stacks.
Progress0 / 4 steps
1
Create a simple dbt model
Create a dbt model SQL file named models/simple_model.sql with the exact query: select 1 as id, 'test' as name
dbt
Need a hint?

This model returns a single row with id 1 and name 'test'.

2
Add a YAML test configuration
Create a YAML file named models/simple_model.yml with a test that checks the id column is unique in the simple_model model. Use the exact syntax: tests: - unique
dbt
Need a hint?

The YAML file defines tests for the model columns. Here we test that 'id' is unique.

3
Run the dbt test command
Run the command dbt test --select simple_model in your terminal to execute the unit test on the simple_model model.
dbt
Need a hint?

This command runs the tests defined for the simple_model only.

4
Display the test result
Print the expected output line PASS 1 test, 0 failures to show the test passed successfully.
dbt
Need a hint?

This output means your unit test passed with no errors.