0
0
dbtdata~10 mins

Testing model outputs in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Testing model outputs
Run dbt model
Generate output table
Run tests on output
Test passes
Continue
Test fails
Fix model or data
Deploy or iterate
This flow shows how dbt runs a model, generates output, tests it, and either continues or fixes issues.
Execution Sample
dbt
select id, count(*) as cnt
from source_table
group by id
This SQL model groups data by id and counts rows per id.
Execution Table
StepActionEvaluationResult
1Run model SQLGroup by id and count rowsOutput table with id and cnt columns
2Run test: unique idCheck if all ids are uniquePass if no duplicates
3Run test: cnt > 0Check if count is positivePass if all counts > 0
4Test resultsAll tests passModel output is valid
5EndNo errors foundReady for deployment
💡 All tests pass, so execution stops successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
output_tableemptytable with id and cntsamesamesame
test_unique_idnot runnot runpasspasspass
test_cnt_positivenot runnot runnot runpasspass
Key Moments - 2 Insights
Why do we run tests after the model runs?
Tests check if the output data meets expectations, catching errors early as shown in steps 2 and 3 of the execution table.
What happens if a test fails?
If a test fails, the flow stops and you fix the model or data before continuing, preventing bad data from moving forward.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of step 3?
ATest passes because all counts are positive
BTest fails due to negative counts
CModel output is empty
DTest not run yet
💡 Hint
Check the 'Result' column in row with Step 3 in execution_table
At which step do we verify that all ids are unique?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for the unique id test
If the test for unique ids failed, what would happen next?
AContinue to deploy model
BFix model or data before continuing
CIgnore and run other tests
DDelete the model
💡 Hint
Refer to the concept_flow where test failure leads to fixing issues
Concept Snapshot
Testing model outputs in dbt:
- Run your SQL model to create output
- Use dbt tests to check data quality
- Tests include uniqueness, non-null, and value checks
- If tests pass, deploy or continue
- If tests fail, fix model or data before proceeding
Full Transcript
In dbt, after running a model that creates a table, we run tests to check the output data. These tests verify things like whether ids are unique and counts are positive. If all tests pass, the model output is valid and ready for deployment. If any test fails, we stop and fix the model or data. This process helps catch errors early and ensures data quality.