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   荣锋亮  阅读(53)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-05-16 ulid 一个比uuid 更好的id 生成方案
2023-05-16 golang vrrp + ipvs 实现简单的服务ha
2023-05-16 c-for-go cgo 绑定自动生成工具
2023-05-16 cube.js 数据模型更新
2023-05-16 hasura graphql-engine 支持mysql&oracle了
2023-05-16 containerlab 简单试用
2023-05-16 containerlab 基于容器的网络实验室工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示