dbt-labs/dbt-core · error

Unit test must have a dependency

Error message

Unit test must have a dependency

What it means

While building the compile-node context for a unit test, the code takes the first `ref` of the test model and unwraps it with `expect`. dbt requires every unit test to declare at least one dependency (a ref to the model under test); if `refs` is empty the expect panics. This is an invariant the unit-test schema is supposed to guarantee.

Solutions

  1. Add the required `model:` (and any `given` refs) to the unit test definition in your schema YAML so the node has at least one ref
  2. Run `dbt parse`/`dbt ls --select <test>` to confirm the unit test node resolves with refs attached
  3. Validate unit test definitions at parse time (fail with a proper error instead of panicking) when `refs` is empty
  4. Upgrade dbt if you hit this with a structurally valid test — it may be a parser bug that drops refs

Example fix

// before (schema.yml)
unit_tests:
  - name: my_test
    given: []
// after
unit_tests:
  - name: my_test
    model: my_model
    given:
      - input: ref('my_model')
        rows: []
Defensive patterns

Strategy: validation

Validate before calling

# dbt schema YAML check: unit test must declare a model/ref
# before running builds, or in Rust before context build:
if model.base().refs.is_empty() {
    return Err(anyhow!("unit test '{}' has no refs; add a model/given ref", model.common().name));
}

Try / catch

// surface as a normal error instead of panicking
let ref_name = model.base().refs.first().map(|r| r.name.clone())
    .ok_or_else(|| anyhow::anyhow!("Unit test must have a dependency"))?;

Prevention

When it happens

Trigger: Running `dbt build`/compile where a unit test node has an empty `refs` list — e.g. a unit test YAML entry with no `given`/model ref, a unit test defined without `model: name:` input, or a partially parsed test node whose refs were not attached.

Common situations: Hand-written unit_tests blocks in schema YAML missing the `model` key; migrating tests from generic `tests:` to `unit_tests:` without adding depends-on refs; bugs in parsing that drop refs from unit test nodes.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dbt-labs/dbt-core@0267ce9170 (2026-09-07). Data as JSON: /api/errors/b06fa6d7dfa0e5d9. Report an issue: GitHub.

Appendix: source

Thrown at crates/dbt-jinja-utils/src/phases/compile/compile_node_context.rs:190

            .as_object()
            .unwrap()
            .downcast_ref::<BTreeMap<String, MinijinjaValue>>()
            .unwrap()
            .clone()
    } else {
        BTreeMap::new()
    };
    let mut ctx = base_context.clone();

    let this_relation = match model.resource_type() {
        NodeType::UnitTest => {
            let ref_name = model
                .base()
                .refs
                .first()
                .cloned()
                .map(|r| r.name)
                .expect("Unit test must have a dependency");
            let (_, this_relation, _, _) = node_resolver.lookup_ref(
                &Some(model.common().package_name.clone()),
                &ref_name,
                &None,
                &None,
            )?;
            this_relation
        }
        NodeType::Model => {
            let ref_name = model.common().name.clone();
            // for repl, we use the just create a relation on spot using model passed in.
            if ref_name == REPL_MODEL_NAME {
                dbt_adapter::relation::RelationObject::new(Arc::from(
                    dbt_adapter::relation::do_create_relation(
                        adapter_type,
                        model.base().database.clone(),
                        model.base().schema.clone(),
                        Some(model.base().alias.clone()),

View on GitHub (pinned to 0267ce9170)