How to use unit tests in dbt for macros

20.11.2024 | 2 min Read
Category: Data Engineering | Tags: #dbt, #testing

Have you grown fond of dbt's unit tests, but are wondering how they can be used for macros? Or are you perhaps waiting for good use cases before adopting them? We share a proposed testing framework that enables unit testing of macros in dbt.

We need better test support for macros

So-called unit testing is an important part of software development, but is often difficult to implement for data transformations. With dbt 1.8, support for unit testing models finally arrived, albeit with some limitations.

Macros are intuitively a perfect fit for unit testing, since they themselves are small units of logic that take some input and generate some output. It is also particularly useful for those who maintain packages to be able to easily both test and extend the test setup for the macros they have developed, as the introduction of bugs can potentially affect many end users.

However, there is currently no support for testing macros directly, and one therefore needs to think a bit creatively to find a setup that works well.

Read more about the framework for macro unit testing on Medium

Recently, I arrived at a testing framework that appears to work well for macros in dbt, and I have written more about this in the Medium article below.

In short, the approach involves using an ephemeral model as a source, and then using this in simple models that call a given macro. From there, it is straightforward to make use of the normal unit test setup, and it thus becomes possible to test the logic in your macros so that bugs do not reach production.

Learn more about dbt

I have previously written a straightforward article on how to get started with dbt, so do have a look if you are a complete beginner.

author image

Sindre Grindheim

Sindre is an experienced Data Platform Engineer in architecture and implementation of modern data platforms on Azure and Google Cloud, Databricks and Snowflake.