전체 그래프
dbt

Tests

data-engineeringdbttestingdata-quality

상위: dbt

요약

dbt 테스트는 모델의 데이터 품질을 검증하는 SQL 어설션이다. 내장 Generic 테스트 4종과 사용자 정의 Singular 테스트를 지원하며, dbt test 한 줄로 전체 프로젝트의 데이터 품질을 검증할 수 있다.

Generic Tests (내장 4종)

YAML 파일에서 선언적으로 정의한다.

# models/staging/_stg_models.yml
version: 2

models:
  - name: stg_orders
    columns:
      - name: order_id
        tests:
          - unique
          - not_null
      - name: status
        tests:
          - accepted_values:
              values: ['placed', 'shipped', 'completed', 'returned']
      - name: customer_id
        tests:
          - relationships:
              to: ref('stg_customers')
              field: customer_id
테스트검증 내용
unique컬럼 값이 고유한가
not_nullNULL이 없는가
accepted_values허용된 값만 있는가
relationships참조 무결성이 지켜지는가

Singular Tests (커스텀)

SQL 파일로 직접 작성한다. 결과가 0행이면 통과, 1행 이상이면 실패.

-- tests/assert_positive_amount.sql
SELECT
    order_id,
    amount
FROM {{ ref('stg_orders') }}
WHERE amount < 0

비즈니스 규칙이 복잡하거나, Generic 테스트로 표현이 어려운 경우에 사용한다.

테스트 심각도 설정

models:
  - name: stg_orders
    columns:
      - name: order_id
        tests:
          - unique:
              severity: error    # 실패  빌드 중단
          - not_null:
              severity: warn     # 경고만 출력, 빌드 계속
  • error: 테스트 실패 시 파이프라인 중단 (기본값)
  • warn: 경고만 출력하고 계속 진행

dbt-utils 확장 테스트

dbt-utils 패키지를 설치하면 추가 테스트를 사용할 수 있다.

columns:
  - name: email
    tests:
      - dbt_utils.not_empty_string
  - name: created_at
    tests:
      - dbt_utils.expression_is_true:
          expression: "> '2020-01-01'"

주요 확장 테스트:

  • expression_is_true: 임의 SQL 조건 검증
  • not_empty_string: 빈 문자열 체크
  • at_least_one: 최소 1행 존재 확인
  • unique_combination_of_columns: 복합 유니크 검증

실행 방법

dbt test                          # 전체 테스트
dbt test --select stg_orders      # 특정 모델 테스트
dbt test --select tag:critical    # 태그 기반 선택
dbt build                         # run + test 통합 실행

dbt build를 사용하면 모델 실행 직후 해당 모델의 테스트를 바로 실행한다. 테스트 실패 시 다운스트림 모델 실행을 중단한다.

관련 개념