unit test: macros with this.name

The problem I’m having

we have macros that use this.name to refer to the name of the model. In unit tests of models that use this macro, I get the error:

Compilation Error in unit_test my_test (models\mytest.yml)
'None' has no attribute 'name'

The context of why I’m trying to do this

I want to unit test our macros. In these macros “this.name” is used to get the name of the model. (e.g. as logging information)

What I’ve already tried

I added to the given section of the unit test the following for “this”:

    given:
      - input: this
        format: sql
        rows: |
          select 1

This changes the error message to:

Compilation Error in unit_test my_test (models\mytest.yml)
'str object' has no attribute 'name'

I also tried to override somehow “this.name” but as it is no macro or variable, it didn’t work / I don’t know how.
Since I am sure that others use this.name as well in their code: How do you unit test models / macros with this.name?

I changed “this.name” to “model.name” in the macro I want to test. Now the error is gone.
Can someone explain why this is the case? And what are the differences? (If I got it right, “this” refers to the database model and model refers to the dbt (graph) model.)
It would mean that on cannot use “this.name” in the code in case one wants to use unit tests but has to use “model.name”.

I will do some further testing with other models before I mark this topic as solved. (Edit: Done. So far this is a working alternative for us)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.