编写DBT单元测试触发‘actual_or_expected’错误,求排查
DBT单元测试触发'actual_or_expected'错误的原因及解决方法
问题场景
编写了最基础的DBT单元测试示例,执行后报错'actual_or_expected',相关代码及操作如下:
模型文件(test.sql)
{{ config(materialized='table') }} SELECT 1 AS id, 'test' AS value
单元测试配置(test.yml)
unit_tests: - name: testing model: test given: [] # 该简单测试无需输入数据 expect: rows: - {id: 1} # 预期仅一条id=1的记录
执行命令
dbt test --select testing
报错信息
19:02:13 1 of 1 START unit_test test::testing ........................................... [RUN] 19:02:15 Unhandled error while executing 'actual_or_expected' 19:02:15 1 of 1 ERROR test::testing ..................................................... [ERROR in 1.44s] 19:02:15 19:02:15 Finished running 1 unit test in 0 hours 0 minutes and 5.42 seconds (5.42s). 19:02:15 19:02:15 Completed with 1 error, 0 partial successes, and 0 warnings: 19:02:15 19:02:15 'actual_or_expected' 19:02:15 19:02:15 Done. PASS=0 WARN=0 ERROR=1 SKIP=0 TOTAL=1
问题原因
DBT单元测试默认会对比模型输出的所有字段,但测试配置中expect仅指定了id字段,而模型实际输出了id和value两个字段,字段数量不匹配导致触发该错误。
解决方法
有两种可行的修复方式:
方式一:在expect中包含所有字段
将模型输出的所有字段都加入预期结果:
unit_tests: - name: testing model: test given: [] expect: rows: - {id: 1, value: 'test'}
方式二:指定仅对比目标字段
通过columns参数明确指定需要对比的字段,忽略其他字段:
unit_tests: - name: testing model: test given: [] expect: rows: - {id: 1} columns: ['id'] # 仅对比id字段
此外,需确保使用的dbt-core版本不低于1.3.0(单元测试功能从该版本开始正式支持),避免因版本兼容问题引发错误。
内容的提问来源于stack exchange,提问作者user3575145
相关产品推荐
相关产品推荐

