Testing Data Quality
Ensuring data quality is critical for any analytics pipeline. dbt provides built-in testing capabilities that help catch issues early, enforce data integrity, and maintain confidence in your transformations. This guide explains how to implement tests in your dbt project.
Why Testing Matters in dbt
Data tests serve several essential purposes:
Validate assumptions about your data
Catch errors before they impact downstream consumers
Document expectations about data properties
Ensure consistency across transformations
Without testing, issues can creep into your data pipeline, potentially leading to incorrect business decisions or loss of trust in your analytics.
Benefits of dbt Testing
Ensures Data Integrity – Prevents duplicates, null values, and referential mismatches.
Validates Business Logic – Confirms that data meets expected criteria.
Catches Issues Early – Detects errors before they affect downstream analytics.
Automates Quality Checks – Reduces the need for manual data validation.
Supports Collaboration – Helps teams align on data expectations.
Types of dbt Tests
dbt supports two main types of tests:
1. Generic Tests (Built-in)
Generic tests are reusable test definitions that can be applied to multiple models and columns. dbt includes four built-in generic tests:
unique
Ensures a column has no duplicate values
Primary keys, email addresses
not_null
Ensures a column contains no NULL values
Required fields, join keys
accepted_values
Validates that column values are within a specified list
Status fields, categories
relationships
Ensures referential integrity between tables
Foreign keys, dimensional references
2. Singular Tests
Singular tests are custom SQL queries that define specific test logic. These are one-off tests written as SQL queries that return failing records.
Adding Tests to Your Models
Tests in dbt are typically defined in YAML files alongside your models.
Generic Tests Example
This YAML configuration:
Tests that
customer_id
values are unique and not nullTests that
email
values are unique and not nullTests that
status
values are only 'active', 'inactive', or 'pending'Tests that each
country_id
exists in thecountries
table
Singular Test Example
Singular tests are SQL files in the tests/
directory:
This test identifies orders where the payment amounts don't match the order amount.
Running Tests
dbt makes it easy to run tests as part of your workflow.
Running All Tests
dbt test
Runs all tests in your dbt project
Testing Specific Models
dbt test --models customers
Runs all tests associated with a specific model
Running a Single Test
dbt test --select test_name
Runs a specific test by name
Testing Critical Models Only
dbt test --select tag:critical
Runs tests only for models tagged as 'critical'
Test Configuration Options
You can configure how tests behave using additional parameters.
Setting Severity Levels
Tests can be warnings instead of errors:
Store Test Failures
Save test failures for analysis:
Limiting Failure Volume
Control how many failures are reported:
Creating Custom Generic Tests
You can extend dbt's testing capabilities by creating custom generic tests as macros:
Then use it just like built-in tests:
Test Organization Strategies
As your project grows, organizing tests becomes important:
Test by Business Domain
Group tests alongside the models they validate:
Centralized Tests
Maintain all tests in a dedicated location:
Troubleshooting Failed Tests
When tests fail, dbt provides information to help diagnose the issue:
Review test SQL: dbt outputs the SQL for the failing test
Examine failing records: Look at examples of failing records
Check compiled SQL: Review the compiled test SQL in
target/compiled/
Store failures: Use
store_failures: true
to analyze failure patterns
Test Output Examples
Passing Tests
Failing Tests
For failing tests, dbt shows details about the failures:
Best Practices for dbt Testing
Test Coverage Strategy
Test primary keys with
unique
andnot_null
Test foreign keys with
relationships
Test categorical fields with
accepted_values
Test business logic with singular tests
Focus on testing critical data first
Test Organization
Use a consistent naming convention for singular tests
Group related tests together
Document what each test validates
Test Execution
Run tests before finalizing model changes
Include tests in CI/CD pipelines
Alert on test failures in production
Test Maintainability
Prefer generic tests for common validations
Create custom generic tests for repeated patterns
Use macros to generate complex test logic {% endhint %}
By implementing a robust testing strategy in dbt, you can ensure your data transformations maintain high quality and reliability, building trust in your analytics data among stakeholders.
Last updated
Was this helpful?