Agent skill
Red-Green-Refactor
Test-driven development cycle for writing reliable, well-designed code
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/data/red-green-refactor
SKILL.md
Red-Green-Refactor Methodology
You are following the RED-GREEN-REFACTOR cycle for test-driven development. This methodology ensures code is thoroughly tested and well-designed through iterative cycles.
Core Principle
Write a failing test BEFORE writing any production code.
This is not optional. Every new feature, bug fix, or behavior change starts with a test that demonstrates the requirement.
The Cycle
1. RED Phase - Write a Failing Test
Before writing any implementation:
- Understand the requirement - What specific behavior needs to exist?
- Write a test that asserts this behavior
- Run the test - It MUST fail (red)
- Verify the failure - Ensure it fails for the right reason, not due to syntax errors
The test should be:
- Focused on ONE specific behavior
- Named to describe what is being tested
- Using clear assertions with meaningful error messages
Example test structure:
describe('calculateTotal', () => {
it('should apply discount when total exceeds threshold', () => {
// Arrange - set up test data
// Act - call the function
// Assert - verify the result
});
});
2. GREEN Phase - Make the Test Pass
Write the MINIMUM code necessary to make the test pass:
- Focus on passing, not perfection
- Take the simplest path - Even hardcoding is acceptable initially
- Do not add features the test doesn't require
- Run the test - It MUST pass (green)
Guidelines:
- If tempted to write more code than needed, STOP
- The goal is a passing test, not elegant code
- Ugly code is acceptable at this stage
3. REFACTOR Phase - Improve the Code
With a passing test as your safety net:
- Identify code smells - Duplication, long methods, unclear names
- Refactor incrementally - Small changes, run tests after each
- Maintain green - Tests must pass after every change
- Stop when clean - Don't over-engineer
Common refactorings:
- Extract method/function
- Rename for clarity
- Remove duplication
- Simplify conditionals
Workflow Commands
When implementing a feature or fix:
- Start with a test file - Create or update the test file first
- Write ONE failing test - Focus on the smallest testable unit
- Implement minimally - Just enough to pass
- Refactor if needed - Clean up while tests are green
- Repeat - Next test for next behavior
Decision Points
When to Write a New Test
- Adding a new feature or behavior
- Fixing a bug (test the bug first)
- Changing existing behavior
- Edge cases discovered during implementation
When NOT to Write a Test
- Pure refactoring (existing tests cover it)
- Non-functional changes (formatting, comments)
- Third-party library behavior
Verification Checklist
Before considering work complete:
- All new code has corresponding tests
- Tests fail when the feature is removed
- Tests pass consistently (not flaky)
- Code has been refactored for clarity
- No unnecessary code was added
Common Mistakes to Avoid
- Writing tests after code - Defeats the purpose of TDD
- Writing too many tests at once - One test at a time
- Making tests pass with hacks - The test should drive good design
- Skipping the refactor phase - Technical debt accumulates
- Testing implementation details - Test behavior, not internals
Integration with Other Skills
This skill works well with:
- test-patterns: Provides patterns for structuring tests
- anti-patterns: Helps avoid common testing mistakes
- debugging/root-cause-analysis: When tests reveal unexpected failures
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?