一键导入
test-driven-development
Use when implementing any assigned scenario or bugfix, before writing implementation code
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when implementing any assigned scenario or bugfix, before writing implementation code
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | test-driven-development |
| description | Use when implementing any assigned scenario or bugfix, before writing implementation code |
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
shopsystem note (experimental). This is an adapted variant of the external test-driven-development skill, tuned for a BC Implementer running autonomously inside a container. There is no human in the BC's loop; where the original skill defers to a "human partner," the BC instead emits a
clarifymessage to the lead and awaits the lead's decision. This draft is meant to be learned from, not treated as canon.
A BC Implementer works against two nested loops, and TDD lives in the inner one.
Outer loop — the assigned Gherkin scenario(s). The scenario(s) the lead
dispatched are the acceptance specification: they pin what behavior is
required and they are what work_done proves. The scenario is silent on
internal decomposition — it says nothing about which functions, classes, or
modules you write. The outer loop is satisfied when the assigned scenario(s)
pass against your implementation.
Inner loop — RED-GREEN-REFACTOR. For each behavior you must build to make the assigned scenario(s) pass, you run the TDD cycle below. This is where the BC does its own internal design: the scenario tells you the destination, and the inner loop is how you get there, one failing test at a time. This skill fills the gap the scenario deliberately leaves open.
When a piece of assigned work decomposes into multiple behaviors, track those behaviors as bd sub-issues of the work's lead bead — never as TodoWrite entries or markdown checklists. The bd registry is the single source of truth for decomposition.
Always:
Exceptions (emit a clarify to the lead and await the decision):
When you believe an exception applies, do not decide unilaterally: emit a
clarify message to the lead naming the work and the exception you think
applies, and await the lead's answer before proceeding.
Thinking "skip TDD just this once"? Stop. That's rationalization.
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? Delete it. Start over.
No exceptions:
Implement fresh from tests. Period.
digraph tdd_cycle {
rankdir=LR;
red [label="RED\nWrite failing test", shape=box, style=filled, fillcolor="#ffcccc"];
verify_red [label="Verify fails\ncorrectly", shape=diamond];
green [label="GREEN\nMinimal code", shape=box, style=filled, fillcolor="#ccffcc"];
verify_green [label="Verify passes\nAll green", shape=diamond];
refactor [label="REFACTOR\nClean up", shape=box, style=filled, fillcolor="#ccccff"];
next [label="Next", shape=ellipse];
red -> verify_red;
verify_red -> green [label="yes"];
verify_red -> red [label="wrong\nfailure"];
green -> verify_green;
verify_green -> refactor [label="yes"];
verify_green -> green [label="no"];
refactor -> verify_green [label="stay\ngreen"];
verify_green -> next;
next -> red;
}
Test runner. The commands below are illustrative. Use whatever test runner the BC's own harness uses. For a Python BC this is typically
pytest <path>; a TypeScript BC would usenpm test <path>. Substitute your BC's runner throughout — the discipline is the same regardless of runner.
Write one minimal test showing what should happen.
```python def test_retries_failed_operations_3_times(): attempts = {"count": 0}def operation():
attempts["count"] += 1
if attempts["count"] < 3:
raise RuntimeError("fail")
return "success"
result = retry_operation(operation)
assert result == "success"
assert attempts["count"] == 3
Clear name, tests real behavior, one thing
</Good>
<Bad>
```python
def test_retry_works(mocker):
mock = mocker.Mock(side_effect=[RuntimeError(), RuntimeError(), "success"])
retry_operation(mock)
assert mock.call_count == 3
Vague name, tests mock not code
Requirements:
MANDATORY. Never skip.
pytest path/to/test_retry.py # or your BC's runner
Confirm:
Test passes? You're testing existing behavior. Fix test.
Test errors? Fix error, re-run until it fails correctly.
Write simplest code to pass the test.
```python def retry_operation(fn): for i in range(3): try: return fn() except Exception: if i == 2: raise raise RuntimeError("unreachable") ``` Just enough to pass ```python def retry_operation(fn, max_retries=None, backoff=None, on_retry=None): # YAGNI ... ``` Over-engineeredDon't add features, refactor other code, or "improve" beyond the test.
MANDATORY.
pytest path/to/test_retry.py # or your BC's runner
Confirm:
Test fails? Fix code, not test.
Other tests fail? Fix now.
After green only:
Keep tests green. Don't add behavior.
Next failing test for next behavior. (Each behavior is a bd sub-issue when the work decomposes — see The Two Loops.)
| Quality | Good | Bad |
|---|---|---|
| Minimal | One thing. "and" in name? Split it. | test_validates_email_and_domain_and_whitespace |
| Clear | Name describes behavior | test_test1 |
| Shows intent | Demonstrates desired API | Obscures what code should do |
"I'll write tests after to verify it works"
Tests written after code pass immediately. Passing immediately proves nothing:
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:
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:
The "waste" is keeping code you can't trust. Working code without real tests is technical debt.
"TDD is dogmatic, being pragmatic means adapting"
TDD IS pragmatic:
"Pragmatic" shortcuts = debugging in production = slower.
"Tests after achieve the same goals - it's spirit not ritual"
No. Tests-after answer "What does this do?" Tests-first answer "What should this do?"
Tests-after are biased by your implementation. You test what you built, not what's required. You verify remembered edge cases, not discovered ones.
Tests-first force edge case discovery before implementing. Tests-after verify you remembered everything (you didn't).
30 minutes of tests after ≠ TDD. You get coverage, lose proof tests work.
| Excuse | Reality |
|---|---|
| "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. |
All of these mean: Delete code. Start over with TDD.
Bug: Empty email accepted
RED
def test_rejects_empty_email():
result = submit_form({"email": ""})
assert result["error"] == "Email required"
Verify RED
$ pytest # or your BC's runner
FAILED: KeyError: 'error' (expected 'Email required', got nothing)
GREEN
def submit_form(data):
if not (data.get("email") or "").strip():
return {"error": "Email required"}
# ...
Verify GREEN
$ pytest # or your BC's runner
PASSED
REFACTOR Extract validation for multiple fields if needed.
Before marking work complete (emitting work_done):
work_done proves)Can't check all boxes? You skipped TDD. Start over.
What
work_donesurfaces.work_doneevidence is, and remains, the assigned scenario(s) pass against a clean working tree. The TDD discipline in this skill is the engineering process you follow to get there — at this time it is not evidence the BC surfaces inwork_done. Run the inner loop because it produces correct, trustworthy code; do not attempt to report RED-GREEN-REFACTOR transcripts, per-test fail-watch logs, or TDD adherence as part of thework_donepayload.
| Problem | Solution |
|---|---|
| Don't know how to test | Write wished-for API. Write assertion first. Still stuck? Emit a clarify to the lead. |
| Test too complicated | Design too complicated. Simplify interface. |
| Must mock everything | Code too coupled. Use dependency injection. |
| Test setup huge | Extract helpers. Still complex? Simplify design. |
Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression.
Never fix bugs without a test.
When adding mocks or test utilities, read @testing-anti-patterns.md to avoid common pitfalls:
Production code → test exists and failed first
Otherwise → not TDD
No exceptions without the lead's decision. If you believe an exception applies,
emit a clarify to the lead and await the answer — never grant yourself the
exception.
Run the open, interactive discovery conversation that turns a directional or ambiguous prompt into a written, grounded intent record
Frame two or more shaped options against each other, surface the tradeoffs, and run the deciding conversation with the product authority
Order the candidates and commitments into a defensible sequence, capturing the ordering and its rationale as a prioritization record
Continuously maintain the map of the product's problem space so every candidate and intent has a place to hang, producing problem-space map revisions
Render the product's current state outward as an honest, grounded narrative, producing a README, site, or current-state revision that traces to real capability
Take a validated intent and drive a single candidate to shaped — bounded, de-risked, and ready for a commitment decision