unit_tests 속성
unit_tests 속성
유닛 테스트는 프로덕션에서 전체 모델을 구체화하기 전에 작은 정적 입력 세트로 SQL 모델링 로직을 검증해요. 테스트 주도 개발(TDD) 방식을 지원해서 개발자 효율과 코드 신뢰성을 모두 높여 줘요.
출처: 문서
본문
💡 알고 계셨나요? dbt v1.8부터 또는 dbt "v1 Latest" 릴리스 트랙에서 사용할 수 있어요.
유닛 테스트는 프로덕션에서 전체 모델을 구체화하기 전에 작은 정적 입력 세트로 SQL 모델링 로직을 검증해요. 테스트 주도 개발 방식을 지원해서 개발자 효율과 코드 신뢰성을 모두 높여 줘요. 유닛 테스트만 실행하려면 다음 명령어를 사용해요:
dbt test --select test_type:unit
Prerequisites
- 현재 SQL 모델의 유닛 테스트만 지원해요.
- 현재 현재 프로젝트의 모델에만 유닛 테스트를 추가할 수 있어요.
- materialized view 구체화를 사용하는 모델의 유닛 테스트는 현재 지원하지 않아요.
- 재귀 SQL을 사용하는 모델의 유닛 테스트는 현재 지원하지 않아요.
- 인트로스펙티브(introspective) 쿼리를 사용하는 모델의 유닛 테스트는 현재 지원하지 않아요.
- 모델에 여러 버전이 있으면 기본적으로 유닛 테스트는 모델의 모든 버전에서 실행돼요. 자세한 내용은 버전 모델 유닛 테스트를 읽어 주세요.
- 유닛 테스트는 models/ 디렉터리의 YML 파일에 정의해야 해요.
join로직을 유닛 테스트하려면 테이블 이름을 별칭(alias)으로 지정해야 해요.- 컴파일 중 "node not found" 에러를 피하려면 모든 ref 또는 source 모델 참조를 유닛 테스트 구성에
input으로 포함하세요. 유닛 테스트는model-paths(기본적으로models/디렉터리)에서 발견돼요. 그러니 모델 옆의model-paths아래.yml파일에 정의하세요. 데이터 테스트용으로 예약된tests/디렉터리에는 유닛 테스트 YAML을 정의하지 마세요.models/schema.yml
unit_tests:
- name: <test-name> # this is the unique name of the test
model: <model-name>
versions: #optional
include: <list-of-versions-to-include> #optional
exclude: <list-of-versions-to-exclude> #optional
config:
meta: {dictionary}
tags: <string> | [<string>]
enabled: {boolean} # optional. v1.9 or higher. If not configured, defaults to `true`
compute: local | remote # optional. v2.0 or higher. Defaults to `remote`. Requires DBT_ENGINE_EXPERIMENTAL_LOCAL_UNIT_TESTS=true. See Run unit tests locally
given:
- input: <ref_or_source_call> # optional for seeds
format: dict | csv | sql
# either define rows inline or name of fixture
rows: {dictionary} | <string>
fixture: <fixture-name> # SQL or csv
- input: ... # declare additional inputs
expect:
format: dict | csv | sql
# either define rows inline or use the name of a fixture
rows: {dictionary} | <string>
fixture: <fixture-name> # SQL or csv
overrides: # optional: configuration for the dbt execution environment
macros:
is_incremental: true | false
dbt_utils.current_timestamp: <string>
# ... any other Jinja function from https://docs.getdbt.com/reference/dbt-jinja-functions
# ... any other context property
vars: {dictionary}
env_vars: {dictionary}
- name: <test-name> ... # declare additional unit tests
Examples
models/schema.yml
unit_tests:
- name: test_is_valid_email_address # this is the unique name of the test
model: dim_customers # name of the model I'm unit testing
given: # the mock data for your inputs
- input: ref('stg_customers')
rows:
- {email: [email protected], email_top_level_domain: example.com}
- {email: [email protected], email_top_level_domain: unknown.com}
- {email: badgmail.com, email_top_level_domain: gmail.com}
- {email: missingdot@gmailcom, email_top_level_domain: gmail.com}
- input: ref('top_level_email_domains')
rows:
- {tld: example.com}
- {tld: gmail.com}
expect: # the expected output given the inputs above
rows:
- {email: [email protected], is_valid_email_address: true}
- {email: [email protected], is_valid_email_address: false}
- {email: badgmail.com, is_valid_email_address: false}
- {email: missingdot@gmailcom, is_valid_email_address: false}
models/schema.yml
unit_tests:
- name: test_is_valid_email_address # this is the unique name of the test
model: dim_customers # name of the model I'm unit testing
given: # the mock data for your inputs
- input: ref('stg_customers')
rows:
- {email: [email protected], email_top_level_domain: example.com}
- {email: [email protected], email_top_level_domain: unknown.com}
- {email: badgmail.com, email_top_level_domain: gmail.com}
- {email: missingdot@gmailcom, email_top_level_domain: gmail.com}
- input: ref('top_level_email_domains')
format: csv
rows: |
tld
example.com
gmail.com
expect: # the expected output given the inputs above
format: csv
fixture: valid_email_address_fixture_output
models/schema.yml
unit_tests:
- name: test_is_valid_email_address # this is the unique name of the test
model: dim_customers # name of the model I'm unit testing
given: # the mock data for your inputs
- input: ref('stg_customers')
rows:
- {email: [email protected], email_top_level_domain: example.com}
- {email: [email protected], email_top_level_domain: unknown.com}
- {email: badgmail.com, email_top_level_domain: gmail.com}
- {email: missingdot@gmailcom, email_top_level_domain: gmail.com}
- input: ref('top_level_email_domains')
format: sql
rows: |
select 'example.com' as tld union all
select 'gmail.com' as tld
expect: # the expected output given the inputs above
format: sql
fixture: valid_email_address_fixture_output
더 알아보기 (Learn more)
- Unit test input — 테스트 입력 지정
- Unit test overrides — 매크로·변수 오버라이드
- Unit testing versioned models — 버전 모델 유닛 테스트
- Data formats — dict/csv/sql 형식