Understanding dbt Unit Tests
Unit tests in dbt help you verify that your transformations produce expected outputs given specific input data. This ensures your business logic is correct before you deploy it to production.Unit testing is available in dbt Core v1.8+ and above
Why Unit Testing Matters
Traditional data tests (likenot_null, unique) validate the quality of data that already exists. Unit tests serve a different and complementary purpose:
Unit tests provide several benefits:
- Test before building: Validate logic without materializing models
- Verify transformations: Ensure your SQL logic handles edge cases correctly
- Support test-driven development: Write tests first, then implement the model
- Catch errors early: Find bugs before they reach production
- Improve code reliability: Maintain confidence during refactoring
When to Use Unit Tests
Unit tests are particularly valuable when your models include:- Complex transformations: Regular expressions, date calculations, window functions
- Critical business logic: Calculations that impact important metrics
- Known edge cases: Scenarios that have caused issues in the past
- Models undergoing refactoring: Changes to existing transformation logic
- Frequently used models: Core models that many others depend on
Unit Test Structure
A dbt unit test consists of three essential parts:- Mock inputs: Sample data for source tables and referenced models
- Model under test: The model whose logic you want to validate
- Expected outputs: The exact results you expect after transformation
Basic Example
Here’s a simple unit test defined in YAML:Creating Unit Tests
Unit tests are defined in YAML files within your models directory, typically alongside the model they’re testing.Defining Input Data
Mock input data can be provided in several formats: Dictionary Format (default)Defining Expected Output
You can specify expected outputs in different ways: Rows (most common)Example: Testing a Customer Classification Model
Let’s see a complete example for a model that classifies customers based on spending: The model being tested:Running Unit Tests
To run unit tests, use the dbt test command with appropriate selectors:Testing Special Cases
Incremental Models
When testing incremental models, you can override theis_incremental() macro to test both full refresh and incremental scenarios:
Ephemeral Models
To test models that depend on ephemeral models, use SQL format for the input:Testing Macros
You can override macro implementations for testing:Limitations & Best Practices
dbt’s unit testing framework has some limitations to be aware of:Best Practices for Effective Unit Testing
Test-Driven Development with dbt
Unit tests enable a test-driven development (TDD) workflow for your data transformations:- Write a test: Define the expected behavior
- Run the test: It should fail because the model doesn’t exist or doesn’t handle the case yet
- Implement the model: Create or modify the model to pass the test
- Verify: Run the test again to confirm it passes
- Refactor: Clean up your implementation while keeping the tests passing