유닛 테스트 overrides

유닛 테스트 overrides

유닛 테스트를 구성할 때 특정 유닛 테스트에 대해 매크로, 프로젝트 변수, 환경 변수의 출력을 오버라이드(override)할 수 있어요.

출처: 문서

본문

models/schema.yml

 - name: test_my_model_overrides
    model: my_model
    given:
      - input: ref('my_model_a')
        rows:
          - {id: 1, a: 1}
      - input: ref('my_model_b')
        rows:
          - {id: 1, b: 2}
          - {id: 2, b: 2}
    overrides:
      macros:
        type_numeric: override
        invocation_id: 123
      vars:
        my_test: var_override
      env_vars:
        MY_TEST: env_var_override
    expect:
      rows:
        - {macro_call: override, var_call: var_override, env_var_call: env_var_override, invocation_id: 123}

Macros

유닛 테스트 정의에서 유닛 테스트 대상 모델이 직접 참조하는 매크로의 출력을 오버라이드할 수 있어요. 오버라이드가 적용되는 것은 유닛 테스트 대상 모델 내에서 직접 참조되는 매크로, 변수, 환경 변수에만 한정돼요. 매크로·변수·환경 변수가 간접적으로만 참조된다면(예: 모델이 호출하는 매크로 안에서), 오버라이드가 적용되지 않아요. 유닛 테스트 대상 모델이 다음 매크로를 사용한다면 반드시 오버라이드해야 해요:

  • is_incremental: 인크리멘털 모델을 유닛 테스트한다면 is_incremental을 명시적으로 true 또는 false로 설정해야 해요. 인크리멘털 모델 유닛 테스트 관련 문서는 여기를 참고해 주세요. models/schema.yml
unit_tests:
  - name: my_unit_test
    model: my_incremental_model
    overrides:
      macros:
        # unit test this model in "full refresh" mode
        is_incremental: false 
    ...
  • dbt_utils.star: star 매크로를 사용하는 모델을 유닛 테스트한다면 star를 컬럼 목록으로 명시적으로 설정해야 해요. starfrom 인자로 relation만 받기 때문이에요. 유닛 테스트 목 입력 데이터는 모델 SQL에 직접 주입되어 ref('') 또는 source('') 함수를 대체하므로, 오버라이드하지 않으면 star 매크로가 실패해요. models/schema.yml
unit_tests:
  - name: my_other_unit_test
    model: my_model_that_uses_star
    overrides:
      macros:
        # explicitly set star to relevant list of columns
        dbt_utils.star: col_a,col_b,col_c 
    ...

더 알아보기 (Learn more)