Tdd

From tae0y/python-project-template · View interactive version

Tdd is an AI agent skill from the GitHub repository tae0y/python-project-template. Test-Driven Development workflow. Use for ALL code changes - features, bug fixes, refactoring. TDD is non-negotiable.

How to use this skill

Skill file

Test-Driven Development

TDD is the fundamental practice. Every line of production code must be written in response to a failing test.

This skill focuses on the TDD workflow/process.


RED-GREEN-REFACTOR Cycle

RED: Write Failing Test First

GREEN: Minimum Code to Pass

REFACTOR: Assess Improvements


TDD Evidence in Commit History

Default Expectation

Commit history should show clear RED → GREEN → REFACTOR progression.

Ideal progression:

commit abc123: [MAINTENANCE] Add failing test for user authentication
commit def456: [NEW FEATURE] Implement user authentication to pass test
commit ghi789: [MAINTENANCE] Extract validation logic for clarity

Rare Exceptions

TDD evidence may not be linearly visible in commits in these cases:

1. Multi-Session Work

2. Context Continuation

3. Refactoring Commits

Documenting Exceptions in PRs

When exception applies, document in PR description:

## TDD Evidence

RED phase: commit c925187 (added failing tests for shopping cart)
GREEN phase: commits 5e0055b, 9a246d0 (implementation + bug fixes)
REFACTOR: commit 11dbd1a (test isolation improvements)

Test Evidence:
✅ 4/4 tests passing (7.7s with 4 workers)

Important: Exception is for EVIDENCE presentation, not TDD practice. TDD process must still be followed - these are cases where commit history doesn't perfectly reflect the process that was actually followed.


Coverage Verification - CRITICAL

NEVER Trust Coverage Claims Without Verification

Always run coverage yourself before approving PRs.

Verification Process

Before approving any PR claiming "100% coverage":

  1. Check out the branch

```bash

git checkout feature-branch

```

  1. Run coverage verification:

```bash

uv run pytest --cov --cov-report=term-missing

```

  1. Verify ALL metrics hit 100%:
  1. Check that tests are behavior-driven (not testing implementation details)

Watch for anti-patterns that create fake coverage (coverage theater).

Reading Coverage Output

Look for the "All files" line in coverage summary:

Name              Stmts   Miss  Cover   Missing
-------------------------------------------------
src/models.py        42      0   100%
src/services.py      38      0   100%
src/utils.py         15      0   100%
-------------------------------------------------
TOTAL               95      0   100%

✅ This is 100% coverage.

Red Flags

Watch for these signs of incomplete coverage:

❌ PR claims "100% coverage" but you haven't verified

❌ Coverage summary shows <100%

TOTAL               95      8    92%

❌ "Missing" column shows line numbers

src/services.py      38      5    87%   45-48, 52

❌ Coverage gaps without exception documentation

When Coverage Drops, Ask

"What business behavior am I not testing?" — not "What line am I missing?"


Development Workflow

Adding a New Feature

  1. Write failing test — describe expected behavior
  2. Run test — confirm it fails (uv run pytest)
  3. Implement minimum — just enough to pass
  4. Run test — confirm it passes
  5. Refactor if valuable — improve code structure
  6. Commit — following project commit convention

Workflow Example

# 1. Write failing test
def test_reject_empty_user_names():
    result = create_user(id="user-123", name="")
    assert result.success is False  # ❌ Test fails (no implementation)

# 2. Implement minimum code
def create_user(id: str, name: str) -> CreateUserResult:
    if not name:
        return CreateUserResult(success=False, error="Name required")
    ...  # ✅ Test passes

# 3. Refactor if needed (extract validation, improve naming)

# 4. Commit
# git add . && git commit -m "[NEW FEATURE] Reject empty user names"

Pull Request Requirements

Before submitting PR:


Refactoring Priority

After green, classify any issues:

| Priority | Action | Examples |

|----------|--------|----------|

| Critical | Fix now | Mutations, knowledge duplication, >3 levels nesting |

| High | This session | Magic numbers, unclear names, >30 line functions |

| Nice | Later | Minor naming, single-use helpers |

| Skip | Don't change | Already clean code |

For detailed refactoring methodology, load the refactoring skill.


Anti-Patterns to Avoid


Summary Checklist

Before marking work complete:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.