Red-Green-Refactor
npx machina-cli add skill rohitg00/skillkit/red-green-refactor --openclawRed-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
Source
git clone https://github.com/rohitg00/skillkit/blob/main/packages/core/src/methodology/packs/testing/red-green-refactor/SKILL.mdView on GitHub Overview
Red-Green-Refactor is a test-driven development cycle that starts with a failing test, makes it pass with the minimal needed code, and then refactors for a cleaner design. This approach ensures changes are driven by tests and kept safe by a green suite. It emphasizes focused, test-first work to improve reliability and maintainability.
How This Skill Works
The cycle begins with RED: write a failing test for a single behavior. Then GREEN: implement the minimal code to pass the test. Finally REFACTOR: clean up while keeping the tests green, addressing smells like duplication or unclear names. Repeatedly apply these steps, using tests as a safety net and guiding design decisions.
When to Use It
- Adding a new feature or behavior
- Fixing a bug and validating the fix with a test before code changes
- Changing existing behavior and wanting regression protection
- Exploring edge cases discovered during implementation
- Refactoring to improve clarity and reduce duplication while maintaining green tests
Quick Start
- Step 1: Start with a test file and write ONE failing test that captures the new behavior.
- Step 2: Implement the minimal code required to make the test pass (green).
- Step 3: Refactor for clarity and maintain green tests; repeat for the next behavior.
Best Practices
- Write a failing test BEFORE writing production code
- Keep each test focused on ONE specific behavior
- Run tests frequently; ensure the new test fails (red) and then passes (green)
- Implement the MINIMUM code necessary to pass the test
- Refactor incrementally, maintaining green tests after every change
Example Use Cases
- Example 1: describe('calculateTotal', () => { it('should apply discount when total exceeds threshold', () => { /* Arrange/Act/Assert */ }); });
- Example 2: Start with a test file for a new feature and write ONE failing test to drive the design.
- Example 3: Implement the smallest code path to make the test pass, even if it means a simple or hardcoded solution.
- Example 4: After green, perform a refactor—extract a method, rename for clarity, and remove duplication—while running tests to ensure green remains.
- Example 5: Use the verification checklist to confirm all new code has tests, tests pass consistently, and the code is refactored for clarity.