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 %}