Marketplace

tdd-enforce

Configure TDD enforcement via pre-commit hooks and CI coverage gates. Use when setting up test-first development workflow, adding coverage gates, or enforcing TDD practices.

$ インストール

git clone https://github.com/jmagly/ai-writing-guide /tmp/ai-writing-guide && cp -r /tmp/ai-writing-guide/.factory/skills/tdd-enforce ~/.claude/skills/ai-writing-guide

// tip: Run this command in your terminal to install the skill


name: tdd-enforce description: Configure TDD enforcement via pre-commit hooks and CI coverage gates. Use when setting up test-first development workflow, adding coverage gates, or enforcing TDD practices.

TDD Enforce Skill

Purpose

Configure Test-Driven Development enforcement through pre-commit hooks, CI coverage gates, and automated test execution. Ensures code cannot be committed or merged without adequate test coverage.

Research Foundation

PrincipleSourceReference
TDD MethodologyKent Beck (2002)"Test-Driven Development by Example"
80% CoverageGoogle Testing Blog (2010)Code Coverage Goal
Pre-commit HooksIndustry Best PracticeHusky, pre-commit
CI GatesISTQB CT-TASTest Automation Strategy

When This Skill Applies

  • User asks to "set up TDD" or "enforce test-first"
  • User wants to "add coverage gates" or "block commits without tests"
  • User mentions "pre-commit hooks for tests" or "CI test gates"
  • Project needs test quality enforcement
  • Brownfield project needs TDD adoption

Trigger Phrases

Natural LanguageAction
"Set up TDD enforcement"Configure pre-commit + CI gates
"Add coverage gates"Configure CI coverage thresholds
"Block commits without tests"Set up pre-commit test hooks
"Enforce test-first development"Full TDD setup
"Run tests on commit"Configure pre-commit test execution
"Check if tests exist for new code"Configure test presence validation

Configuration Options

Coverage Thresholds

coverage:
  line: 80        # Google standard: 80% minimum
  branch: 75      # Branch coverage threshold
  function: 90    # Function coverage threshold
  critical_paths: 100  # Auth, payments, validation

Enforcement Levels

LevelPre-commitCI GateDescription
strictBlockFailNo exceptions, 100% enforcement
standardWarn + BlockFailStandard TDD enforcement
gradualWarnWarnFor TDD adoption in brownfield
auditLog onlyReportVisibility without blocking

Implementation Process

1. Detect Project Type

def detect_project():
    """Identify project language and tooling"""
    if exists("package.json"):
        return "javascript"  # Use Husky
    elif exists("pyproject.toml") or exists("setup.py"):
        return "python"      # Use pre-commit
    elif exists("pom.xml") or exists("build.gradle"):
        return "java"        # Use maven/gradle hooks
    # ... etc

2. Install Pre-commit Hooks

JavaScript (Husky + lint-staged):

npm install --save-dev husky lint-staged
npx husky init

.husky/pre-commit:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# Run tests for staged files
npx lint-staged

# Check coverage delta
npm run test:coverage -- --changedSince=HEAD~1

Python (pre-commit):

pip install pre-commit
pre-commit install

.pre-commit-config.yaml:

repos:
  - repo: local
    hooks:
      - id: pytest-check
        name: pytest-check
        entry: pytest --cov=src --cov-fail-under=80
        language: system
        types: [python]
        pass_filenames: false

3. Configure CI Coverage Gates

GitHub Actions:

name: Test Coverage Gate
on: [pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Run tests with coverage
        run: npm test -- --coverage

      - name: Check coverage threshold
        run: |
          COVERAGE=$(jq '.total.lines.pct' coverage/coverage-summary.json)
          if (( $(echo "$COVERAGE < 80" | bc -l) )); then
            echo "Coverage $COVERAGE% is below 80% threshold"
            exit 1
          fi

      - name: Comment coverage on PR
        uses: actions/github-script@v7
        with:
          script: |
            const coverage = require('./coverage/coverage-summary.json');
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `## Coverage Report\n- Lines: ${coverage.total.lines.pct}%\n- Branches: ${coverage.total.branches.pct}%`
            });

4. Configure Test Presence Validation

Check that new/modified files have corresponding tests:

def validate_test_presence(changed_files):
    """Ensure every source file has a test file"""
    missing_tests = []

    for src_file in changed_files:
        if is_source_file(src_file):
            test_file = get_test_file_path(src_file)
            if not exists(test_file):
                missing_tests.append(src_file)

    if missing_tests:
        print("ERROR: Missing tests for:")
        for f in missing_tests:
            print(f"  - {f}")
        return False
    return True

Output Format

When reporting TDD enforcement setup:

## TDD Enforcement Configured

### Pre-commit Hooks
- [x] Husky installed and initialized
- [x] Pre-commit hook: Run affected tests
- [x] Pre-commit hook: Check test presence for new files

### Coverage Gates
- Line coverage threshold: 80%
- Branch coverage threshold: 75%
- Critical path coverage: 100%

### CI Integration
- [x] GitHub Actions workflow created
- [x] Coverage gate: Fail PR if coverage < 80%
- [x] PR comment: Coverage report

### Files Created/Modified
- `.husky/pre-commit`
- `.github/workflows/test-coverage.yml`
- `package.json` (scripts updated)

### Next Steps
1. Run `npm test` to verify baseline coverage
2. Commit and push to test CI gates
3. Review coverage report on first PR

Gradual Adoption for Brownfield

For existing projects without tests:

Phase 1: Audit Mode (Week 1-2)

enforcement: audit
# Log coverage but don't block
# Establish baseline

Phase 2: New Code Only (Week 3-4)

enforcement: gradual
coverage_delta: true  # Only check new code
threshold: 80

Phase 3: Full Enforcement (Week 5+)

enforcement: standard
threshold: 80
block_on_decrease: true

Integration Points

  • Works with /setup-tdd command
  • Integrates with mutation-test skill for quality validation
  • Feeds into /flow-gate-check for phase transitions
  • Reports to Test Architect agent

Related Skills

  • mutation-test - Validate test quality beyond coverage
  • flaky-detect - Identify unreliable tests
  • generate-factory - Create test data infrastructure
  • test-sync - Maintain test-code alignment

Script Reference

tdd_setup.py

Configure TDD enforcement for project:

python scripts/tdd_setup.py --level standard --threshold 80

coverage_gate.py

Check coverage against thresholds:

python scripts/coverage_gate.py --min-line 80 --min-branch 75