pytest-dbt-core icon indicating copy to clipboard operation
pytest-dbt-core copied to clipboard

[Feature] Unit testing models

Open JCZuurmond opened this issue 3 years ago • 5 comments

Is this your first time opening an issue?

Describe the Feature

Support unit testing models. Most likely we need another fixture to fetch a model, then similar to a macro, we use that fixture in a test.

To consider:

  • models usually contain references to other models or sources: ref and source. How do we handle this? Could we for example mock them? If so, can we share mocks across tests.

Describe alternatives you've considered

Not unit testing models, stick to unit testing macros. You can put the complex SQL - or all SQL - in (modular) macros, then test those. This is not user friendly though.

Who will this benefit?

Those who want to test models, not just macros

Are you interested in contributing this feature?

Yes

Anything else?

See discussion on this topic in the dbt-core repo

JCZuurmond avatar Jun 01 '22 12:06 JCZuurmond

what happened with this one?

megetron3 avatar Apr 25 '24 18:04 megetron3

@megetron3 : I opened this issue to receive feedback from users on this feature. You can add a thumbs up on the first comment to signal your interest.

Currently, the package is lightweight and worked with new dbt releases with minimal/no changes making maintaining the package low time consuming. However, the functionality is also limited.

dbt Labs announced adding unit testing capabilities to dbt-core. I am not up-to-date with the status. I prefer they supporting unit testing over this package supporting unit testing.

This does not stop us from adding unit testing to this package, though, I want to do it with careful consideration. Would you be interested in adding this feature?

JCZuurmond avatar May 01 '24 09:05 JCZuurmond

@JCZuurmond, I appreciate your response. Upon thorough reading, it appears that the new unit-testing feature introduced by dbt-core utilizes YAML syntax. While it may be a viable option for unit testing, it's evident that pytest and Python offer distinct capabilities, which differ from the YAML syntax and the capabilities of dbt.

I believe employing pure Python for testing dbt models could serve as a valuable alternative.

megetron3 avatar May 12 '24 15:05 megetron3

Hi @megetron3, I agree with you on pure Python testing being a valuable alternative. That is the reason that I created this package.

Currently I don't have the time to implement this feature. I would be open to supporting someone who wants to implement this.

Thanks for your interaction on this issue. It motivates to know if users are interested in this functionality

JCZuurmond avatar Jun 02 '24 09:06 JCZuurmond

The following resource might be helpful with implementing this feature: https://github.com/sqlfluff/sqlfluff/blob/main/plugins/sqlfluff-templater-dbt/sqlfluff_templater_dbt/templater.py

JCZuurmond avatar Jun 04 '24 16:06 JCZuurmond