dbt 单元测试简单说明

dbt 对于测试的支持包含了数据测试以及单元测试, 数据测试可以保证表的质量,但是单元测试可以确保模型的业务一致性

简单说明

  • 当前只支持模型的
  • 只支持当前项目的单元测试
  • 除非声明多版本也会进行测试
  • 单元测试只能在models 目录下 yaml 格式的
  • 如果希望依赖ephemeral 模型需要配置format: sql
  • 目前似乎还没ga,最新release 版本的dbt 是不支持的

参考配置

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: cool@example.com,     email_top_level_domain: example.com}
         - {email: cool@unknown.com,     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: cool@example.com,    is_valid_email_address: true}
        - {email: cool@unknown.com,    is_valid_email_address: false}
        - {email: badgmail.com,        is_valid_email_address: false}
        - {email: missingdot@gmailcom, is_valid_email_address: false}

说明

此功能尽管官方文档是有说明,但是目前使用新版本测试似乎是有问题的(目前v1.8 beta 版本似乎是支持的,但是目前缺少adapter 兼容)
实际上对于dbt 的单元测试社区已经有不少可行的package 都是值得使用的 dbt-unit-testing 就很不错

参考资料

https://docs.getdbt.com/reference/resource-properties/unit-tests
https://github.com/EqualExperts/dbt-unit-testing
https://github.com/yu-iskw/dbt-unittest
https://github.com/AgeOfLearning/dbt-unit-test
https://github.com/dbt-labs/dbt-core/issues/8283
https://docs.getdbt.com/docs/build/unit-tests#when-to-add-a-unit-test-to-your-model

posted on 2024-05-16 06:42  荣锋亮  阅读(12)  评论(0编辑  收藏  举报

导航