test-driven-development
This Claude Code skill enforces test-driven development discipline by requiring developers to follow the RED-GREEN-REFACTOR cycle: writing a failing test first, implementing minimal code to pass it, then refactoring for quality. Use it when developing new features, fixing bugs, or refactoring existing code to ensure tests genuinely verify behavior rather than being written after implementation, which provides false confidence in code correctness.
git clone --depth 1 https://github.com/majiayu000/spellbook /tmp/test-driven-development && cp -r /tmp/test-driven-development/skills/test-driven-development. ~/.claude/skills/test-driven-developmenttest-driven-development.SKILL.md
# Test-Driven Development (TDD)
> From [obra/superpowers](https://github.com/obra/superpowers)
## Core Principle
Write tests before implementation. Watch them fail. Write minimal code to pass.
**THE IRON LAW: NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST**
## When to Apply TDD
- New features
- Bug fixes
- Refactoring
- Any behavior changes
## The Red-Green-Refactor Cycle
### RED: Write a Failing Test
1. Write ONE minimal test demonstrating desired behavior
2. Use clear, descriptive test names
3. Use real code, avoid unnecessary mocks
4. Test should fail for the RIGHT reason
```
# Verify RED
- Run the test
- Confirm it fails
- Confirm failure is NOT due to syntax errors
- Confirm you're not testing existing functionality
```
### GREEN: Make It Pass
1. Write the SIMPLEST code that makes the test pass
2. Don't add extra features
3. Don't optimize yet
4. Just make it work
```
# Verify GREEN
- Run all tests
- Confirm new test passes
- Confirm no other tests broke
```
### REFACTOR: Clean Up
1. Improve code quality while keeping tests green
2. Remove duplication
3. Improve naming
4. Extract helpers if needed
5. Run tests after each change
## Why This Order Matters
Tests written AFTER implementation:
- Pass immediately (proves nothing)
- You never see them fail
- Cannot verify they test what matters
- Manual testing is NOT a substitute
## Common Rationalizations to REJECT
| Excuse | Reality |
|--------|---------|
| "Too simple to test" | Simple code still breaks |
| "I'll write tests after" | Post-implementation tests pass immediately |
| "Already manually tested" | Manual testing lacks systematic rigor |
| "Deleting work is wasteful" | Unverified code is technical debt |
| "Keep as reference" | You'll adapt it; that's testing-after |
## Red Flags Requiring RESTART
If any of these occur, DELETE the code and start over:
- Writing code before tests
- Tests passing immediately on first run
- Rationalizing "just this once"
- Planning to "test later"
- Keeping pre-written code "as reference"
## Example Workflow
```bash
# 1. RED - Write failing test
def test_user_can_login_with_valid_credentials():
user = create_user(email="test@example.com", password="secret")
result = login(email="test@example.com", password="secret")
assert result.success == True
assert result.user == user
# 2. Run test - MUST FAIL
# 3. GREEN - Implement minimal code
def login(email, password):
user = User.find_by_email(email)
if user and user.check_password(password):
return LoginResult(success=True, user=user)
return LoginResult(success=False, user=None)
# 4. Run test - MUST PASS
# 5. REFACTOR - Clean up if needed
```
## Summary
1. Write test first
2. Watch it fail
3. Write minimal code to pass
4. Refactor while green
5. RepeatAudit AND optimize a CLAUDE.md / AGENTS.md instruction file — score it against the five high-leverage patterns, flag anti-patterns, then apply approved fixes in place. Use when the user says 优化 CLAUDE.md / 优化 AGENTS.md / optimize my agent doc / 帮我改 claudemd, or after an audit when they want the fixes applied (not just reported).
Generate or update repository-specific AGENTS.md instruction files from real repo evidence. Use when asked to create, design, scaffold, split, or improve root or scoped AGENTS.md files for Codex/Claude/agent workflows, especially when a repo needs directory-specific rules, validation commands, generated-file boundaries, or a short agent onboarding router.
REST/GraphQL/gRPC API design best practices. Use when designing APIs, defining contracts, handling versioning. Covers OpenAPI 3.2, GraphQL Federation, gRPC streaming.
Mobile app UI design expert for iOS and Android. Use when designing app interfaces, creating design systems, ensuring accessibility, or following platform guidelines. Covers Material Design 3, Human Interface Guidelines, color theory, typography, and 2025 trends.
End-to-end app feature inventory and user-story testing workflow with a canonical tracker. Use when the user asks to audit every feature, derive expected behavior from code, test user journeys, or explicitly fix and retest documented UX or logistical defects.
Design architecture foundations before implementation. Use when asked to design or refactor architecture, choose Rust/Go crate, package, module, runtime, workflow, or service boundaries, compare mature project architecture, prevent stacked one-off PRs, audit migration debt in existing repos, or produce a spec before coding.
Evidence-driven architecture research for understanding real systems and making technical decisions. Use when doing architecture landscape studies, source-backed system archaeology, build-vs-buy or adopt/adapt/build decisions, open-source and commercial comparisons, revisiting an earlier architecture choice, or handling requests such as 架构调研, 架构选型, 竞品架构, 技术尽调, 同类方案, 开源替代, how is X built, and what should we learn from X. Do not use for small mechanical changes, market-only discovery, or detailed design after the technology direction is already fixed.
Ask Grok or Gemini via opencli (browser-session based, no API cost). Use when the user says "问 grok", "问 gemini", "ask grok", "ask gemini", "用 grok 查", "grok 怎么看", "让 gemini 分析", or wants a second opinion from Grok/Gemini without paying API tokens. Routes to opencli which drives the already-logged-in Chrome session.