带标签的特定模型运行DBT通用测试失败排查求助
问题描述
给BigQuery的dbt模型通过YAML配置附加模型级通用测试时,测试执行依赖模型标签(执行命令为dbt test --select tag:foo),但运行后出现编译错误,提示test_foo_thing未定义。已尝试将测试文件放置在tests/及tests/my_model/目录,均无法解决问题。
报错信息
11:31:06 1 of 1 START test foo_thing_my_model_ ............... [RUN] 11:31:06 1 of 1 ERROR foo_thing_my_model_ .................... [ERROR in 0.02s] 11:31:06 11:31:06 Finished running 1 test in 0 hours 0 minutes and 0.67 seconds (0.67s). 11:31:07 11:31:07 Completed with 1 error and 0 warnings: 11:31:07 11:31:07 Compilation Error in test foo_thing_my_model_ (models/<path-to>/my_model.yml) 'test_foo_thing' is undefined. This can happen when calling a macro that does not exist. Check for typos and/or install package dependencies with "dbt deps".
模型YAML配置
version: 2 models: - name: my_model config: tags: - aaa - bbb - foo # 用于筛选测试执行的标签 tests: - foo_thing
测试文件内容
{% test foo_thing(model) %} ... -- 复杂SQL查询,返回空行则测试通过 ... {% endtest %}
排查与解决方法
核心问题:自定义测试宏的存放路径错误
dbt中,用{% test %}定义的可复用测试宏,必须放在项目根目录下的macros/目录中,而非tests/目录。tests/目录仅用于存放独立的SQL测试文件(无需宏定义,直接写断言SQL)。具体解决步骤
- 在项目根目录创建
macros/目录(若不存在),将测试文件foo_thing.sql移动到macros/下,最终路径为macros/foo_thing.sql - 检查测试宏名称与YAML中的引用完全一致,dbt对宏名称大小写敏感,避免拼写错误
- 清理dbt缓存并重新编译:执行命令
dbt clean && dbt compile,确保宏被正确加载 - 重新执行测试命令:
dbt test --select tag:foo
- 在项目根目录创建
内容的提问来源于stack exchange,提问作者TPPZ
相关产品推荐
相关产品推荐

