Unit testing your dbt package best practice

I posted an article about best practice of unit testing a dbt package to Medium. According to my research, some dbt packages implements their integration tests. Whereas, there are few packages which contains unit tests. As I published my dbt packaged called yu-iskw/dbt-unittest, there are room to improve our dbt packages by implementing unit tests. I showed an idea about how we can implement unit tests for your dbt package. I hope the idea and my dbt package helps your development.

With the package I published, the subsequent code block is an example of unit testing macro. As you can see, we can use the dbt_unittest.assert_equals macro to check equality of the result of a custom macro and the expected value.

-- integration_tests/macros/test_to_literal.sql
{% macro test_to_literal() %}
  {{ return(adapter.dispatch('test_to_literal', 'integration_tests')(text)) }}
{% endmacro%}
{% macro default__test_to_literal() %}
  {% result = dbt_sample_package.to_literal('test string') %}
  {{ dbt_unittest.assert_equals(result, "'test string'") }}
{% endmacro %}
{% macro postgress__test_to_literal() %}
  {% result = dbt_sample_package.to_literal('test string') %}
  {{ dbt_unittest.assert_equals(result, "E'test string'") }}
{% endmacro %}

This is so cool! I could definitely see a future where unit tests like this are integrated into the .yml configuration for macros

  - name: cents_to_dollars
    description: ...
      - name: ...
      - test_to_cents_to_dollars

Or something. Then from the CLI you could run a command that looks like

dbt unittest

As part of your CI/CD

That is a good ides. I am thinking of something like this. For instance, it might be worthwhile officially providing unit testing macros. I think it would not expose unit testing macros to dbt package users with dbt deps. I am still thinking how is the best to realize what we think.