Generic tests with parameters help you check your data for common problems using reusable rules. They save time and keep your data clean.
0
0
Generic tests with parameters in dbt
Introduction
When you want to check if a column has any missing values in many tables.
When you need to ensure values in a column are unique across rows.
When you want to verify that values in a column belong to a specific list.
When you want to apply the same test with different settings to multiple columns.
Syntax
dbt
version: 2
models:
- name: your_model
columns:
- name: your_column
tests:
- test_name:
param1: value1
param2: value2Generic tests are defined once and can accept parameters to customize their behavior.
Parameters allow you to reuse the same test logic for different columns or conditions.
Examples
This runs the built-in unique test on the email column to check for duplicates.
dbt
version: 2
models:
- name: customers
columns:
- name: email
tests:
- unique: {}This test checks if the status column only contains the allowed values.
dbt
version: 2 models: - name: orders columns: - name: status tests: - accepted_values: values: ['shipped', 'pending', 'cancelled']
This test ensures the amount column has no missing values.
dbt
version: 2
models:
- name: sales
columns:
- name: amount
tests:
- not_null: {}Sample Program
This example applies three generic tests with parameters: user_id must be unique and not null, and user_type must be one of the allowed values.
dbt
version: 2 models: - name: users columns: - name: user_id tests: - unique: {} - not_null: {} - name: user_type tests: - accepted_values: values: ['admin', 'member', 'guest']
OutputSuccess
Important Notes
Always check the documentation for the exact parameters each generic test accepts.
You can create your own custom generic tests with parameters for special rules.
Summary
Generic tests with parameters let you reuse test logic easily.
They help keep your data clean by checking common data quality issues.
Parameters customize tests for different columns or rules.