An agent that reads a bad metric does not care that the SQL looked reasonable.

Metric logic needs small, explicit tests

dbt Core unit tests validate SQL modeling logic on small static inputs before the model is materialized in production. That matters for AI-ready metrics because agents often consume the output as trusted context.

A data test can tell you whether production rows violate an assertion. A unit test can tell you whether the transformation logic handles the edge case before production data makes the mistake expensive.

Core idea: dbt unit tests turn metric semantics into executable evidence before transformed data becomes AI context.

Unit tests and data tests do different jobs

The dbt unit tests documentation explains that unit tests validate SQL modeling logic on static inputs before materialization. The data tests documentation describes assertions about generated model results.

AI-ready metrics need both. Use unit tests for logic branches, null handling, late-arriving records, currency conversion, excluded statuses, and purpose-specific filters. Use data tests for production integrity. Do not ask one test type to cover both jobs.

Patterns that work

  • Create unit tests for every metric branch that an agent could use in a decision.
  • Mock edge cases such as missing fields, denied source records, stale timestamps, and unusual statuses.
  • Pair unit tests with data tests so logic and production integrity both have evidence.
  • Store test intent in the metric contract, not only in the dbt project.
  • Block AI tool registration until metric unit tests pass in CI.

For adjacent ODI context, read dbt source contracts, dbt semantic contracts, AI-ready runtime tests.

What breaks first

  • A metric has production data tests but no proof that edge-case logic works.
  • Agents retrieve a metric without knowing the test evidence behind it.
  • Unit tests cover happy-path examples while ignored states drive real decisions.
  • The semantic layer says a metric is approved, but the transformation contract has no edge-case tests.

Questions to ask

  • Which AI-facing metrics have dbt unit tests?
  • Which edge cases are mocked before production materialization?
  • Can a metric consumer see the latest test result and intent?
  • Which failed tests block agent access?

Sources to start with

These primary sources anchor the technical claims in this guide.

A metric is not AI-ready until its edge cases have names.