dbt Analytics Engineering (50 Qs)•Part 3: Enterprise Scale, Performance & CI/CD Pipelines (Senior / Staff)
dbt Unit Testing (`unit_tests:`): Testing SQL logic with mocked inputs and expected outputs.
senior LevelMost AskedCode ExampleFormsPerformancePipes
In Simple Words:
Traditional dbt tests (not_null, unique) test the quality of live data. Unit Tests test the correctness of your SQL logic using static mocked data without querying real warehouse tables:
yaml
unit_tests:
- name: test_discount_calculation
model: fct_orders
given:
- input: ref('stg_orders')
rows:
- { order_id: 1, base_price: 100, discount_pct: 0.10 }
- { order_id: 2, base_price: 200, discount_pct: 0.00 }
expect:
rows:
- { order_id: 1, final_price: 90.0 }
- { order_id: 2, final_price: 200.0 }
- Runs in seconds during pull request CI checks to catch SQL math bugs before code merges.