本文へ移動
cccskills
無料GitHub で公開

codex-test-driven-development

Use before feature, bugfix, or refactor implementation when behavior can be tested; enforces Red-Green-Refactor discipline.

インストール方法を見る

含まれるファイル(3)

  • SKILL.md10.5 KB
  • agents/openai.yaml293 B
  • references/testing-anti-patterns.md5.9 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

TL;DR

Write test first → watch it fail → write minimal code to pass → watch it pass → refactor. Delete code written before tests. No exceptions. Integrate with $gate for automated verification. Use $tdd or $red-green to activate.

Test-Driven Development (TDD)

Activation

  1. Activate on $codex-test-driven-development, $tdd, or $red-green.
  2. Activate when implementing any feature or bugfix, before writing implementation code.
  3. Activate when codex-workflow-autopilot routes to build or fix workflow.
  4. Activate when codex-intent-context-analyzer detects implementation intent.
  5. Always active as a behavioral constraint during implementation phases.

The Iron Law

NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST

Write code before the test? Delete it. Start over.

No exceptions:

  • Don't keep it as "reference"
  • Don't "adapt" it while writing tests
  • Don't look at it
  • Delete means delete

Implement fresh from tests. Period.

Violating the letter of the rules is violating the spirit of the rules.

When to Use

Always:

  • New features
  • Bug fixes
  • Refactoring
  • Behavior changes

Exceptions (ask the human):

  • Throwaway prototypes
  • Generated code
  • Configuration files

Thinking "skip TDD just this once"? Stop. That's rationalization.

Red-Green-Refactor Cycle

Task type → Implementation needed?
    |- Yes → Write failing test (RED)
    |   |- Verify it fails correctly
    |   |- Write minimal code (GREEN)
    |   |- Verify it passes + all tests pass
    |   |- Refactor (keep green)
    |   `- Next test → repeat
    |
    `- No → skip TDD

RED — Write Failing Test

Write one minimal test showing what should happen.

Good — Python:

def test_retries_failed_operations_three_times():
    attempts = 0

    def operation():
        nonlocal attempts
        attempts += 1
        if attempts < 3:
            raise RuntimeError("fail")
        return "success"

    result = retry_operation(operation)

    assert result == "success"
    assert attempts == 3

Clear name, tests real behavior, one thing.

Good — TypeScript:

test('retries failed operations 3 times', async () => {
  let attempts = 0;
  const operation = () => {
    attempts++;
    if (attempts < 3) throw new Error('fail');
    return 'success';
  };

  const result = await retryOperation(operation);

  expect(result).toBe('success');
  expect(attempts).toBe(3);
});

Bad:

test('retry works', async () => {
  const mock = jest.fn()
    .mockRejectedValueOnce(new Error())
    .mockResolvedValueOnce('success');
  await retryOperation(mock);
  expect(mock).toHaveBeenCalledTimes(2);
});

Vague name, tests mock not code.

Requirements:

  • One behavior per test
  • Clear descriptive name
  • Real code (no mocks unless unavoidable)

Verify RED — Watch It Fail

MANDATORY. Never skip.

# Python
python -m pytest tests/path/test_module.py::test_name -v

# JavaScript/TypeScript
npm test -- --grep "test name"

# Go
go test -run TestName -v ./...

Confirm:

  • Test fails (not errors)
  • Failure message is expected
  • Fails because feature missing (not typos)

Test passes immediately? You're testing existing behavior. Fix test.

Test errors? Fix error, re-run until it fails correctly.

GREEN — Minimal Code

Write simplest code to pass the test.

Good:

async def retry_operation(fn, max_retries=3):
    for i in range(max_retries):
        try:
            return fn()
        except Exception:
            if i == max_retries - 1:
                raise

Just enough to pass.

Bad:

async def retry_operation(
    fn,
    max_retries=3,
    backoff="exponential",
    on_retry=None,
    timeout=30,
    jitter=True,
):
    # YAGNI — over-engineered

Don't add features, refactor other code, or "improve" beyond the test.

Verify GREEN — Watch It Pass

MANDATORY.

# Run specific test
python -m pytest tests/path/test_module.py::test_name -v

# Run ALL tests to check for regressions
python -m pytest tests/ -q

Confirm:

  • Test passes
  • Other tests still pass
  • Output pristine (no errors, warnings)

Test fails? Fix code, not test.

Other tests fail? Fix now.

REFACTOR — Clean Up

After green only:

  • Remove duplication
  • Improve names
  • Extract helpers

Keep tests green. Don't add behavior.

Repeat

Next failing test for next feature.

Good Tests

QualityGoodBad
MinimalOne thing. "and" in name? Split it.test_validates_email_and_domain_and_whitespace
ClearName describes behaviortest_1, test_it_works
Shows intentDemonstrates desired APIObscures what code should do
Real codeTests actual implementationTests mock behavior

Why Order Matters

"I'll write tests after to verify it works"

Tests written after code pass immediately. Passing immediately proves nothing:

  • Might test wrong thing
  • Might test implementation, not behavior
  • Might miss edge cases you forgot
  • You never saw it catch the bug

Test-first forces you to see the test fail, proving it actually tests something.

"I already manually tested all the edge cases"

Manual testing is ad-hoc. You think you tested everything but:

  • No record of what you tested
  • Can't re-run when code changes
  • Easy to forget cases under pressure
  • "It worked when I tried it" ≠ comprehensive

Automated tests are systematic. They run the same way every time.

"Deleting X hours of work is wasteful"

Sunk cost fallacy. The time is already gone. Your choice now:

  • Delete and rewrite with TDD (X more hours, high confidence)
  • Keep it and add tests after (30 min, low confidence, likely bugs)

The "waste" is keeping code you can't trust.

"TDD is dogmatic, being pragmatic means adapting"

TDD IS pragmatic:

  • Finds bugs before commit (faster than debugging after)
  • Prevents regressions (tests catch breaks immediately)
  • Documents behavior (tests show how to use code)
  • Enables refactoring (change freely, tests catch breaks)

"Pragmatic" shortcuts = debugging in production = slower.

Common Rationalizations

ExcuseReality
"Too simple to test"Simple code breaks. Test takes 30 seconds.
"I'll test after"Tests passing immediately prove nothing.
"Tests after achieve same goals"Tests-after = "what does this do?" Tests-first = "what should this do?"
"Already manually tested"Ad-hoc ≠ systematic. No record, can't re-run.
"Deleting X hours is wasteful"Sunk cost fallacy. Keeping unverified code is technical debt.
"Keep as reference, write tests first"You'll adapt it. That's testing after. Delete means delete.
"Need to explore first"Fine. Throw away exploration, start with TDD.
"Test hard = design unclear"Listen to test. Hard to test = hard to use.
"TDD will slow me down"TDD faster than debugging. Pragmatic = test-first.
"Manual test faster"Manual doesn't prove edge cases. You'll re-test every change.
"Existing code has no tests"You're improving it. Add tests for existing code.
"This is different because..."It's not. Start with TDD.

Red Flags — STOP and Start Over

  • Code before test
  • Test after implementation
  • Test passes immediately on first run
  • Can't explain why test failed
  • Tests added "later"
  • Rationalizing "just this once"
  • "I already manually tested it"
  • "Tests after achieve the same purpose"
  • "It's about spirit not ritual"
  • "Keep as reference" or "adapt existing code"
  • "Already spent X hours, deleting is wasteful"
  • "TDD is dogmatic, I'm being pragmatic"

All of these mean: Delete code. Start over with TDD.

Example: Bug Fix

Bug: Empty email accepted

RED

def test_rejects_empty_email():
    result = submit_form({"email": ""})
    assert result["error"] == "Email required"

Verify RED

$ python -m pytest tests/test_form.py::test_rejects_empty_email -v
FAIL: AssertionError: expected 'Email required', got None

GREEN

def submit_form(data):
    if not data.get("email", "").strip():
        return {"error": "Email required"}
    # ...

Verify GREEN

$ python -m pytest tests/test_form.py -v
PASS (all tests)

REFACTOR Extract validation for multiple fields if needed.

Verification Checklist

Before marking work complete:

  • Every new function/method has a test
  • Watched each test fail before implementing
  • Each test failed for expected reason (feature missing, not typo)
  • Wrote minimal code to pass each test
  • All tests pass
  • Output pristine (no errors, warnings)
  • Tests use real code (mocks only if unavoidable)
  • Edge cases and errors covered

Can't check all boxes? You skipped TDD. Start over.

When Stuck

ProblemSolution
Don't know how to testWrite wished-for API. Write assertion first. Ask the human.
Test too complicatedDesign too complicated. Simplify interface.
Must mock everythingCode too coupled. Use dependency injection.
Test setup hugeExtract helpers. Still complex? Simplify design.

Debugging Integration

Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression.

Never fix bugs without a test.

CodexAI Integration

Gate Integration

After TDD cycle, run quality gate:

# Quick check
python codex-execution-quality-gate/scripts/auto_gate.py --mode quick --project-root <repo>

# Smart test selection for changed files
python codex-execution-quality-gate/scripts/smart_test_selector.py --project-root <repo>

Workflow Integration

Workflow PhaseTDD Requirement
$plan task breakdownEach task must include test-first steps
$create implementationRED-GREEN-REFACTOR per feature unit
$debug bug fixFailing reproduction test BEFORE fix attempt
$review code reviewVerify TDD compliance in changeset

Testing Anti-Patterns

When adding mocks or test utilities, see references/testing-anti-patterns.md to avoid common pitfalls.

Reference Files

  • references/testing-anti-patterns.md: common testing mistakes and how to avoid them.

Final Rule

Production code → test exists and failed first
Otherwise → not TDD

No exceptions without the human's explicit permission.

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work — guides completion of development work by presenting structured options for merge, PR, or cleanup

日本語の概要は準備中です。原文の説明を表示しています。

Bang-isme/CodexAI---Skills102026年10月5日 更新

Use when a project has 50+ files, missing context, or explicit $codex-genome request; generates multi-layer project context to reduce hallucination.

日本語の概要は準備中です。原文の説明を表示しています。

Bang-isme/CodexAI---Skills102026年10月5日 更新

Use for DESIGN.md visual identity contracts; author, scaffold, lint, diff, and export design contracts with the upstream spec and CLI.

日本語の概要は準備中です。原文の説明を表示しています。

Bang-isme/CodexAI---Skills102026年10月5日 更新

Use when the task involves reading, creating, or editing `.docx` documents, especially when formatting or layout fidelity matters; prefer `python-docx` plus the bundled `scripts/render_docx.py` for visual checks.

日本語の概要は準備中です。原文の説明を表示しています。

Bang-isme/CodexAI---Skills102026年10月5日 更新

Use when code changes may require documentation updates; maps git diff to candidate docs without auto-editing documentation.

日本語の概要は準備中です。原文の説明を表示しています。

Bang-isme/CodexAI---Skills102026年10月5日 更新

Use for documents, reports, memos, guides, academic papers, or formal text that needs clear structure, reliable tone, formatting, and diagrams.

日本語の概要は準備中です。原文の説明を表示しています。

Bang-isme/CodexAI---Skills102026年10月5日 更新

Bang-isme のスキルをすべて見る

このスキルの問題を報告する