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…

\n )\n = true\n and accepted_email_domains.tld is not null,\n false) as is_valid_email_address\n from customers\n\t\tleft join accepted_email_domains\n on customers.email_top_level_domain = lower(accepted_email_domains.tld)\n\n)\n\nselect * from check_valid_emails\n```\n\nThe logic posed in this example can be challenging to validate. You can add a unit test to this model to ensure the `is_valid_email_address` logic captures all known edge cases: emails without `.`, emails without `@`, and emails from invalid domains.\n\n`dbt_project.yml`\n\n```yaml\nunit_tests:\n - name: test_is_valid_email_address\n description: \"Check my is_valid_email_address logic captures all known edge cases - emails without ., emails without @, and emails from invalid domains.\"\n\n # Model\n model: dim_customers\n\n # Inputs\n given:\n - input: ref('stg_customers')\n rows:\n - {email: [email protected], email_top_level_domain: example.com}\n - {email: [email protected], email_top_level_domain: unknown.com}\n - {email: badgmail.com, email_top_level_domain: gmail.com}\n - {email: missingdot@gmailcom, email_top_level_domain: gmail.com}\n - input: ref('top_level_email_domains')\n rows:\n - {tld: example.com}\n - {tld: gmail.com}\n\n # Output\n expect:\n rows:\n - {email: [email protected], is_valid_email_address: true}\n - {email: [email protected], is_valid_email_address: false}\n - {email: badgmail.com, is_valid_email_address: false}\n - {email: missingdot@gmailcom, is_valid_email_address: false}\n\n```\n\n# Data `format`s for unit tests\n\n## `dict`\n\n### Inline `dict` example\n\nThe `dict` data format is the default if no `format` is defined.\n\n`dict` requires an inline YAML dictionary for `rows`:\n\n`models/schema.yml`\n\n```yaml\nunit_tests:\n - name: test_my_model\n model: my_model\n given:\n - input: ref('my_model_a')\n format: dict\n rows:\n - {id: 1, name: gerda}\n - {id: 2, name: michelle}\n```\n\n## `csv`\n\n### Inline `csv` example\n\nWhen using the `csv` format, you can use either an inline CSV string for `rows`:\n\n`models/schema.yml`\n\n```yaml\n\nunit_tests:\n - name: test_my_model\n model: my_model\n given:\n - input: ref('my_model_a')\n format: csv\n rows: |\n id,name\n 1,gerda\n 2,michelle\n\n```\n\n### Fixture `csv` example\n\nOr, you can provide the name of a CSV file in the `test-paths` location (`tests/fixtures` by default): \n\n`models/schema.yml`\n\n```yaml\n\nunit_tests:\n - name: test_my_model\n model: my_model\n given:\n - input: ref('my_model_a')\n format: csv\n fixture: my_model_a_fixture\n\n```\n\n`tests/fixtures/my_model_a_fixture.csv`\n\n```csv\n\nid,name\n1,gerda\n2,michelle\n\n```\n\n## `sql`\n\nWhen using the `sql` format, you can use either an inline SQL query for `rows`:\n\n### Inline `sql` example\n\n`models/schema.yml`\n\n```yaml\n\nunit_tests:\n - name: test_my_model\n model: my_model\n given:\n - input: ref('my_model_a')\n format: sql\n rows: |\n select 1 as id, 'gerda' as name, null as loaded_at union all\n select 2 as id, 'michelle' as name, null as loaded_at\n\n```\n\n### Fixture `sql` example\n\nOr, you can provide the name of a SQL file in the `test-paths` location (`tests/fixtures` by default): \n\n`models/schema.yml`\n\n```yaml\n\nunit_tests:\n - name: test_my_model\n model: my_model\n given:\n - input: ref('my_model_a')\n format: sql\n fixture: my_model_a_fixture\n\n```\n\n`tests/fixtures/my_model_a_fixture.sql`\n\n```sql\n\nselect 1 as id, 'gerda' as name, null as loaded_at union all\nselect 2 as id, 'michelle', null as loaded_at as name\n\n```\n\n**Notes**\n- Contrary to dbt SQL models, Jinja is unsupported within SQL fixtures for unit tests.\n- You must supply mock data for _all columns_ when using the `sql` format.\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":4566,"content_sha256":"eeb3a31ba2224d611d4c29a3f3ac7907808fa0e6969d5c2186092529920219e1"},{"filename":"references/spec.md","content":"See below for all the required and optional keys in the YAML definition of unit tests.\n\n`models/schema.yml`\n\n```yml\n\nunit_tests:\n - name: \u003ctest-name> # this is the unique name of the test\n description: \u003cstring> # optional\n model: \u003cmodel-name> # required\n versions: # optional\n include: \u003clist-of-versions-to-include> # optional\n exclude: \u003clist-of-versions-to-exclude> # optional\n given: # required\n - input: \u003cref_or_source_call> # optional for seeds\n format: dict | csv | sql # If not configured, defaults to `dict`\n # either define `rows` inline or name of the `fixture`\n rows: {dictionary} | \u003cstring>\n fixture: \u003cfixture-name> # available option for `sql` or `csv` formats \n - input: ... # declare additional inputs\n expect: # required\n format: dict | csv | sql # If not configured, defaults to `dict`\n # either define `rows` inline or name of the `fixture`\n rows: {dictionary} | \u003cstring>\n fixture: \u003cfixture-name> # available option for `sql` or `csv` formats \n config: # optional\n meta: {dictionary} # optional\n tags: \u003cstring> | [\u003cstring>] # optional\n enabled: {boolean} # optional. v1.9 or higher. If not configured, defaults to `true`\n overrides: # optional: configuration for the dbt execution environment\n macros:\n is_incremental: true | false\n dbt_utils.current_timestamp: \u003cstring> # example macro name that your model depends upon \n # ... any other Jinja function\n # ... any other context property\n vars: {dictionary}\n env_vars: {dictionary}\n - name: \u003ctest-name> ... # declare additional unit tests\n\n ```\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":1688,"content_sha256":"8d3a4f9895d988a8eef262f44669b9e520b0299c44e9b58e4acc91bd2ab787f9"},{"filename":"references/special-cases-ephemeral-dependency.md","content":"## Unit testing a model that depends on ephemeral model(s)\n\nIf you want to unit test a model that depends on an ephemeral model, you must use `format: sql` for that input.\n\n```yml\nunit_tests:\n - name: my_unit_test\n model: dim_customers\n given:\n - input: ref('ephemeral_model')\n format: sql\n rows: |\n select 1 as id, 'emily' as name\n expect:\n rows:\n - {id: 1, first_name: emily}\n```\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":432,"content_sha256":"9333220abbe0efe146631405a380fd1b2a98c972527df4151fa178d253e3a41e"},{"filename":"references/special-cases-incremental-model.md","content":"## Unit testing incremental models\n\nWhen 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.\n\n### Note\nIncremental 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.\n\n ```shell\n dbt run --select \"config.materialized:incremental\" --empty\n ```\n\n After running the command, you can then perform a regular `dbt build` for that model and then run your unit test.\n\nWhen 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).\n\nFor example, say you have an incremental model in your project:\n\n`my_incremental_model.sql`\n\n```sql\n\n{{\n config(\n materialized='incremental'\n )\n}}\n\nselect * from {{ ref('events') }}\n{% if is_incremental() %}\nwhere event_time > (select max(event_time) from {{ this }})\n{% endif %}\n\n```\n\nYou can define unit tests on `my_incremental_model` to ensure your incremental logic is working as expected:\n\n```yml\n\nunit_tests:\n - name: my_incremental_model_full_refresh_mode\n model: my_incremental_model\n overrides:\n macros:\n # unit test this model in \"full refresh\" mode\n is_incremental: false \n given:\n - input: ref('events')\n rows:\n - {event_id: 1, event_time: 2020-01-01}\n expect:\n rows:\n - {event_id: 1, event_time: 2020-01-01}\n\n - name: my_incremental_model_incremental_mode\n model: my_incremental_model\n overrides:\n macros:\n # unit test this model in \"incremental\" mode\n is_incremental: true \n given:\n - input: ref('events')\n rows:\n - {event_id: 1, event_time: 2020-01-01}\n - {event_id: 2, event_time: 2020-01-02}\n - {event_id: 3, event_time: 2020-01-03}\n - input: this \n # contents of current my_incremental_model\n rows:\n - {event_id: 1, event_time: 2020-01-01}\n expect:\n # what will be inserted/merged into my_incremental_model\n rows:\n - {event_id: 2, event_time: 2020-01-02}\n - {event_id: 3, event_time: 2020-01-03}\n\n```\n\nThere 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.\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":2652,"content_sha256":"9264d0bef874c77b7fc19a7e1406409f24f693bbd6e3e479bbe19839c9747509"},{"filename":"references/special-cases-special-case-overrides.md","content":"# Unit test overrides\n\nWhen configuring your unit test, you can override the output of macros, project variables, or environment variables for a given unit test.\n\n`models/schema.yml`\n\n```yml\n\n - name: test_my_model_overrides\n model: my_model\n given:\n - input: ref('my_model_a')\n rows:\n - {id: 1, a: 1}\n - input: ref('my_model_b')\n rows:\n - {id: 1, b: 2}\n - {id: 2, b: 2}\n overrides:\n macros:\n type_numeric: override\n invocation_id: 123\n vars:\n my_test: var_override\n env_vars:\n MY_TEST: env_var_override\n expect:\n rows:\n - {macro_call: override, var_call: var_override, env_var_call: env_var_override, invocation_id: 123}\n\n```\n\n## Macros\n\nYou can override the output of any macro in your unit test defition. \n\nIf the model you're unit testing uses these macros, you must override them:\n - `is_incremental`: If you're unit testing an incremental model, you must explicity set `is_incremental` to `true` or `false`.\n\n`models/schema.yml`\n\n ```yml\n\n unit_tests:\n - name: my_unit_test\n model: my_incremental_model\n overrides:\n macros:\n # unit test this model in \"full refresh\" mode\n is_incremental: false \n ...\n\n ```\n\n - `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.\n\n`models/schema.yml`\n\n ```yml\n\n unit_tests:\n - name: my_other_unit_test\n model: my_model_that_uses_star\n overrides:\n macros:\n # explicity set star to relevant list of columns\n dbt_utils.star: col_a,col_b,col_c \n ...\n\n ``` \n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":1924,"content_sha256":"8f90954286cbe1c5ab105137255000aeea641413ed1f23324abb874fcae82137"},{"filename":"references/special-cases-versioned-model.md","content":"# Unit testing versioned SQL models\n\nIf your model has multiple versions, the default unit test will run on _all_ versions of your model. To specify version(s) of your model to unit test, use `include` or `exclude` for the desired versions in your model versions config:\n\n`models/schema.yml`\n\n```yaml\n\n# my test_is_valid_email_address unit test will run on all versions of my_model\nunit_tests:\n - name: test_is_valid_email_address\n model: my_model\n ...\n \n# my test_is_valid_email_address unit test will run on ONLY version 2 of my_model\nunit_tests:\n - name: test_is_valid_email_address \n model: my_model \n versions:\n include: \n - 2\n ...\n \n# my test_is_valid_email_address unit test will run on all versions EXCEPT 1 of my_model\nunit_tests:\n - name: test_is_valid_email_address\n model: my_model \n versions:\n exclude: \n - 1\n ...\n\n```\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":905,"content_sha256":"1a88625d7a9166881e945125ea6f7b1b3963bb5e9f3e1efbf1d9d996d2e34005"},{"filename":"references/warehouse-bigquery-caveats.md","content":"# Caveats for BigQuery\n\n- You must specify _all_ fields in a BigQuery `STRUCT` in a unit test. You cannot use only a subset of fields in a `STRUCT`.\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":149,"content_sha256":"c259e4400cda127e3e6ac1044e70e68b4f99fa179dc7f22ae1bddef6112cd16c"},{"filename":"references/warehouse-bigquery-data-types.md","content":"### BigQuery\n\nPlatform-specific data type examples:\n\n```yaml\nunit_tests:\n - name: test_my_data_types\n model: fct_data_types\n given:\n - input: ref('stg_data_types')\n rows:\n - int_field: 1\n float_field: 2.0\n str_field: my_string\n str_escaped_field: \"my,cool'string\"\n date_field: 2020-01-02\n timestamp_field: 2013-11-03 00:00:00-0\n timestamptz_field: 2013-11-03 00:00:00-0\n bigint_field: 1\n geography_field: 'st_geogpoint(75, 45)'\n json_field: {\"name\": \"Cooper\", \"forname\": \"Alice\"}\n str_array_field: ['a','b','c']\n int_array_field: [1, 2, 3]\n date_array_field: ['2020-01-01']\n struct_field: 'struct(\"Isha\" as name, 22 as age)'\n struct_of_struct_field: 'struct(struct(1 as id, \"blue\" as color) as my_struct)'\n struct_array_field: ['struct(st_geogpoint(75, 45) as my_point)', 'struct(st_geogpoint(75, 35) as my_point)']\n # Make sure to include **all** the fields in a BigQuery `struct` within the unit test.\n # It's not currently possible to use only a subset of columns in a 'struct'\n```\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":1182,"content_sha256":"7c605113880c1cb7febf4313c3cd44ecf6fed7fec22a9f580430029a53564326"},{"filename":"references/warehouse-postgres-data-types.md","content":"### Postgres\n\nPlatform-specific data type examples:\n\n```yaml\n\nunit_tests:\n - name: test_my_data_types\n model: fct_data_types\n given:\n - input: ref('stg_data_types')\n rows:\n - int_field: 1\n float_field: 2.0\n numeric_field: 1\n str_field: my_string\n str_escaped_field: \"my,cool'string\"\n bool_field: true\n date_field: 2020-01-02\n timestamp_field: 2013-11-03 00:00:00-0\n timestamptz_field: 2013-11-03 00:00:00-0\n json_field: '{\"bar\": \"baz\", \"balance\": 7.77, \"active\": false}'\n```\n\nCurrently, the `array` data type is not supported for YAML format `dict` inputs. Use the `sql` format instead if you need to mock `array` inputs or outputs.\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":750,"content_sha256":"be4f2f6c66b722baadc827fa81e4ca3f1ce3175cb002d2a8dfb33aeef5cdbba0"},{"filename":"references/warehouse-redshift-caveats.md","content":"# Caveats for Redshift\n\n## Unit test limitations for Redshift\n\n- Redshift doesn't support unit tests when the SQL in the common table expression (CTE) contains functions such as `LISTAGG`, `MEDIAN`, `PERCENTILE_CONT`, and so on. These functions must be executed against a user-created table. dbt combines given rows to be part of the CTE, which Redshift does not support.\n\n In order to support this pattern in the future, dbt would need to \"materialize\" the input fixtures as tables, rather than interpolating them as CTEs. Adding this functionality is proposed in GitHub issue #8499.\n\n- Redshift doesn't support unit tests that rely on sources in a database that differs from the models. Redshift sources need to be in the same database as the models.\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":754,"content_sha256":"5bec6b91712be0b93061982c9923acd6bda2eb02a67697fdc13cabffc3f948b1"},{"filename":"references/warehouse-redshift-data-types.md","content":"### Redshift\n\nPlatform-specific data type examples:\n\n```yaml\n\nunit_tests:\n - name: test_my_data_types\n model: fct_data_types\n given:\n - input: ref('stg_data_types')\n rows:\n - int_field: 1\n float_field: 2.0\n str_field: my_string\n str_escaped_field: \"my,cool'string\"\n date_field: 2020-01-02\n timestamp_field: 2013-11-03 00:00:00-0\n timestamptz_field: 2013-11-03 00:00:00-0\n json_field: '{\"bar\": \"baz\", \"balance\": 7.77, \"active\": false}'\n```\n\nCurrently, the `array` data type is not supported for YAML format `dict` inputs. Use the `sql` format instead if you need to mock `array` inputs or outputs.\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":694,"content_sha256":"82669225e3de247388d56341ea66deaf436395ef2fff34911c79dceb93aa6138"},{"filename":"references/warehouse-snowflake-data-types.md","content":"### Snowflake\n\nPlatform-specific data type examples:\n\n```yaml\nunit_tests:\n - name: test_my_data_types\n model: fct_data_types\n given:\n - input: ref('stg_data_types')\n rows:\n - int_field: 1\n float_field: 2.0\n str_field: my_string\n str_escaped_field: \"my,cool'string\"\n date_field: 2020-01-02\n timestamp_field: 2013-11-03 00:00:00-0\n timestamptz_field: 2013-11-03 00:00:00-0\n number_field: 3\n variant_field: 3\n geometry_field: POINT(1820.12 890.56)\n geography_field: POINT(-122.35 37.55)\n object_field: {'Alberta':'Edmonton','Manitoba':'Winnipeg'}\n str_array_field: ['a','b','c']\n int_array_field: [1, 2, 3]\n binary_field: 19E1FFDCCB6CDEE788BF631C1C4905D1\n```\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":823,"content_sha256":"d7eb96ce9710da807eab9e2e6c2667970c80c97681726e58176ac7b036d82ad2"},{"filename":"references/warehouse-spark-data-types.md","content":"### Spark\n\nPlatform-specific data type examples:\n\n```yaml\n\nunit_tests:\n - name: test_my_data_types\n model: fct_data_types\n given:\n - input: ref('stg_data_types')\n rows:\n - int_field: 1\n float_field: 2.0\n str_field: my_string\n str_escaped_field: \"my,cool'string\"\n bool_field: true\n date_field: 2020-01-02\n timestamp_field: 2013-11-03 00:00:00-0\n timestamptz_field: 2013-11-03 00:00:00-0\n int_array_field: 'array(1, 2, 3)'\n map_field: 'map(\"10\", \"t\", \"15\", \"f\", \"20\", NULL)'\n named_struct_field: 'named_struct(\"a\", 1, \"b\", 2, \"c\", 3)'\n```\n","content_type":"text/markdown; charset=utf-8","language":"markdown","size":664,"content_sha256":"293587b72bb78c366d798e261373c22025b5db6b319932ac8e0dbb9a62b749c7"}],"content_json":{"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"text":"Add unit test for a dbt model","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Additional Resources","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Spec Reference","type":"text","marks":[{"type":"link","attrs":{"href":"references/spec.md","title":null}}]},{"text":" - All required and optional YAML keys for unit tests","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Examples","type":"text","marks":[{"type":"link","attrs":{"href":"references/examples.md","title":null}}]},{"text":" - Unit test examples across formats (dict, csv, sql)","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Incremental Models","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-incremental-model.md","title":null}}]},{"text":" - Unit testing incremental models","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Ephemeral Dependencies","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-ephemeral-dependency.md","title":null}}]},{"text":" - Unit testing models depending on ephemeral models","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Special Case Overrides","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-special-case-overrides.md","title":null}}]},{"text":" - Introspective macros, project variables, environment variables","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Versioned Models","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-versioned-model.md","title":null}}]},{"text":" - Unit testing versioned SQL models","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"BigQuery Caveats","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-bigquery-caveats.md","title":null}}]},{"text":" - BigQuery-specific caveats","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"BigQuery Data Types","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-bigquery-data-types.md","title":null}}]},{"text":" - BigQuery data type handling","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Postgres Data Types","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-postgres-data-types.md","title":null}}]},{"text":" - Postgres data type handling","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Redshift Caveats","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-redshift-caveats.md","title":null}}]},{"text":" - Redshift-specific caveats","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Redshift Data Types","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-redshift-data-types.md","title":null}}]},{"text":" - Redshift data type handling","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Snowflake Data Types","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-snowflake-data-types.md","title":null}}]},{"text":" - Snowflake data type handling","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Spark Data Types","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-spark-data-types.md","title":null}}]},{"text":" - Spark data type handling","type":"text"}]}]}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"What are unit tests in dbt","type":"text"}]},{"type":"paragraph","content":[{"text":"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.","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"When to use","type":"text"}]},{"type":"paragraph","content":[{"text":"You should unit test a model:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"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.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Verifying that a bug fix solves a bug report for an existing dbt model.","type":"text"}]}]}]},{"type":"paragraph","content":[{"text":"More examples:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"When your SQL contains complex logic:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Regex","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Date math","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Window functions","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"case when","type":"text","marks":[{"type":"code_inline"}]},{"text":" statements when there are many ","type":"text"},{"text":"when","type":"text","marks":[{"type":"code_inline"}]},{"text":"s","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Truncation","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Complex joins (multiple joins, self-joins, or joins with non-trivial conditions)","type":"text"}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"When you're writing custom logic to process input data, similar to creating a function.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Logic for which you had bugs reported before.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Edge cases not yet seen in your actual data that you want to be confident you are handling properly.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Prior to refactoring the transformation logic (especially if the refactor is significant).","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Models with high \"criticality\" (public, contracted models or models directly upstream of an exposure).","type":"text"}]}]}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"When not to use","type":"text"}]},{"type":"paragraph","content":[{"text":"Cases we don't recommend creating unit tests for:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"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.","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"common SQL spec functions like ","type":"text"},{"text":"min()","type":"text","marks":[{"type":"code_inline"}]},{"text":", etc.","type":"text"}]}]}]}]}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"General format","type":"text"}]},{"type":"paragraph","content":[{"text":"dbt unit test uses a trio of the model, given inputs, and expected outputs (Model-Inputs-Outputs):","type":"text"}]},{"type":"ordered_list","attrs":{"order":1,"listStyle":"number"},"content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"model","type":"text","marks":[{"type":"code_inline"}]},{"text":" - when building this model","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"given","type":"text","marks":[{"type":"code_inline"}]},{"text":" inputs - given a set of source, seeds, and models as preconditions","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"expect","type":"text","marks":[{"type":"code_inline"}]},{"text":" output - then expect this row content of the model as a postcondition","type":"text"}]}]}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"Workflow","type":"text"}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"1. Choose the model to test","type":"text"}]},{"type":"paragraph","content":[{"text":"Self explanatory -- the title says it all!","type":"text"}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"2. Mock the inputs","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Create an input for each of the nodes the model depends on.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Specify the mock data it should use.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Specify the ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":" if different than the default (YAML ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":").","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"See the \"Data ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":"s for unit tests\" section below to determine which ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":" to use.","type":"text"}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"The mock data only needs include the subset of columns used within this test case.","type":"text"}]}]}]},{"type":"paragraph","content":[{"text":"Tip:","type":"text","marks":[{"type":"strong"}]},{"text":" Use ","type":"text"},{"text":"dbt show","type":"text","marks":[{"type":"code_inline"}]},{"text":" 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.","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"shell"},"content":[{"text":"# Preview upstream model data\ndbt show --select upstream_model --limit 5","type":"text"}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"3. Mock the output","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Specify the data that you expect the model to create given those inputs.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Specify the ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":" if different than the default (YAML ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":").","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"See the \"Data ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":"s for unit tests\" section below to determine which ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":" to use.","type":"text"}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"The mock data only needs include the subset of columns used within this test case.","type":"text"}]}]}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"4. Ensure upstream models exist before running","type":"text"}]},{"type":"paragraph","content":[{"text":"Unit tests require direct parent models to exist in the warehouse.","type":"text","marks":[{"type":"strong"}]},{"text":" Before running unit tests standalone (","type":"text"},{"text":"dbt test","type":"text","marks":[{"type":"code_inline"}]},{"text":"), verify that upstream models already exist first:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"bash"},"content":[{"text":"# Check if upstream models exist in the warehouse\ndbt list --select +my_model --exclude my_model --resource-type model\n# Then verify the tables/views actually exist in the warehouse via dbt show or your SQL client\ndbt show --select upstream_model --limit 1","type":"text"}]},{"type":"paragraph","content":[{"text":"If upstream models ","type":"text"},{"text":"do not exist","type":"text","marks":[{"type":"strong"}]},{"text":", or ","type":"text"},{"text":"exist but have been modified and not yet refreshed","type":"text","marks":[{"type":"strong"}]},{"text":", build them using ","type":"text"},{"text":"--empty","type":"text","marks":[{"type":"code_inline"}]},{"text":" to create schema-only versions:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"bash"},"content":[{"text":"# Build upstream models cheaply (schema only, no data read)\ndbt run --select +my_model --exclude my_model --empty","type":"text"}]},{"type":"blockquote","content":[{"type":"paragraph","content":[{"text":"Warning:","type":"text","marks":[{"type":"strong"}]},{"text":" ","type":"text"},{"text":"--empty","type":"text","marks":[{"type":"code_inline"}]},{"text":" overwrites existing models with schema-only (zero-row) versions. Only use it when models don't exist yet, or when schema changes need to be applied. Do not use it if upstream models contain data you want to preserve — it will wipe that data.","type":"text"}]}]},{"type":"paragraph","content":[{"text":"Skip this step if using ","type":"text"},{"text":"dbt build --select my_model","type":"text","marks":[{"type":"code_inline"}]},{"text":" (recommended) — it handles the full pipeline including unit tests.","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Minimal unit test","type":"text"}]},{"type":"paragraph","content":[{"text":"Suppose you have this model:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"sql"},"content":[{"text":"-- models/hello_world.sql\n\nselect 'world' as hello","type":"text"}]},{"type":"paragraph","content":[{"text":"Minimal unit test for that model:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"yaml"},"content":[{"text":"# models/_properties.yml\n\nunit_tests:\n - name: test_hello_world\n\n # Always only one transformation to test\n model: hello_world\n\n # No inputs needed this time!\n # Most unit tests will have inputs -- see the \"real world example\" section below\n given: []\n\n # Expected output can have zero to many rows\n expect:\n rows:\n - {hello: world}","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Executing unit tests","type":"text"}]},{"type":"paragraph","content":[{"text":"Run the unit tests, build the model, and run the data tests for the ","type":"text"},{"text":"hello_world","type":"text","marks":[{"type":"code_inline"}]},{"text":" model:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"shell"},"content":[{"text":"dbt build --select hello_world","type":"text"}]},{"type":"paragraph","content":[{"text":"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.","type":"text"}]},{"type":"paragraph","content":[{"text":"Or only run the unit tests without building the model or running the data tests:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"shell"},"content":[{"text":"dbt test --select \"hello_world,test_type:unit\"","type":"text"}]},{"type":"paragraph","content":[{"text":"Or choose a specific unit test by name:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"shell"},"content":[{"text":"dbt test --select test_is_valid_email_address","type":"text"}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"Excluding unit tests from production builds","type":"text"}]},{"type":"paragraph","content":[{"text":"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.","type":"text"}]},{"type":"paragraph","content":[{"text":"Use the ","type":"text"},{"text":"--resource-type","type":"text","marks":[{"type":"code_inline"}]},{"text":" flag ","type":"text"},{"text":"--exclude-resource-type","type":"text","marks":[{"type":"code_inline"}]},{"text":" or the ","type":"text"},{"text":"DBT_EXCLUDE_RESOURCE_TYPES","type":"text","marks":[{"type":"code_inline"}]},{"text":" environment variable to exclude unit tests from your production builds and save compute.","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"More realistic example","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"yaml"},"content":[{"text":"unit_tests:\n\n - name: test_order_items_count_drink_items_with_zero_drinks\n description: >\n Scenario: Order without any drinks\n When the `order_items_summary` table is built\n Given an order with nothing but 1 food item\n Then the count of drink items is 0\n\n # Model\n model: order_items_summary\n\n # Inputs\n given:\n - input: ref('order_items')\n rows:\n - {\n order_id: 76,\n order_item_id: 3,\n is_drink_item: false,\n }\n - input: ref('stg_orders')\n rows:\n - { order_id: 76 }\n\n # Output\n expect:\n rows:\n - {\n order_id: 76,\n count_drink_items: 0,\n }","type":"text"}]},{"type":"paragraph","content":[{"text":"For more examples of unit tests, see ","type":"text"},{"text":"references/examples.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/examples.md","title":null}}]}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Supported and unsupported scenarios","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt only supports unit testing SQL models.","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit testing Python models is not supported.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit testing non-model nodes like snapshots, seeds, sources, analyses, etc. is not supported.","type":"text"}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt only supports adding unit tests to models in your ","type":"text"},{"text":"current","type":"text","marks":[{"type":"em"}]},{"text":" project.","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit testing cross-project models or models imported from a package is not supported.","type":"text"}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt ","type":"text"},{"text":"does not","type":"text","marks":[{"type":"em"}]},{"text":" support unit testing models that use the ","type":"text"},{"text":"materialized view","type":"text","marks":[{"type":"code_inline"}]},{"text":" materialization.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt ","type":"text"},{"text":"does not","type":"text","marks":[{"type":"em"}]},{"text":" support unit testing models that use recursive SQL.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt ","type":"text"},{"text":"does not","type":"text","marks":[{"type":"em"}]},{"text":" support unit testing models that use introspective queries.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt ","type":"text"},{"text":"does not","type":"text","marks":[{"type":"em"}]},{"text":" support an ","type":"text"},{"text":"expect","type":"text","marks":[{"type":"code_inline"}]},{"text":" output for final state of the database table after inserting/merging for incremental models.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dbt ","type":"text"},{"text":"does","type":"text","marks":[{"type":"em"}]},{"text":" support an ","type":"text"},{"text":"expect","type":"text","marks":[{"type":"code_inline"}]},{"text":" output for what will be merged/inserted for incremental models.","type":"text"}]}]}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Handy to know","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit tests must be defined in a YAML file in your ","type":"text"},{"text":"model-paths","type":"text","marks":[{"type":"code_inline"}]},{"text":" directory (","type":"text"},{"text":"models/","type":"text","marks":[{"type":"code_inline"}]},{"text":" by default)","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Fixture files for unit tests must be defined in a SQL or CSV file in your ","type":"text"},{"text":"test-paths","type":"text","marks":[{"type":"code_inline"}]},{"text":" directory (","type":"text"},{"text":"tests/fixtures","type":"text","marks":[{"type":"code_inline"}]},{"text":" by default)","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Include all ","type":"text"},{"text":"ref","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"source","type":"text","marks":[{"type":"code_inline"}]},{"text":" model references in the unit test configuration as ","type":"text"},{"text":"input","type":"text","marks":[{"type":"code_inline"}]},{"text":"s to avoid \"node not found\" errors during compilation.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"If your model has multiple versions, by default the unit test will run on ","type":"text"},{"text":"all","type":"text","marks":[{"type":"em"}]},{"text":" versions of your model.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"If you want to unit test a model that depends on an ephemeral model, you must use ","type":"text"},{"text":"format: sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" for the ephemeral model input.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Table names within the model must be aliased in order to unit test ","type":"text"},{"text":"join","type":"text","marks":[{"type":"code_inline"}]},{"text":" logic","type":"text"}]}]}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"YAML for specifying unit tests","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"For all the required and optional keys in the YAML definition of unit tests, see ","type":"text"},{"text":"references/spec.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/spec.md","title":null}}]}]}]}]},{"type":"heading","attrs":{"level":1},"content":[{"text":"Inputs for unit tests","type":"text"}]},{"type":"paragraph","content":[{"text":"Use ","type":"text"},{"text":"input","type":"text","marks":[{"type":"code_inline"}]},{"text":"s in your unit tests to reference a specific model or source for the test:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"For ","type":"text"},{"text":"input:","type":"text","marks":[{"type":"code_inline"}]},{"text":", use a string that represents a ","type":"text"},{"text":"ref","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"source","type":"text","marks":[{"type":"code_inline"}]},{"text":" call:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"ref('my_model')","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"ref('my_model', v='2')","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"ref('dougs_project', 'users')","type":"text","marks":[{"type":"code_inline"}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"source('source_schema', 'source_name')","type":"text","marks":[{"type":"code_inline"}]}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"For seed inputs:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"If you do not supply an input for a seed, we will use the seed's CSV file ","type":"text"},{"text":"as","type":"text","marks":[{"type":"em"}]},{"text":" the input.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"If you do supply an input for a seed, we will use that input instead.","type":"text"}]}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Use “empty” inputs by setting rows to an empty list ","type":"text"},{"text":"rows: []","type":"text","marks":[{"type":"code_inline"}]}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"This is useful if the model has a ","type":"text"},{"text":"ref","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"source","type":"text","marks":[{"type":"code_inline"}]},{"text":" dependency, 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!","type":"text"}]}]}]}]}]},{"type":"paragraph","content":[{"text":"models/schema.yml","type":"text","marks":[{"type":"code_inline"}]}]},{"type":"code_block","attrs":{"wrap":false,"language":"yaml"},"content":[{"text":"unit_tests:\n - name: test_is_valid_email_address # this is the unique name of the test\n model: dim_customers # name of the model I'm unit testing\n given: # the mock data for your inputs\n - input: ref('stg_customers')\n rows:\n - {email: [email protected], email_top_level_domain: example.com}\n - {email: [email protected], email_top_level_domain: unknown.com}\n - {email: badgmail.com, email_top_level_domain: gmail.com}\n - {email: missingdot@gmailcom, email_top_level_domain: gmail.com}\n - input: ref('top_level_email_domains')\n rows:\n - {tld: example.com}\n - {tld: gmail.com}\n - input: ref('irrelevant_dependency') # dependency that we need to acknowlege, but does not need any data\n rows: []\n...\n","type":"text"}]},{"type":"heading","attrs":{"level":1},"content":[{"text":"Data ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]},{"text":"s for unit tests","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"dict","type":"text","marks":[{"type":"code_inline"},{"type":"strong"}]},{"text":" is the default format","type":"text","marks":[{"type":"strong"}]},{"text":" — always start here","type":"text"}]},{"type":"paragraph","content":[{"text":"Unless you have a specific reason to use another format, use ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" (inline YAML). It is the default when ","type":"text"},{"text":"format:","type":"text","marks":[{"type":"code_inline"}]},{"text":" is omitted.","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"yaml"},"content":[{"text":"given:\n - input: ref('orders')\n # no format: key needed — dict is the default\n rows:\n - {order_id: 1, status: completed, amount: 100}\n - {order_id: 2, status: pending, amount: 50}","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"How to choose the ","type":"text"},{"text":"format","type":"text","marks":[{"type":"code_inline"}]}]},{"type":"table","attrs":{"layout":null},"content":[{"type":"tr","content":[{"type":"th","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Use ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" (default)","type":"text"}]}]},{"type":"th","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Use ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]}]}]},{"type":"th","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Use ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Everything else — this is the starting point","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Model depends on an ","type":"text"},{"text":"ephemeral","type":"text","marks":[{"type":"strong"}]},{"text":" model","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Using an external fixture file","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph"}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Column data type not supported by dict/csv","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Column data type not supported by dict","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph"}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"External fixture file with unsupported types","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph"}]}]}]},{"type":"paragraph","content":[{"text":"Critical ","type":"text","marks":[{"type":"strong"}]},{"text":"sql","type":"text","marks":[{"type":"code_inline"},{"type":"strong"}]},{"text":" note","type":"text","marks":[{"type":"strong"}]},{"text":": ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" format requires specifying ","type":"text"},{"text":"ALL columns","type":"text","marks":[{"type":"strong"}]},{"text":" in the mock data. ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" and ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" only require the columns relevant to the test — much more concise.","type":"text"}]},{"type":"paragraph","content":[{"text":"Critical ","type":"text","marks":[{"type":"strong"}]},{"text":"sql","type":"text","marks":[{"type":"code_inline"},{"type":"strong"}]},{"text":" requirement","type":"text","marks":[{"type":"strong"}]},{"text":": If any of your model's ","type":"text"},{"text":"ref()","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"source()","type":"text","marks":[{"type":"code_inline"}]},{"text":" inputs are ephemeral models, you ","type":"text"},{"text":"must","type":"text","marks":[{"type":"strong"}]},{"text":" use ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" format for those inputs. ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" and ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" will fail.","type":"text"}]},{"type":"paragraph","content":[{"text":"dbt supports three formats for mock data within unit tests:","type":"text"}]},{"type":"ordered_list","attrs":{"order":1,"listStyle":"number"},"content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" (default): Inline YAML dictionary values.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":": Inline CSV values or a CSV file.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":": Inline SQL query or a SQL file.","type":"text"}]}]}]},{"type":"paragraph","content":[{"text":"To see examples of each of the formats, see ","type":"text"},{"text":"references/examples.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/examples.md","title":null}}]}]},{"type":"paragraph","content":[{"text":"Notes:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"For the ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" format you must supply mock data for ","type":"text"},{"text":"all columns","type":"text","marks":[{"type":"em"}]},{"text":" whereas ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" and ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" may supply only a subset.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Only the ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" format allows you to unit test a model that depends on an ephemeral model -- ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" and ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" can't be used in that case.","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"There are no formats that support Jinja.","type":"text"}]}]}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"Fixture files","type":"text"}]},{"type":"paragraph","content":[{"text":"The ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" format only supports inline YAML mock data, but you can also use ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" either inline or in a separate fixture file. Store your fixture files in a ","type":"text"},{"text":"fixtures","type":"text","marks":[{"type":"code_inline"}]},{"text":" subdirectory in any of your ","type":"text"},{"text":"test-paths","type":"text","marks":[{"type":"code_inline"}]},{"text":". For example, ","type":"text"},{"text":"tests/fixtures/my_unit_test_fixture.sql","type":"text","marks":[{"type":"code_inline"}]},{"text":".","type":"text"}]},{"type":"paragraph","content":[{"text":"When using the ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" format, you only have to define the mock data for the columns relevant to you. This enables you to write succinct and ","type":"text"},{"text":"specific","type":"text","marks":[{"type":"em"}]},{"text":" unit tests. For the ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" format ","type":"text"},{"text":"all","type":"text","marks":[{"type":"em"}]},{"text":" columns need to be defined.","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Special cases","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit testing incremental models. See ","type":"text"},{"text":"references/special-cases-incremental-model.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-incremental-model.md","title":null}}]},{"text":".","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit testing a model that depends on ephemeral model(s). See ","type":"text"},{"text":"references/special-cases-ephemeral-dependency.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-ephemeral-dependency.md","title":null}}]},{"text":".","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit test a model that depends on any introspective macros, project variables, or environment variables. See ","type":"text"},{"text":"references/special-cases-special-case-overrides.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-special-case-overrides.md","title":null}}]},{"text":".","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"Unit testing versioned SQL models. See ","type":"text"},{"text":"references/special-cases-versioned-model.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/special-cases-versioned-model.md","title":null}}]},{"text":".","type":"text"}]}]}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"Platform/adapter-specific caveats","type":"text"}]},{"type":"paragraph","content":[{"text":"There are platform-specific details required if implementing on (Redshift, BigQuery, etc). Read the caveats file for your database (if it exists):","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-bigquery-caveats.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-bigquery-caveats.md","title":null}}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-redshift-caveats.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-redshift-caveats.md","title":null}}]}]}]}]},{"type":"heading","attrs":{"level":1},"content":[{"text":"Platform/adapter-specific data types","type":"text"}]},{"type":"paragraph","content":[{"text":"Unit tests are designed to test for the expected ","type":"text"},{"text":"values","type":"text","marks":[{"type":"em"}]},{"text":", not for the data types themselves. dbt takes the value you provide and attempts to cast it to the data type as inferred from the input and output models.","type":"text"}]},{"type":"paragraph","content":[{"text":"How you specify input and expected values in your unit test YAML definitions are largely consistent across data warehouses, with some variation for more complex data types.","type":"text"}]},{"type":"paragraph","content":[{"text":"Read the data types file for your database:","type":"text"}]},{"type":"bullet_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-bigquery-data-types.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-bigquery-data-types.md","title":null}}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-postgres-data-types.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-postgres-data-types.md","title":null}}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-redshift-data-types.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-redshift-data-types.md","title":null}}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-snowflake-data-types.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-snowflake-data-types.md","title":null}}]}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"references/warehouse-spark-data-types.md","type":"text","marks":[{"type":"link","attrs":{"href":"references/warehouse-spark-data-types.md","title":null}}]}]}]}]},{"type":"heading","attrs":{"level":1},"content":[{"text":"Disabling a unit test","type":"text"}]},{"type":"paragraph","content":[{"text":"By default, all specified unit tests are enabled and will be included according to the ","type":"text"},{"text":"--select","type":"text","marks":[{"type":"code_inline"}]},{"text":" flag.","type":"text"}]},{"type":"paragraph","content":[{"text":"To disable a unit test from being executed, set:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"yaml"},"content":[{"text":" config: \n enabled: false","type":"text"}]},{"type":"paragraph","content":[{"text":"This is helpful if a unit test is incorrectly failing and it needs to be disabled until it is fixed.","type":"text"}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"When a unit test fails","type":"text"}]},{"type":"paragraph","content":[{"text":"When a unit test fails, there will be a log message of \"actual differs from expected\", and it will show a \"data diff\" between the two:","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":""},"content":[{"text":"actual differs from expected:\n\n@@ ,email ,is_valid_email_address\n→ ,[email protected],True→False\n ,[email protected],False","type":"text"}]},{"type":"paragraph","content":[{"text":"There are two main possibilities when a unit test fails:","type":"text"}]},{"type":"ordered_list","attrs":{"order":1,"listStyle":"number"},"content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"There was an error in the way the unit test was constructed (false positive)","type":"text"}]}]},{"type":"list_item","content":[{"type":"paragraph","content":[{"text":"There is an bug is the model (true positive)","type":"text"}]}]}]},{"type":"paragraph","content":[{"text":"It takes expert judgement to determine one from the other.","type":"text"}]},{"type":"heading","attrs":{"level":3},"content":[{"text":"The ","type":"text"},{"text":"--empty","type":"text","marks":[{"type":"code_inline"}]},{"text":" flag","type":"text"}]},{"type":"paragraph","content":[{"text":"The direct parents of the model that you’re unit testing need to exist in the warehouse before you can execute the unit test. The ","type":"text"},{"text":"run","type":"text","marks":[{"type":"code_inline"}]},{"text":" and ","type":"text"},{"text":"build","type":"text","marks":[{"type":"code_inline"}]},{"text":" commands supports the ","type":"text"},{"text":"--empty","type":"text","marks":[{"type":"code_inline"}]},{"text":" flag for building schema-only dry runs. The ","type":"text"},{"text":"--empty","type":"text","marks":[{"type":"code_inline"}]},{"text":" flag limits the ","type":"text"},{"text":"ref","type":"text","marks":[{"type":"code_inline"}]},{"text":"s and ","type":"text"},{"text":"sources","type":"text","marks":[{"type":"code_inline"}]},{"text":" to zero rows. dbt will still execute the model SQL against the target data warehouse but will avoid expensive reads of input data. This validates dependencies and ensures your models will build properly.","type":"text"}]},{"type":"paragraph","content":[{"text":"Use the ","type":"text"},{"text":"--empty","type":"text","marks":[{"type":"code_inline"}]},{"text":" flag to build an empty version of the models to save warehouse spend.","type":"text"}]},{"type":"code_block","attrs":{"wrap":false,"language":"bash"},"content":[{"text":"\ndbt run --select \"stg_customers top_level_email_domains\" --empty\n","type":"text"}]},{"type":"heading","attrs":{"level":2},"content":[{"text":"Common Mistakes","type":"text"}]},{"type":"table","attrs":{"layout":null},"content":[{"type":"tr","content":[{"type":"th","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Mistake","type":"text"}]}]},{"type":"th","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Fix","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Testing simple SQL using built-in functions","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Only unit test complex logic: regex, date math, window functions, multi-condition case statements","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Mocking all columns in input data","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Only include columns relevant to the test case","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Using ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" format when ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" works","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Prefer ","type":"text"},{"text":"dict","type":"text","marks":[{"type":"code_inline"}]},{"text":" (most readable), fall back to ","type":"text"},{"text":"csv","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"sql","type":"text","marks":[{"type":"code_inline"}]},{"text":" only when needed","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Missing ","type":"text"},{"text":"input","type":"text","marks":[{"type":"code_inline"}]},{"text":" for a ","type":"text"},{"text":"ref","type":"text","marks":[{"type":"code_inline"}]},{"text":" or ","type":"text"},{"text":"source","type":"text","marks":[{"type":"code_inline"}]}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Include all model dependencies to avoid \"node not found\" errors","type":"text"}]}]}]},{"type":"tr","content":[{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Testing Python models or snapshots","type":"text"}]}]},{"type":"td","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"alignment":""},"content":[{"type":"paragraph","content":[{"text":"Unit tests only support SQL models","type":"text"}]}]}]}]}]},"metadata":{"date":"2026-11-02","name":"adding-dbt-unit-test","author":"@skillopedia","source":{"stars":750,"repo_name":"dbt-agent-skills","origin_url":"https://github.com/dbt-labs/dbt-agent-skills/blob/HEAD/skills/dbt/skills/adding-dbt-unit-test/SKILL.md","repo_owner":"dbt-labs","body_sha256":"b185a1ead7471bd128ef8892ffc39a4d31dcfd1053fcb81dc6955f4e7ca2778e","cluster_key":"60d3bc38e1f14d27d7521bf39a77d541d005ea4b6fdeaa60ca346c80fbf66355","clean_bundle":{"format":"clean-skill-bundle-v1","source":"dbt-labs/dbt-agent-skills/skills/dbt/skills/adding-dbt-unit-test/SKILL.md","text_bytes":17183,"source_commit":"b8facde20b7a92c40ce3e6b86b71c4b92aa44040","skills_sh_rank":12055,"attachment_count":13,"text_attachments":13,"attachment_storage":"skill_attachments","binary_attachments":0},"cluster_size":1,"skill_md_path":"skills/dbt/skills/adding-dbt-unit-test/SKILL.md","import_metadata":{"date":"2026-11-02","author":"@skillopedia","version":"v1","category":"testing-qa","category_label":"Testing"},"exact_dupes_collapsed_into_this":0},"version":"v1","category":"testing-qa","metadata":{"author":"dbt-labs"},"import_tag":"skills-sh-top20k-2026-11","description":"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.","user-invocable":false}},"renderedAt":1793661314072}

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…