Back to Hooks

Auto Test Runner

PostToolUse

Automatically runs relevant tests after code edits to catch regressions immediately

testingautomationciquality

Hook Script

#!/bin/bash
# Auto Test Runner Hook
# Runs related tests after file edits

FILE_PATH="$1"

# Skip if the edit is to a config or non-code file
if [[ "$FILE_PATH" == *.json ]] || [[ "$FILE_PATH" == *.md ]] || [[ "$FILE_PATH" == *.yml ]]; then
  exit 0
fi

# Skip if already editing a test file
if [[ "$FILE_PATH" == *"test"* ]] || [[ "$FILE_PATH" == *"spec"* ]]; then
  exit 0
fi

# Determine test runner
if [ -f "package.json" ]; then
  # Node.js project - find related test
  BASE_NAME=$(basename "$FILE_PATH" | sed 's/\.[^.]*$//')
  RELATED_TEST=$(find . -name "${BASE_NAME}.test.*" -o -name "${BASE_NAME}.spec.*" 2>/dev/null | head -1)
  if [ -n "$RELATED_TEST" ]; then
    echo "Running related test: $RELATED_TEST"
    npx jest "$RELATED_TEST" --no-coverage 2>&1 | tail -5
  fi
elif [ -f "pytest.ini" ] || [ -f "pyproject.toml" ]; then
  # Python project
  BASE_NAME=$(basename "$FILE_PATH" .py)
  RELATED_TEST=$(find . -name "test_${BASE_NAME}.py" 2>/dev/null | head -1)
  if [ -n "$RELATED_TEST" ]; then
    echo "Running related test: $RELATED_TEST"
    python -m pytest "$RELATED_TEST" -x -q 2>&1 | tail -5
  fi
fi

exit 0

Settings Configuration

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "./hooks/auto-test.sh"
      }
    ]
  }
}

How to use

  1. Create a hooks directory in your project: mkdir hooks
  2. Save the hook script as hooks/auto-test.sh
  3. Make it executable: chmod +x hooks/auto-test.sh
  4. Add the configuration to your Claude Code settings
  5. Restart Claude Code to apply changes