Custom generic tests
Beyond built-in generic tests, you can write custom generic tests to cover complex testing scenarios.
At bottom, generic tests are Jinja macros that return failing rows. These tests are considered project-level asset and can be used across different models in your project.
To define a generic test, simply write a macro and wrap it within {% test %} {% endtest %}
block. A generic test should accept one or both standard parameters:
model
: The model or resource where the test is applied.column_name
: If the test validates assumptions about a specific column's data, include thecolumn_name
parameter.
Here's an example:
This payment_amount_reasonable
test receives four parameters: the model, column name, minimum and maximum amounts. Inside, the validation logic is organized into a CTE (common table expression) that queries all the payment amounts not in the reasonable range.
Create a custom generic test
To create a custom generic test, follow these steps:
In your project, go to Library > Generic Test.
Click + Add new.
Define the generic test with the following fields:
Generic test name: provide the displayed name for your test.
Description: describe the functionality of the test.
SQL Code: provide the test code.
Category: select the test category that the test should reflect.
Click Add.
The created generic test is then accessible from Test case template. You set up the test the same way as a built-in genertic test. Specify parameter values in the Configuration section: