Metacenta

Maintainability · rule test-not-beside-model

dbt tests declared away from their models

A Metacenta review checks this under the rule Tests are declared beside the models they test. Everything below applies whether or not you ever commission one.

What this rule checks

This rule flags tests declared in a different directory from the model they test. It compares exact directories, so a schema file in a parent directory counts as elsewhere. When fewer than half of tests sit beside their model, we report the split once, as information.

Why it matters

Someone editing the model cannot see its tests in the same directory. A change then ships with its assertions left behind, or with a test nobody thought to update.

How to fix it

Declare tests beside the models they test. Move the test's YAML block to a schema file in the model's directory. Where we report a split, keep the majority convention and move the few that break it.

Before:

# models/schema.yml
models:
  - name: stg_orders
    columns:
      - name: order_id
        data_tests:
          - unique

After:

# models/staging/_stg_models.yml
models:
  - name: stg_orders
    columns:
      - name: order_id
        data_tests:
          - unique

When it is fine to leave

Keeping every test in central files is a convention dbt permits, and a project where no test sits beside its model is not judged. Singular tests in tests/ belong to no single model, so they are never counted.

What we need to check it

manifest.json alone. We place each test by the YAML file that declares it. Tests the manifest does not attach to a model with a known path are left out, not counted as misplaced.

Published rules it corresponds to

dbt_project_evaluator, rule fct_test_directories. Ours checks a subset of what it flags; theirs is stricter. dbt's rule flags every test not declared in its model's directory. This one first checks that the project uses that convention anywhere. It declines on a project that keeps its tests centrally, which dbt permits and many projects choose.

This means our check corresponds to their rule. It does not mean the publisher reviewed or endorses it.