Skip to Content
Quality Commands/moai coverage

/moai coverage

A command that analyzes test coverage, identifies gaps, and automatically generates missing tests.

One-line summary: /moai coverage is a “Test Gap Hunter”. It precisely measures coverage using language-specific tools and automatically generates missing tests by priority.

Slash Command: Type /moai:coverage in Claude Code to run this command directly. Type /moai alone to see the full list of available subcommands.

Overview

To improve test coverage, you first need to know where it’s lacking. /moai coverage precisely measures coverage using language-specific tools, classifies gaps by risk priority, and automatically generates missing tests.

It generates tests in TDD or DDD style based on the development_mode setting in quality.yaml.

Usage

# Analyze entire project coverage and generate tests > /moai coverage # Analyze with 85% coverage target > /moai coverage --target 85 # Analyze specific file only > /moai coverage --file src/auth/ # Generate report only (no test generation) > /moai coverage --report # Show only uncovered lines > /moai coverage --uncovered # Focus on critical paths only > /moai coverage --critical

Supported Flags

FlagDescriptionExample
--target NCoverage target percentage (default: from quality.yaml test_coverage_target)/moai coverage --target 85
--file PATHAnalyze specific file or directory only/moai coverage --file src/auth/
--reportGenerate report only, do not generate tests/moai coverage --report
--package PKGAnalyze specific package (Go) or module/moai coverage --package pkg/api
--uncoveredShow only uncovered lines/functions/moai coverage --uncovered
--criticalFocus on critical paths (high fan_in, public API)/moai coverage --critical

—target Flag

Specifies the coverage target. If not specified, uses the test_coverage_target value from quality.yaml (default: 85%):

# Target 90% coverage > /moai coverage --target 90

—report Flag

Outputs only the gap analysis report without generating tests:

> /moai coverage --report

Useful when you want to assess the current state.

—critical Flag

Focuses only on P1 (public API, high fan_in) and P2 (business logic, error handling):

> /moai coverage --critical

Execution Process

/moai coverage runs in 5 phases.

Phase 1: Coverage Measurement

Measures precise coverage using language-specific tools:

LanguageCoverage ToolExecution Command
Gogo test + covergo test -coverprofile=coverage.out -covermode=atomic ./...
Pythonpytest-cov or coveragepytest --cov --cov-report=json
TypeScript/JavaScriptvitest or jestvitest run --coverage
Rustcargo-llvm-covcargo llvm-cov --json

Measurement results:

  • Overall coverage percentage
  • Per-file coverage percentages
  • Per-function coverage data (covered/uncovered lines)
  • Branch coverage (where available)

Phase 2: Gap Analysis

Identifies files below the coverage target and classifies by priority:

PriorityConditionDescription
P1 (Critical)Public API functions, fan_in >= 3, @MX:ANCHORHighest priority testing needed
P2 (High)Business logic, error handling pathsHigh business impact code
P3 (Medium)Internal utilities, helper functionsTest needed if target not met
P4 (Low)Generated code, config, trivial getters/settersCan be excluded from target

Phase 3: Test Generation

Generates tests differently based on development_mode in quality.yaml:

ModeTest ApproachDescription
TDDRED-GREEN-REFACTORWrite failing test first, then verify
DDDCharacterization testsCapture existing behavior in tests

Test generation order: P1 → P2 → P3 → Skip P4

For each gap:

  • Table-driven tests (Go) or parameterized tests (Python/TS)
  • Includes edge cases and error scenarios
  • Follows existing test patterns in the codebase
  • Respects file naming conventions (*_test.go, *.test.ts, test_*.py)

Phase 4: Verification

After test generation:

  • Run full test suite to ensure no regressions
  • Re-measure coverage to confirm improvement
  • Compare before/after coverage percentages
  • Verify coverage target is met

Phase 5: Report

## Coverage Report ### Before: 72.5% -> After: 88.3% ### Target: 85% - ACHIEVED ### Tests Generated: 8 - auth_test.go: TestAuthenticateUser (covers P1 gap) - auth_test.go: TestValidateToken (covers P1 gap) - handler_test.go: TestErrorHandling (covers P2 gap) ### Coverage by Package | Package | Before | After | Target | Status | |---------|--------|-------|--------|--------| | pkg/api | 70% | 88% | 85% | PASS | | pkg/core | 45% | 82% | 85% | FAIL | ### Remaining Gaps - pkg/core: 3% remaining (2 functions uncovered)

Agent Delegation Chain

Agent Roles:

AgentRoleKey Tasks
MoAI OrchestratorWorkflow coordination, user interactionReport output, next step guidance
expert-testingMeasurement, analysis, generation, verificationCoverage measurement, gap analysis, test writing, verification

FAQ

Q: Which coverage tools are used?

Standard tools are automatically selected for your project language. Go uses go test -cover, Python uses pytest-cov, TypeScript uses vitest or jest coverage.

Q: What’s the quality of generated tests?

Tests are written in a consistent style by analyzing existing test patterns in the codebase. They include table-driven tests, edge cases, and error scenarios.

Q: What if the coverage target isn’t met?

A list of remaining gaps is presented along with options to generate additional tests. P4 (low priority) gaps are skipped, so 100% may not be achievable.

Q: Can I exclude specific files from coverage measurement?

Yes, using the coverage_exemptions setting in quality.yaml. However, the exclusion ratio is limited to 5% by default.

Last updated on