How to Run Tagged dbt Tests in Dagster

Last updated: October 20, 2025

When running dbt tests in Dagster, it's important to understand that tests are not assets - they are operations that run against assets. Using build_dbt_asset_selection with tags will filter assets with that tag, not the tests.

To run specific tagged tests (including source tests), use the DbtCliResource directly instead of asset selection. Here's how:

  from dagster_dbt import DbtCliResource

  @op(required_resource_keys={"dbt"})
  def run_tagged_tests(context):
      """Run only tests with specific tags using dbt CLI directly"""
      dbt = context.resources.dbt

      # Use dbt's test selection syntax
      result = dbt.cli([
          "test",
          "--select", "tag:hourly_tests"  # This will select TESTS with the tag
      ], context=context)

      return result

  hourly_test_schedule = ScheduleDefinition(
      name="hourly_tests_schedule",
      cron_schedule="0 * * * *",
      job=define_job(
          name="hourly_tests",
          op_defs=[run_tagged_tests],
          resource_defs={"dbt": DbtCliResource(project_dir="path/to/dbt")}
      ),
      execution_timezone="UTC",
  )

The standard asset-based approach using build_dbt_asset_selection with tags will not work for running tagged tests on sources, as it filters the assets (models/sources) that have the tag, not the tests themselves.