DEV Community

Cover image for dbt-macro-test: unit test your dbt macros with a YAML spec
Hirofumi Tsuda
Hirofumi Tsuda

Posted on

dbt-macro-test: unit test your dbt macros with a YAML spec

dbt's native unit tests, available from dbt-core 1.8, are written for models. You give a model its input rows and the rows you expect, and dbt compares them. A macro can't be called with arguments and checked the same way. dbt-labs/dbt#10547 asks for that, and has been open since August 2024.

There are workarounds. The one closest to dbt is a model that only calls the macro, with a native unit test on that model, as this post describes. dbt-unittest offers assertion macros that you run with dbt run-operation. dbt-utils tests its macros in a separate integration_tests project, and pytest-dbt-core tests macros from Python. The model-based approach keeps dbt in charge of the comparison, but the test models end up in your project's manifest, and every macro needs a few files of its own.

dbt-macro-test writes that kind of test from a YAML spec. For each case it generates the throwaway model and the native unit test in a temporary project, then has dbt run them. Nothing is added to your project, and running, comparing and printing the diff are all dbt's.

One spec goes in; dbt-macro-test checks the arguments and generates a throwaway dbt project; dbt runs the unit test in your warehouse through your adapter, compares the rows and prints its own diff

A first test

pip install dbt-duckdb        # or the adapter your project uses
pip install dbt-macro-test
Enter fullscreen mode Exit fullscreen mode
-- macros/safe_divide.sql
{% macro safe_divide(numerator, denominator) %}
    {{ numerator }} / nullif({{ denominator }}, 0)
{% endmacro %}
Enter fullscreen mode Exit fullscreen mode
# 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}
Enter fullscreen mode Exit fullscreen mode
$ dbt-macro-test run
1 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 passed
Enter fullscreen mode Exit fullscreen mode

dbt's start-up lines and the timestamps are left out above. If an expected value is wrong, dbt prints its own diff and the exit code is 1:

actual differs from expected:

@@ ,result
+++,Decimal('2.5')
---,Decimal('2.0')

1 failed, 1 passed
Enter fullscreen mode Exit fullscreen mode

Here is the sample project of the repository, which has 26 cases: they are listed and run, then one expected value is changed and that case fails.

A terminal: dbt-macro-test run --list shows 26 cases, the full run ends with 26 passed, then one expected value is changed and the run of that case fails with dbt's diff and exit code 1

Errors, refs and the rest

A macro that raises an error can be tested too. The message only has to be contained in the error:

  - macro: quantity
    cases:
      - name: stops_with_an_error_when_it_is_negative
        given:
          arguments: {value: -5}
        expect:
          error:
            message: quantity must not be negative, got -5
Enter fullscreen mode Exit fullscreen mode

A spec can also:

  • give the rows that a macro sees when it calls ref('orders') or source(...); no real data is read
  • write the expected rows as csv, as a select or in a file
  • test a macro of a package by its qualified name, such as dbt_utils.slugify
  • override vars, environment variables and other macros for one case
  • compare text that isn't SQL, such as a slug, as text
  • check a macro that inserts, merges or deletes, through temporary relations that the tool makes for the case

The last one is a real write to the target's schema, so use a local or throwaway target. The guide has an example for each of these, and each of them runs in CI.

In CI, and what is missing

dbt-macro-test run exits with 0 when every case passed, 1 when one failed, and 2 when the tests could not be run, for example because of an invalid spec. --select "safe_*::by_zero" runs only some of the cases. The unit tests run in your warehouse, so a CI job needs the adapter, a profile, and the rights of a dbt run on a schema it can write to.

Only duckdb has been tried. Other adapters may work, since dbt does the running, but none has been run. The tool is at 0.2 and the spec format may still change. It needs dbt-core 1.8 or later and Python 3.11 or later, and the adapter has to be installed in the same environment, because dbt runs in the same process. Whether it works on dbt Fusion is not known.

If a macro can't be expressed in a spec, an issue on the repository is welcome.

Top comments (0)