Generic tests with parameters help you check your data for common problems using reusable rules. They save time and keep your data clean.
Generic tests with parameters in 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.
version: 2
models:
- name: customers
columns:
- name: email
tests:
- unique: {}version: 2 models: - name: orders columns: - name: status tests: - accepted_values: values: ['shipped', 'pending', 'cancelled']
version: 2
models:
- name: sales
columns:
- name: amount
tests:
- not_null: {}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.
version: 2 models: - name: users columns: - name: user_id tests: - unique: {} - not_null: {} - name: user_type tests: - accepted_values: values: ['admin', 'member', 'guest']
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.
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.
Practice
generic tests with parameters in dbt?Solution
Step 1: Understand generic tests
Generic tests are reusable test templates that can be applied to many columns or tables.Step 2: Role of parameters
Parameters customize these tests for specific columns or rules without rewriting the test logic.Final Answer:
They allow reusing test logic with different settings. -> Option DQuick Check:
Generic tests + parameters = reusable logic [OK]
- Thinking generic tests fix data automatically
- Confusing generic tests with table creation
- Believing generic tests replace SQL queries
Solution
Step 1: Review dbt test YAML syntax
Generic tests with parameters are called using a dictionary format: test_name: {param: value}.Step 2: Match syntax to options
tests: - unique: { column_name: 'id' } uses the correct dictionary format with the parameter inside curly braces.Final Answer:
tests: - unique: { column_name: 'id' } -> Option BQuick Check:
YAML test call = dictionary with parameters [OK]
- Using function call syntax in YAML
- Passing parameters as plain strings or lists
- Omitting curly braces for parameters
tests:
- not_null:
column_name: 'email'
- accepted_values:
column_name: 'status'
values: ['active', 'inactive']
What does the
accepted_values test check?Solution
Step 1: Identify the test and parameters
Theaccepted_valuestest is applied to the 'status' column with allowed values ['active', 'inactive'].Step 2: Understand test purpose
This test ensures the column only contains the specified allowed values.Final Answer:
It checks that 'status' column has only 'active' or 'inactive' values. -> Option AQuick Check:
accepted_values test = allowed values check [OK]
- Confusing accepted_values with not_null test
- Mixing columns between tests
- Assuming accepted_values checks for nulls
tests:
- unique:
column: 'user_id'
But the test fails to run. What is the likely error?
Solution
Step 1: Check parameter naming
Generic tests expect parameter 'column_name' to specify the column, not 'column'.Step 2: Validate test structure
Using 'column' instead of 'column_name' causes the test to fail because the parameter is unrecognized.Final Answer:
Parameter name should be 'column_name', not 'column'. -> Option AQuick Check:
Correct parameter name = column_name [OK]
- Using wrong parameter keys
- Misplacing tests outside 'columns:' block
- Confusing quote types for strings
min_value and max_value. Which YAML snippet correctly calls this test for column age with range 18 to 65?Solution
Step 1: Use correct parameter names
The test expects parameters named 'column_name', 'min_value', and 'max_value'.Step 2: Use correct YAML syntax and types
tests: - within_range: column_name: 'age' min_value: 18 max_value: 65 uses correct parameter names and numeric values without quotes for numbers.Step 3: Check other options
Function call syntax is invalid in YAML; incorrect parameter names like 'column', 'min', 'max'; unquoted column name and quoted numbers may cause type issues.Final Answer:
tests: - within_range: column_name: 'age' min_value: 18 max_value: 65 -> Option CQuick Check:
Correct params + types = tests: - within_range: column_name: 'age' min_value: 18 max_value: 65 [OK]
- Using wrong parameter keys like 'column' or 'min'
- Passing numbers as strings
- Using function call syntax in YAML
Ready to prove it?
Pass the test and this pattern moves unknown
