kilo-org/kilo-marketplace/adding-dbt-unit-test
- community
Checking that the SKILL.md file is still in the repository.
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.
Install
npx vskill@latest install kilo-org/kilo-marketplace/adding-dbt-unit-testSKILL.md
Add unit test for a dbt model
Additional Resources
- Spec Reference - All required and optional YAML keys for unit tests
- Examples - Unit test examples across formats (dict, csv, sql)
- Incremental Models - Unit testing incremental models
- Ephemeral Dependencies - Unit testing models depending on ephemeral models
- Special Case Overrides - Introspective macros, project variables, environment variables
- Versioned Models - Unit testing versioned SQL models
- BigQuery Caveats - BigQuery-specific caveats
- BigQuery Data Types - BigQuery data type handling
- Postgres Data Types - Postgres data type handling
- Redshift Caveats - Redshift-specific caveats
- Redshift Data Types - Redshift data type handling
- Snowflake Data Types - Snowflake data type handling
- Spark Data Types - Spark data type handling
What are unit tests in dbt
dbt unit tests validate SQL modeling logic on static inputs before materializing in production. If any unit test for a model fails, dbt will not materialize that model.
When to use
You should unit test a model:
- Adding Model-Input-Output scenarios for the intended functionality of the model as well as edge cases to prevent regressions if the model logic is changed at a later date.
- Verifying that a bug fix solves a bug report for an existing dbt model.
More examples:
- When your SQL contains complex logic:
- Regex
- Date math
- Window functions
case whenstatements when there are manywhens- Truncation
- Complex joins (multiple joins, self-joins, or joins with non-trivial conditions)
- When you're writing custom logic to process input data, similar to creating a function.
- Logic for which you had bugs reported before.
- Edge cases not yet seen in your actual data that you want to be confident you are handling properly.
- Prior to refactoring the transformation logic (especially if the refactor is significant).
- Models with high "criticality" (public, contracted models or models directly upstream of an exposure).
When not to use
Cases we don't recommend creating unit tests for:
- Built-in functions that are tested extensively by the warehouse provider. If an unexpected issue arises, it's more likely a result of issues in the underlying data rather than the function itself. Therefore, fixture data in the unit test won't provide valuable information.
- common SQL spec functions like
min(), etc.
General format
dbt unit test uses a trio of the model, given inputs, and expected outputs (Model-Inputs-Outputs):
model- when building this modelgiveninputs - given a set of source, seeds, and models as preconditionsexpectoutput - then expect this row content of the model as a postcondition
Workflow
1. Choose the model to test
Self explanatory -- the title says it all!
2. Mock the inputs
- Create an input for each of the nodes the model depends on.
- Specify the mock data it should use.
- Specify the
formatif different than the default (YAMLdict). - See the "Data
formats for unit tests" section below to determine whichformatto use. - The mock data only needs include the subset of columns used within this test case.
Tip: Use dbt show to explore existing data from upstream models or sources. This helps you understand realistic input structures. However, always sanitize the sample data to remove any sensitive or PII information before using it in your unit test fixtures.
3. Mock the output
- Specify the data that you expect the model to create given those inputs.
- Specify the
formatif different than the default (YAMLdict). - See the "Data
formats for unit tests" section below to determine whichformatto use. - The mock data only needs include the subset of columns used within this test case.
Minimal unit test
Suppose you have this model:
Minimal unit test for that model:
Executing unit tests
Run the unit tests, build the model, and run the data tests for the hello_world model:
This saves on warehouse spend as the model will only be materialized and move on to the data tests if the unit tests pass successfully.
Or only run the unit tests without building the model or running the data tests:
Or choose a specific unit test by name:
Excluding unit tests from production builds
dbt Labs strongly recommends only running unit tests in development or CI environments. Since the inputs of the unit tests are static, there's no need to use additional compute cycles running them in production. Use them when doing development for a test-driven approach and CI to ensure changes don't break them.
Use the --resource-type flag --exclude-resource-type or the DBT_EXCLUDE_RESOURCE_TYPES environment variable to exclude unit tests from your production builds and save compute.
More realistic example
For more examples of unit tests, see references/examples.md
Supported and unsupported scenarios
- dbt only supports unit testing SQL models.
- Unit testing Python models is not supported.
- Unit testing non-model nodes like snapshots, seeds, sources, analyses, etc. is not supported.
- dbt only supports adding unit tests to models in your _current_ project.
- Unit testing cross-project models or models imported from a package is not supported.
- dbt _does not_ support unit testing models that use the
materialized viewmaterialization. - dbt _does not_ support unit testing models that use recursive SQL.
- dbt _does not_ support unit testing models that use introspective queries.
- dbt _does not_ support an
expectoutput for final state of the database table after inserting/merging for incremental models. - dbt _does_ support an
expectoutput for what will be merged/inserted for incremental models.
Handy to know
- Unit tests must be defined in a YAML file in your
model-pathsdirectory (models/by default) - Fixture files for unit tests must be defined in a SQL or CSV file in your
test-pathsdirectory (tests/fixturesby default) - Include all
reforsourcemodel references in the unit test configuration asinputs to avoid "node not found" errors during compilation. - If your model has multiple versions, by default the unit test will run on all versions of your model.
- If you want to unit test a model that depends on an ephemeral model, you must use
format: sqlfor the ephemeral model input. - Table names within the model must be aliased in order to unit test
joinlogic
YAML for specifying unit tests
- For all the required and optional keys in the YAML definition of unit tests, see references/spec.md
Inputs for unit tests
Use inputs in your unit tests to reference a specific model or source for the test:
- For
input:, use a string that represents areforsourcecall: ref('my_model')orref('my_model', v='2')orref('dougs_project', 'users')source('source_schema', 'source_name')- For seed inputs:
- If you do not supply an input for a seed, we will use the seed's CSV file _as_ the input.
- If you do supply an input for a seed, we will use that input instead.
- Use “empty” inputs by setting rows to an empty list
rows: [] - This is useful if the model has a
reforsourcedependency, but its values are irrelevant to this particular unit test. Just beware if the model has a join on that input that would cause rows to drop out!
models/schema.yml
Data formats for unit tests
dbt supports three formats for mock data within unit tests:
dict(default): Inline YAML dictionary values.csv: Inline CSV values or a CSV file.sql: Inline SQL query or a SQL file.
To see examples of each of the formats, see references/examples.md
How to choose the format
- Use the
dictformat by default, but fall back to another format as-needed. - Use the
sqlformat when testing a model that depends on anephemeralmodel - Use the
sqlformat when unit testing a column whose data type is not supported by thedictorcsvformats. - Use the
csvorsqlformats when using a fixture file. Default tocsv, but fallback tosqlif any of the column data types are not supported by thecsvformat. - The
sqlformat is the least readable and requires suppling mock data for _all_ columns, so prefer other formats when possible. But it is also the most flexible, and should be used as the fallback in scenarios wheredictorcsvwon't work.
Notes:
- For the
sqlformat you must supply mock data for _all columns_ whereasdictandcsvmay supply only a subset. - Only the
sqlformat allows you to unit test a model that depends on an ephemeral model --dictandcsvcan't be used in that case. - There are no formats that support Jinja.
Fixture files
The dict format only supports inline YAML mock data, but you can also use csv or sql either inline or in a separate fixture file. Store your fixture files in a fixtures subdirectory in any of your test-paths. For example, tests/fixtures/my_unit_test_fixture.sql.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/dbt/skills/adding-dbt-unit-test/SKILL.md
Popularity
- Repository stars
- 153
- Repository forks
- 77
- 7-day trend score
- 25
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/kilo-org/kilo-marketplace/adding-dbt-unit-test)