Skip to main content
This cookbook shows how to automatically run tests when Droid modifies code, maintain test coverage, and enforce testing requirements.

How it works

Testing hooks can:
  1. Auto-run tests: Execute tests after code changes
  2. Track coverage: Monitor and enforce coverage thresholds
  3. Validate test files: Ensure tests exist for new code
  4. Run specific tests: Execute only relevant test suites
  5. Generate reports: Create test and coverage reports

Prerequisites

Install testing frameworks for your stack:

Basic testing automation

Run tests after code changes

Automatically run tests when Droid edits files. Create .factory/hooks/run-tests.sh:
Add to .factory/hooks.json:

Enforce test coverage

Block changes that decrease test coverage. Create .factory/hooks/check-coverage.sh:
Configure coverage threshold:

Require tests for new files

Ensure new code files have corresponding tests. Create .factory/hooks/require-tests.sh:

Advanced testing automation

Smart test selection

Only run tests affected by changes. Create .factory/hooks/run-affected-tests.sh:

Snapshot testing validation

Detect and validate snapshot updates. Create .factory/hooks/validate-snapshots.sh:

Test performance monitoring

Track test execution time and warn on slow tests. Create .factory/hooks/monitor-test-perf.py:

Test flakiness detector

Detect and report flaky tests: Create .factory/hooks/detect-flaky-tests.sh:

Best practices

1

Run tests asynchronously when possible

Don’t block Droid unnecessarily:
2

Set appropriate timeouts

Allow enough time for test suites:
3

Use test file conventions

Follow standard naming patterns:
4

Make coverage configurable

Different files may need different thresholds:
5

Cache test results

Skip tests if code hasn’t changed:

Troubleshooting

Problem: Test execution blocks workflow Solution: Run only unit tests, skip integration:
Problem: Tests fail in hooks but pass manually Solution: Check environment differences:
Problem: Coverage includes generated files Solution: Configure coverage exclusions:

See also