How to Add a Custom Test inside a config block of a model.

The problem I’m having

I have a config block inside of my model, where I want to add a test so that it can be run after the model is built.

The context of why I’m trying to do this

I want to run certain tests during development at each model level.

What I’ve already tried

– tests/compare_row_counts.sql

{% test compare_row_counts(model, prod_schema, prod_table) %}

with model_count as (
    select count(*) as cnt from {{ model }}
),

prod_count as (
    select count(*) as cnt from {{ prod_schema }}.{{ prod_table }}
),

validation as (
    select 
        model_count.cnt as model_count, 
        prod_count.cnt as prod_count
    from model_count, prod_count
)

select *
from validation
where model_count != prod_count

{% endtest %}

models/my_model.sql

...
{{
    config(
        tags: ...,
        materialized: ....,
        tests: [
                {
                   'compare_row_counts': {
                            'prod_schema': 'PROD_SCH',
                            'prod_table': 'PROD_TBL',
                    }
                }
        ],
)
}}

When I build or test my model, it doesn’t run the tests.

Is there a way to specify tests at a model level.

Thank you.

Tests aren’t configured in a model’s config block typically.

Note: @Jeremy Yeo originally posted this reply in Slack. It might not have transferred perfectly.

Typically this would be handled by adding the test to the associated .yml configuration. Additional parameters can be configured there as well. To run them immediately after the model is built use the dbt build -s my_models command.

Thank you! I’ll try it out .

I wish we could test by adding it in config block though.