dbt-macro-test
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.
- A macro can be tested like a function. Arguments in, rows out. dbt's native unit tests cover models only (dbt-labs/dbt#10547), so there was no way to call a macro with given inputs and assert on what it gives.
- dbt does the running. Your project's profile, adapter,
vars,dispatchand packages are used; the comparison and the output of a failure are dbt's own, not a second implementation. - More than rows. The expected rows written out, as csv or SQL, or in a file; the error or the
warning that a macro raises;
overridesof macros,varsand environment variables for a case. - Made for CI. Exit code 0, 1 or 2,
--select, Linux and Windows, dbt-core 1.8 and later, and an example for GitHub Actions below.
Status: alpha. The interface may still change. It has been tried on duckdb only (see What is not supported).
This project is independent and is not affiliated with or endorsed by dbt Labs. "dbt" is a trademark of dbt Labs, Inc.
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 uses
pip install dbt-macro-test
Python 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/:
# macro_test/safe_divide.yml
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 run
1 of 2 PASS _mt_safe_divide_safe_divide_division_by_zero_is_null::division_by_zero_is_null [PASS in 0.08s]
2 of 2 PASS _mt_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 passed
When 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 passed
Both 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.
How it works
Each case of a spec becomes a throwaway model that calls the macro, and a native dbt unit test of
that model, in a temporary directory; dbt runs them. Execution, comparison and the output of a
failure are dbt's own. What the tool adds is the spec format, the generation of the project, a
check of the arguments before dbt runs, and the report. If dbt ships a way to test macros of its
own, the specs still say what a macro should do, which is the part worth keeping.
docs/design.md says
how it works and why;
docs/history/design-log.md
has the evidence for the gap and the prior art.
What a spec can say
Arguments are given by name. A case can expect rows (written out, as csv or SQL, or in a file),
an error, or a warning; it can return a whole select, several columns, or a value of a
composite type; and it can override macros, vars and environment variables for that case. All
of it is in the spec reference.
The command line
dbt-macro-test run # everything under ./macro_test
dbt-macro-test run macro_test/safe_divide.yml # one file, or a directory
dbt-macro-test run --target ci # a target of the profile
dbt-macro-test run --project-dir ../shop # from anywhere
dbt-macro-test run --select safe_divide # every case of one macro
dbt-macro-test run --select "safe_*::by_zero" # macro::case; * and ? stand for any characters
dbt-macro-test run --list --select greeting # show what would run, run nothing
| Option | |
|---|---|
PATH |
A spec file or a directory. Default: macro_test/ of the project. |
--project-dir |
The dbt project. Default: the nearest directory at or above PATH (or the working directory) with a dbt_project.yml. |
--profiles-dir |
Where profiles.yml is. Default: $DBT_PROFILES_DIR, then the project's own profiles.yml, then ~/.dbt (the order dbt uses). |
--target |
The target of the profile. |
--select |
Run only the cases that match; can be given more than once, the patterns are ORed. |
--list |
Print the cases that would run, as macro::case, and stop. |
--keep-generated |
Keep the generated dbt project (a temporary directory) and print where it is. |
--verbose |
Say what is carried over from the project, and which profiles.yml was used. |
The exit code is 0 when every case passed, 1 when a case failed or errored, and 2 when
the tests could not be run at all: an invalid spec, a wrong argument, no dbt project, dbt itself
failing, or nothing matching --select. The last line says what happened, such as
1 failed, 8 passed.
What is carried over from the project
The project's vars, dispatch and macro-paths are carried over, so a macro sees what it
sees in the real project, and its packages (packages.yml) are available to it. Its
on-run-start and on-run-end hooks are not run. If a case cannot be run at all (a SQL error,
for instance), the SQL generated for it is printed after dbt's own output.
Packages that are already installed (in dbt_packages/, or where packages-install-path
says) are used as they are and dbt deps is not run, so nothing in them is changed. Otherwise
packages.yml is copied (a relative local: path made absolute) and dbt deps runs in the
temporary directory.
Using it in CI
The tool needs what dbt test needs: the adapter installed, a profiles.yml, and a connection
to the warehouse in it. Unit tests run against the warehouse: dbt's unit-test
materialization creates a temporary relation for each case, to read the types of the columns, and
drops it (unit.sql in dbt-core's global project, which an adapter may override; dbt-duckdb does
not). What exactly is sent is the adapter's create_table_as, and only duckdb's has been seen
(create temporary table, in dbt's log), so plan for the credentials to need the rights of a
dbt run on a schema you can write to.
# .github/workflows/macro-test.yml
name: macro tests
on: [pull_request]
jobs:
macro-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install dbt-duckdb dbt-macro-test
- run: dbt deps # leave this out if the project has no packages.yml
- run: dbt-macro-test run
env:
DBT_PROFILES_DIR: ci # the directory of a profiles.yml that CI can use
profiles.yml: give it with--profiles-dirorDBT_PROFILES_DIR. Keep the credentials out of it and read them from the environment with{{ env_var('SNOWFLAKE_PASSWORD') }}, and set them from the secrets of the repository.- Secrets are not available to pull requests from forks, so a job that needs the warehouse
does not work for them. Two ways round it: run the macro tests only for branches of the
repository (
on: push, orif: github.event.pull_request.head.repo.full_name == github.repositoryon the job), or use a local adapter for CI, as follows. - A local adapter, such as duckdb, as the CI target needs no credentials and no warehouse
(add a
citarget of typeduckdbtoprofiles.yml, and--target ci). The price: dbt picks the implementation of a macro by the type of the adapter (adapter.dispatch), so a run on duckdb tests theduckdb__ordefault__implementation, not the one for your warehouse, and SQL that only your warehouse understands will not run on it. It is a good fit for macros that are plain SQL or Jinja, and not for what is warehouse-specific. dbt deps: it needs the network (the hub, or git), and it is the slowest step of a typical job; cachedbt_packages/on a hash ofpackages.yml(orpackage-lock.yml) if it matters. Whendbt_packages/is there, the tool uses it as it is and does not rundbt deps. dbt checks only that the number of installed packages matchespackages.yml, so a package whose version was changed is not noticed: rundbt depsbeforedbt-macro-test run, as the example does.
What is not supported
Each of these has an issue.
- A macro that needs a
refor asourceto exist, such asdbt_utils.starorunion_relations(#9). - A macro that returns text which is not SQL, such as
dbt_utils.slugify(#29); and asserting on a macro's return value without a database (#11). - A macro that runs DML, such as
merge(#30). - Generic tests (#13), and asserting on which macros were called (#16).
- dbt Fusion (#23); the tool runs dbt-core in-process.
- Adapters other than duckdb: the tool leaves the work to dbt, so others may well work, but none has been run (#14).
The tool runs dbt through dbtRunner, dbt's programmatic entry point. It also uses three
things that are not a documented interface: the project file, read with
dbt.config.project.load_raw_project; the function of dbt_common that finds the macros in a
file; and the fields of the results and events of a run. A new dbt-core may change them; the suite
is run on dbt-core 1.8 and on the newest one in CI to find where they differ. See
docs/design.md.
Development
Needs uv.
uv sync
uv run pytest
The tests run on up to four workers (pytest-xdist, set in pyproject.toml), and the
end-to-end ones, which run dbt, take most of the time. To run one test, or to see a
failure without the workers, use uv run pytest -n 0 path/to/test.py::test_name. CI runs the
suite on Python 3.11 to 3.14, on dbt-core 1.8, and on Windows; dbt-core 1.9, 1.10 and 1.11 were
each run once by hand.
License
Metadata
Release files for dbt-macro-test 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dbt_macro_test-0.1.0.tar.gz | 331.2 kB | With provenance | Details for dbt_macro_test-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dbt_macro_test-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for dbt_macro_test-0.1.0-py3-none-any.whl |
Total release size: 360.9 kB
Release files / dbt_macro_test-0.1.0.tar.gz
| Download URL | dbt_macro_test-0.1.0.tar.gz |
|---|---|
| Size | 331.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5ea68f194f2a6491125b8560e1afed0cfc16f3ab2a76a35d1a7c74e72ae6c36a
|
|
BLAKE2b-256 checksum How to use checksums |
4620896369916b86f3a8b18e180fc78a0218b1c3cb524638f6319b8c97be52fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / dbt_macro_test-0.1.0-py3-none-any.whl
| Download URL | dbt_macro_test-0.1.0-py3-none-any.whl |
|---|---|
| Size | 29.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae9f4f9dd4f169bc60a86adab9e81d5c86c97ed08dcb39b2c814f00377d6216a
|
|
BLAKE2b-256 checksum How to use checksums |
1efc38a5a807f6bdba547f94a0ae96d0daacdd28d1d04c0500e9ca13c167255c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for dbt-macro-test 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for dbt_macro_test-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.