Test your dbt macros
Call a macro with the arguments you choose, say what it should return, and run it on your machine or in CI. A test is a few lines of YAML; the tool turns each case into one of dbt’s own unit tests, so the result, and the diff when it fails, are dbt’s.

Quick start
Section titled “Quick start”1. Install. The tool runs dbt in the same process, so the adapter of your project has to be installed in the same environment, with dbt-core (1.8 or later; the tool brings it):
pip install dbt-duckdb # or dbt-snowflake, dbt-bigquery, ... the one your project usespip install dbt-macro-testPython 3.11 or later; Linux and Windows are run in CI, macOS has not been.
2. Write a test. In a dbt project, with a macro:
-- macros/safe_divide.sql{% macro safe_divide(numerator, denominator) %} {{ numerator }} / nullif({{ denominator }}, 0){% endmacro %}and write a spec for it under macro_test/:
macros: - macro: safe_divide cases: - name: plain_division given: arguments: {numerator: 10, denominator: 4} expect: rows: - {result: 2.5} # a macro's output is the `result` column - name: division_by_zero_is_null given: arguments: {numerator: 10, denominator: 0} expect: rows: - {result: null}3. Run it, from the project:
$ dbt-macro-test run1 of 2 PASS _dmt_safe_divide_safe_divide_division_by_zero_is_null::division_by_zero_is_null [PASS in 0.08s]2 of 2 PASS _dmt_safe_divide_safe_divide_plain_division::plain_division ........ [PASS in 0.02s]Done. PASS=2 WARN=0 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=2
2 passedWhen a case fails, dbt’s own diff is shown, and the exit code is 1 (here the spec expected 2.0):
[ERROR]: in unit_test plain_division (models/_unit_tests.yml)
actual differs from expected:
@@ ,result+++,Decimal('2.5')---,Decimal('2.0')
1 failed, 1 passedBoth outputs are trimmed (dbt’s start-up lines and the timestamps are left out). They are from running exactly this, in a new project with a duckdb profile and the tool installed as above.
The expected values are those of duckdb. On Postgres, 10 / 4 divides two integers as integers and
gives 2, so the first case there would fail; a case can be limited to some adapters with
adapters (see the guide).
Where to go next
Section titled “Where to go next”- Guide: what to write for the macro you have (a value, columns, text, an error, a
ref, a macro that writes), with an example for each. Every example runs in CI. - Spec reference: every key of a spec.
- Design: how it works, and why it is built this way.
- Changelog.
- The README says why this exists, what is carried over from your project, how to use it in CI, and what is not supported.