EqualExperts / dbt-unit-testing

This dbt package contains macros to support unit testing that can be (re)used across dbt projects.
MIT License
413 stars 77 forks source link

Reuse mock-ref in multiple unit tests #172

Closed darian-heede closed 11 months ago

darian-heede commented 12 months ago

Hi there,

first off, I would like to thank you for this great package!

Is there a way of reusing a mock-ref for multiple unit-tests combined using UNION ALL without having to set it in every dbt_unit_testing.test() call?

The following example reuses the mock-ref mock-a in both unit tests:

{% call dbt_unit_testing.test('test-a', 'Test A') %}

  {% call dbt_unit_testing.mock_ref('mock-a') %}
    ...
  {% endcall %}

  {% call dbt_unit_testing.mock_ref('mock-b') %}
    ...
  {% endcall %}

  {% call dbt_unit_testing.expect() %}
    ...
  {% endcall %}

{% endcall %}

UNION ALL

{% call dbt_unit_testing.test('test-b', 'Test B') %}

  {% call dbt_unit_testing.mock_ref('mock-a') %}
    ...
  {% endcall %}

  {% call dbt_unit_testing.mock_ref('mock-c') %}
    ...
  {% endcall %}

  {% call dbt_unit_testing.expect() %}
    ...
  {% endcall %}

{% endcall %}

It would be great not having to repeat the mock-a definition for every unit test.

psousa50 commented 11 months ago

You can define a variable with the value of your mock, like this:

{% set mock_a %}
  {% call dbt_unit_testing.mock_ref('mock-a') %}
    ...
  {% endcall %}
{% endset %}

{% call dbt_unit_testing.test('test-a', 'Test A') %}
  {{ mock_a }}

  {% call dbt_unit_testing.mock_ref('mock-b') %}
    ...
  {% endcall %}

  {% call dbt_unit_testing.expect() %}
    ...
  {% endcall %}

{% endcall %}

UNION ALL

{% call dbt_unit_testing.test('test-b', 'Test B') %}

  {{ mock_a }}

  {% call dbt_unit_testing.mock_ref('mock-c') %}
    ...
  {% endcall %}

  {% call dbt_unit_testing.expect() %}
    ...
  {% endcall %}

{% endcall %}

Hope it helps

darian-heede commented 11 months ago

Hi @psousa50 , this is exactly what I was looking for, thanks!