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-special-case-overrides.md

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

Unit test overrides

When configuring your unit test, you can override the output of macros, project variables, or environment variables for a given unit test.

models/schema.yml


 - name: test_my_model_overrides
    model: my_model
    given:
      - input: ref('my_model_a')
        rows:
          - {id: 1, a: 1}
      - input: ref('my_model_b')
        rows:
          - {id: 1, b: 2}
          - {id: 2, b: 2}
    overrides:
      macros:
        type_numeric: override
        invocation_id: 123
      vars:
        my_test: var_override
      env_vars:
        MY_TEST: env_var_override
    expect:
      rows:
        - {macro_call: override, var_call: var_override, env_var_call: env_var_override, invocation_id: 123}

Macros

You can override the output of any macro in your unit test defition.

If the model you're unit testing uses these macros, you must override them:

  • is_incremental: If you're unit testing an incremental model, you must explicity set is_incremental to true or false.

models/schema.yml


unit_tests:
  - name: my_unit_test
    model: my_incremental_model
    overrides:
      macros:
        # unit test this model in "full refresh" mode
        is_incremental: false 
    ...
  • dbt_utils.star: If you're unit testing a model that uses the star macro, you must explicity set star to a list of columns. This is because the star only accepts a relation for the from argument; the unit test mock input data is injected directly into the model SQL, replacing the ref() or source() function, causing the star macro to fail unless overidden.

models/schema.yml


unit_tests:
  - name: my_other_unit_test
    model: my_model_that_uses_star
    overrides:
      macros:
        # explicity set star to relevant list of columns
        dbt_utils.star: col_a,col_b,col_c 
    ...

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.