Skip to main content
Glama

test_models

Run dbt tests on data models to validate quality and identify issues. Test modified models, downstream dependencies, or specific selections using dbt selector syntax.

Instructions

Run dbt tests on models and sources.

State-based selection modes (uses dbt state:modified selector):

  • select_state_modified: Test only models modified since last successful run (state:modified)

  • select_state_modified_plus_downstream: Test modified + downstream dependencies (state:modified+) Note: Requires select_state_modified=True

Manual selection (alternative to state-based):

  • select: dbt selector syntax (e.g., "customers", "tag:mart", "test_type:generic")

  • exclude: Exclude specific tests

Args: select: Manual selector for tests/models to test exclude: Exclude selector select_state_modified: Use state:modified selector (changed models only) select_state_modified_plus_downstream: Extend to state:modified+ (changed + downstream) fail_fast: Stop execution on first failure

Returns: Test results with status and failures

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
selectNo
excludeNo
select_state_modifiedNo
select_state_modified_plus_downstreamNo
fail_fastNo

Implementation Reference

  • The core handler function for the 'test_models' tool. Executes 'dbt test' command using the BridgeRunner, supports manual selectors, state-based selection (modified models and downstream), exclude patterns, fail-fast, and progress reporting via MCP context. Parses run_results.json for detailed output.
    async def toolImpl_test_models( self, ctx: Context | None, select: str | None = None, exclude: str | None = None, select_state_modified: bool = False, select_state_modified_plus_downstream: bool = False, fail_fast: bool = False, ) -> dict[str, Any]: """Implementation of test_models tool.""" # Prepare state-based selection (validates and returns selector) selector = await self._prepare_state_based_selection(select_state_modified, select_state_modified_plus_downstream, select) # Early return if state-based requested but no state exists if select_state_modified and not selector: return { "status": "success", "message": "No previous state found - cannot determine modifications", "results": [], "elapsed_time": 0, } # Build command args args = ["test"] # Add selector if we have one (state-based or manual) if selector: args.extend(["-s", selector, "--state", "target/state_last_run"]) elif select: args.extend(["-s", select]) if exclude: args.extend(["--exclude", exclude]) if fail_fast: args.append("--fail-fast") # Execute with progress reporting logger.info(f"Running dbt tests with args: {args}") # Define progress callback if context available async def progress_callback(current: int, total: int, message: str) -> None: if ctx: await ctx.report_progress(progress=current, total=total, message=message) result = await self.runner.invoke(args, progress_callback=progress_callback if ctx else None) # type: ignore if not result.success: error_msg = str(result.exception) if result.exception else "Tests failed" response = { "status": "error", "message": error_msg, "command": " ".join(args), } # Include dbt output for debugging if result.stdout: response["dbt_output"] = result.stdout if result.stderr: response["stderr"] = result.stderr return response # Parse run_results.json for details run_results = self._parse_run_results() return { "status": "success", "command": " ".join(args), "results": run_results.get("results", []), "elapsed_time": run_results.get("elapsed_time"), }
  • FastMCP registration of the 'test_models' tool using @app.tool() decorator. Defines input parameters, comprehensive docstring with usage examples, and delegates execution to the toolImpl_test_models handler.
    async def test_models( ctx: Context, select: str | None = None, exclude: str | None = None, select_state_modified: bool = False, select_state_modified_plus_downstream: bool = False, fail_fast: bool = False, ) -> dict[str, Any]: """Run dbt tests on models and sources. State-based selection modes (uses dbt state:modified selector): - select_state_modified: Test only models modified since last successful run (state:modified) - select_state_modified_plus_downstream: Test modified + downstream dependencies (state:modified+) Note: Requires select_state_modified=True Manual selection (alternative to state-based): - select: dbt selector syntax (e.g., "customers", "tag:mart", "test_type:generic") - exclude: Exclude specific tests Args: select: Manual selector for tests/models to test exclude: Exclude selector select_state_modified: Use state:modified selector (changed models only) select_state_modified_plus_downstream: Extend to state:modified+ (changed + downstream) fail_fast: Stop execution on first failure Returns: Test results with status and failures """ await self._ensure_initialized_with_context(ctx) return await self.toolImpl_test_models(ctx, select, exclude, select_state_modified, select_state_modified_plus_downstream, fail_fast)
  • Shared helper function used by test_models (and run_models, build_models) to validate and construct state-based dbt selectors like 'state:modified' or 'state:modified+'. Handles conflicts with manual select and checks for state existence.
    async def _prepare_state_based_selection( self, select_state_modified: bool, select_state_modified_plus_downstream: bool, select: str | None, ) -> str | None: """Validate and prepare state-based selection. Args: select_state_modified: Use state:modified selector select_state_modified_plus_downstream: Extend to state:modified+ select: Manual selector (conflicts with state-based) Returns: The dbt selector string to use ("state:modified" or "state:modified+"), or None if: - Not using state-based selection - No previous state exists (cannot determine modifications) Raises: ValueError: If validation fails """ # Validate: hierarchical requirement if select_state_modified_plus_downstream and not select_state_modified: raise ValueError("select_state_modified_plus_downstream requires select_state_modified=True") # Validate: can't use both state-based and manual selection if select_state_modified and select: raise ValueError("Cannot use both select_state_modified* flags and select parameter") # If not using state-based selection, return None if not select_state_modified: return None # Check if state exists state_dir = self.project_dir / "target" / "state_last_run" # type: ignore if not state_dir.exists(): # No state - cannot determine modifications return None # Return selector (state exists) return "state:modified+" if select_state_modified_plus_downstream else "state:modified"
  • Shared helper to parse dbt's target/run_results.json after run/test/build commands. Extracts simplified results with unique_id, status, message, execution_time, and failures for consistent tool responses.
    def _parse_run_results(self) -> dict[str, Any]: """Parse target/run_results.json after dbt run/test/build. Returns: Dictionary with results array and metadata """ if not self.project_dir: return {"results": [], "elapsed_time": 0} run_results_path = self.project_dir / "target" / "run_results.json" if not run_results_path.exists(): return {"results": [], "elapsed_time": 0} try: with open(run_results_path) as f: data = json.load(f) # Simplify results for output simplified_results = [] for result in data.get("results", []): simplified_results.append( { "unique_id": result.get("unique_id"), "status": result.get("status"), "message": result.get("message"), "execution_time": result.get("execution_time"), "failures": result.get("failures"), } ) return { "results": simplified_results, "elapsed_time": data.get("elapsed_time", 0), } except Exception as e: logger.warning(f"Failed to parse run_results.json: {e}") return {"results": [], "elapsed_time": 0}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/NiclasOlofsson/dbt-core-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server