Unit Testing
Unit testing in dbt allows you to validate your SQL transformation logic using controlled input data. Unlike traditional data tests that verify the quality of existing data, unit tests help you catch logical errors during development, bringing the test-driven development approach to data transformations.
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 (like not_null
, unique
) validate the quality of data that already exists. Unit tests serve a different and complementary purpose:
Validate transformation logic
Validate data quality
Run before building models
Run after models are built
Use controlled test data
Use actual production data
Focus on business logic correctness
Focus on data integrity
Help with test-driven development
Help with data quality assurance
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)
CSV Format
SQL Format
Defining Expected Output
You can specify expected outputs in different ways:
Rows (most common)
Column Values
Row Count
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:
The unit test:
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 the is_incremental()
macro to test both full refresh and incremental scenarios:
For the incremental update test, you need to provide mock data for the existing table:
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:
{% hint style="warning" %}
Current Limitations
Only supports SQL models (not Python models)
Can only test models in your current project
Doesn't support materialized views
Doesn't support recursive SQL or introspective queries
Requires all table names to be aliased in join logic {% endhint %}
Best Practices for Effective Unit Testing
Focus on logic, not functions
Test your business logic rather than built-in database functions
Use descriptive test names
Clearly explain what each test is verifying
Test edge cases
Include unusual scenarios your logic needs to handle
Only mock what's needed
Define only the columns relevant to your test
Run in development
Use unit tests during development, not in production
Use in CI/CD
Integrate unit tests into your CI/CD pipeline
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
This approach helps ensure your models correctly implement business requirements from the start.
By adopting unit testing in your dbt workflow, you can catch issues earlier, document model behavior, and build more confidence in your data transformations.
Last updated
Was this helpful?