You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过DBT CLI仅运行特定模型中指定列的所有测试?

如何通过CLI仅针对DBT模型中某一列运行所有测试?

我有一个包含多项测试的DBT模型,希望仅针对其中某一列运行所有测试,请问怎么通过CLI实现?

示例模型

# my_table.yml
models:
  - name: my_table
    columns:
      - name: col_1
        tests:
          - unique
          - accepted_values:
              values:
                - 1
                - 2
      - name: col_2
        tests:
          - not_null
          - accepted_values:
              values:
                - 3
                - 4

当前已支持的选择器

如果要仅运行not_null这类指定测试名的测试,可以用以下命令:

dbt test -s my_table,test_name:not_null

目标功能(暂不直接支持)

我原本希望能用类似这样的命令直接针对col_1运行所有测试,但目前DBT暂不支持这种按列名筛选的方式:

dbt test -s my_table,column_name:col_1

可行替代方案

虽然没有直接的列名选择器,但可以通过以下两种方式实现需求:

方式一:给列的测试添加标签,通过标签筛选

修改模型配置,给目标列的所有测试加上统一标签:

# my_table.yml
models:
  - name: my_table
    columns:
      - name: col_1
        tests:
          - unique:
              tags: [col_1_tests]
          - accepted_values:
              values:
                - 1
                - 2
              tags: [col_1_tests]
      - name: col_2
        tests:
          - not_null
          - accepted_values:
              values:
                - 3
                - 4

然后通过标签运行该列的所有测试:

dbt test -s my_table,tag:col_1_tests

方式二:直接指定该列的所有测试名称

如果列的测试数量不多,可以直接列出所有测试名结合模型筛选:

dbt test -s my_table,test_name:unique,test_name:accepted_values

注意:如果其他列也有同名测试(比如col_2也有accepted_values),这个命令会同时运行这些测试。如果要精准只运行col_1的测试,可以给测试起唯一名称:

# my_table.yml
models:
  - name: my_table
    columns:
      - name: col_1
        tests:
          - unique:
              name: col_1_unique
          - accepted_values:
              name: col_1_accepted_values
              values:
                - 1
                - 2
      - name: col_2
        tests:
          - not_null
          - accepted_values:
              values:
                - 3
                - 4

然后运行:

dbt test -s my_table,test_name:col_1_unique,test_name:col_1_accepted_values

内容的提问来源于stack exchange,提问作者Yaakov Bressler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:55:08