All skills
dbt-labs avatar

/adding-dbt-unit-test

@9d91941 official
by dbt Labsdbt-labs/dbt-agent-skills729 stars
62

Creates unit test YAML definitions that mock upstream model inputs and validate expected outputs. Use when adding unit tests for a dbt model or practicing test-driven development (TDD) in dbt.

Use this Skill: https://skilld.dev/gh/dbt-labs/dbt-agent-skills/adding-dbt-unit-test

This session only. Nothing lands on disk.

referencesspecial-cases-incremental-model.md

≈663 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Unit testing incremental models

When configuring your unit test, you can override the output of macros, vars, or environment variables. This enables you to unit test your incremental models in "full refresh" and "incremental" modes.

Note

Incremental models need to exist in the database first before running unit tests. Use the --empty flag to build an empty version of the models to save warehouse spend. You can also optionally select only your incremental models using the --select flag.

dbt run --select "config.materialized:incremental" --empty

After running the command, you can then perform a regular dbt build for that model and then run your unit test.

When testing an incremental model, the expected output is the result of the materialization (what will be merged/inserted), not the resulting model itself (what the final table will look like after the merge/insert).

For example, say you have an incremental model in your project:

my_incremental_model.sql


{{
    config(
        materialized='incremental'
    )
}}

select * from {{ ref('events') }}
{% if is_incremental() %}
where event_time > (select max(event_time) from {{ this }})
{% endif %}

You can define unit tests on my_incremental_model to ensure your incremental logic is working as expected:


unit_tests:
  - name: my_incremental_model_full_refresh_mode
    model: my_incremental_model
    overrides:
      macros:
        # unit test this model in "full refresh" mode
        is_incremental: false 
    given:
      - input: ref('events')
        rows:
          - {event_id: 1, event_time: 2020-01-01}
    expect:
      rows:
        - {event_id: 1, event_time: 2020-01-01}

  - name: my_incremental_model_incremental_mode
    model: my_incremental_model
    overrides:
      macros:
        # unit test this model in "incremental" mode
        is_incremental: true 
    given:
      - input: ref('events')
        rows:
          - {event_id: 1, event_time: 2020-01-01}
          - {event_id: 2, event_time: 2020-01-02}
          - {event_id: 3, event_time: 2020-01-03}
      - input: this 
        # contents of current my_incremental_model
        rows:
          - {event_id: 1, event_time: 2020-01-01}
    expect:
      # what will be inserted/merged into my_incremental_model
      rows:
        - {event_id: 2, event_time: 2020-01-02}
        - {event_id: 3, event_time: 2020-01-03}

There is currently no way to unit test whether the dbt framework inserted/merged the records into your existing model correctly, but we're investigating support for this in the future in GitHub issue #8664.

Source: SKILL.md on GitHub

1 warning17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides instructional guidance and reference material for creating dbt unit tests. It does not contain any executable scripts, dependencies, or security risks.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer7mo

    9/14 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 9d91941. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 5 months ago
user-invocable
false
metadata
{
  "author": "dbt-labs"
}
  • dbt
  • unit-testing
  • sql
  • yaml
  • tdd
  • data-warehouse

README badge

README badge for dbt-labs/dbt-agent-skills/adding-dbt-unit-test

Creates YAML unit test definitions for dbt SQL models with mocked upstream inputs and expected outputs. Use this when adding unit tests to validate model transformation logic or practicing test-driven development in dbt projects.

Generated from the current SKILL.md.

What SQL models can I create unit tests for?
You can create unit tests for SQL models only. Python models, snapshots, seeds, sources, analyses, and models using materialized view or recursive SQL materializations are not supported.
Do upstream models need to exist before running unit tests?
Yes. Direct parent models must exist in the warehouse before running unit tests. You can build them schema-only with `dbt run --select +my_model --exclude my_model --empty`, or use `dbt build --select my_model` which handles the full pipeline automatically.
Should I run unit tests in production?
No. dbt Labs recommends running unit tests only in development and CI environments. Use the `--exclude-resource-type` flag or `DBT_EXCLUDE_RESOURCE_TYPES` environment variable to skip them in production builds.
What data formats are supported for mock inputs and outputs?
The default format is `dict` (inline YAML). The skill also supports CSV and SQL formats for fixture data, with SQL required when testing models that depend on ephemeral models.
Can I unit test cross-project models or models from packages?
No. dbt only supports adding unit tests to models in your current project.

Generated from the current SKILL.md. These answers refresh after source changes.