tdd-bdd: add horizontal-slicing anti-pattern; trigger on BDD too
This commit is contained in:
parent
881a8cb99e
commit
cabfcc019f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.DS_Store
|
||||
*.swp
|
||||
*.swo
|
||||
.nfs*
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
name: tdd-bdd
|
||||
description: Drive implementation with a strict red-green-refactor loop where every test is first stated as a BDD-flavored Given/When/Then scenario before being translated into the project's native test framework. Use when the user says "tdd", "/tdd-bdd", "use TDD", "write tests first", or asks to add a feature/fix a bug and a test suite exists (or should exist) in the repo.
|
||||
description: Drive implementation with a strict red-green-refactor loop where every test is first stated as a BDD-flavored Given/When/Then scenario before being translated into the project's native test framework. Use when the user says "tdd", "bdd", "/tdd-bdd", "use TDD", "use BDD", "behaviour-driven development", "behavior-driven development", "red-green-refactor", "write tests first", or asks to add a feature/fix a bug and a test suite exists (or should exist) in the repo.
|
||||
---
|
||||
|
||||
# tdd-bdd
|
||||
@ -31,11 +31,13 @@ description: Drive implementation with a strict red-green-refactor loop where ev
|
||||
Then <observable outcome>
|
||||
```
|
||||
- Show the scenarios to the user and get a yes before writing code.
|
||||
- The scenarios are a **plan**, not a batch of tests to write upfront. See
|
||||
the horizontal-slicing anti-pattern below.
|
||||
|
||||
### 1. Red — one scenario at a time
|
||||
- Translate the *first* scenario into the project's native test framework
|
||||
(pytest, testthat, vitest, go test, …). The Given/When/Then comments stay
|
||||
above the test as a docstring.
|
||||
- Translate **only the first** scenario into the project's native test
|
||||
framework (pytest, testthat, vitest, go test, …). Leave the rest as
|
||||
prose. The Given/When/Then comments stay above the test as a docstring.
|
||||
- Run the test. **Confirm it fails for the right reason** (missing
|
||||
behavior, not a typo / import error).
|
||||
|
||||
@ -64,10 +66,28 @@ description: Drive implementation with a strict red-green-refactor loop where ev
|
||||
- No production code exists that is not exercised by a test added in this
|
||||
loop.
|
||||
|
||||
## Anti-pattern: horizontal slicing
|
||||
|
||||
**DO NOT write all the tests first, then all the implementation.** Treating
|
||||
RED as "write every test from step 0" and GREEN as "make them all pass" is
|
||||
horizontal slicing. It produces tests of *imagined* behavior — you commit to
|
||||
shapes and signatures before the code teaches you what actually matters.
|
||||
|
||||
```
|
||||
WRONG (horizontal): RIGHT (vertical, tracer bullets):
|
||||
RED: t1 t2 t3 RED→GREEN: t1 → impl1
|
||||
GREEN: i1 i2 i3 RED→GREEN: t2 → impl2
|
||||
RED→GREEN: t3 → impl3
|
||||
```
|
||||
|
||||
Each cycle responds to what the previous cycle revealed. The scenario list
|
||||
from step 0 is a *plan*; it is not a queue of tests to enqueue at once.
|
||||
|
||||
## Guardrails
|
||||
- **Never** skip the red step. If a test passes on first run, it is
|
||||
testing the wrong thing — fix it before continuing.
|
||||
- **Never** write more than one failing test at a time.
|
||||
- **Never** write more than one failing test at a time (horizontal-slicing
|
||||
trap above).
|
||||
- If a scenario turns out to be untestable at the chosen level, surface it
|
||||
— don't silently drop it. Move it up (integration) or down (unit) one
|
||||
level and reconfirm with the user.
|
||||
|
||||
@ -7,6 +7,50 @@ core tdd-bdd loop in an R repo.
|
||||
|
||||
---
|
||||
|
||||
## Characteristics — what makes a test good or bad
|
||||
|
||||
Use this rubric before writing any `it(...)` block. The paired examples
|
||||
below are concrete instances of these rules in R.
|
||||
|
||||
### Good tests
|
||||
- Verify **behavior users or callers care about** — inputs in, observable
|
||||
outputs/state/errors out.
|
||||
- Exercise the **public interface only** (`box::use`'d exports, the
|
||||
`targets` pipeline output, the exported S3/S4 method).
|
||||
- **Survive internal refactors.** Rename a helper, split a function,
|
||||
swap an internal data structure — the test stays green.
|
||||
- **Test WHAT, not HOW.** "Returns NULL when no terms pass the threshold",
|
||||
not "calls `filter_terms()` once with `z >= threshold`".
|
||||
- **One logical assertion per `it`.** Multiple `expect_*()` calls are fine
|
||||
if they describe one outcome (e.g. a returned data.frame's class *and*
|
||||
its columns); two unrelated outcomes belong in two `it` blocks.
|
||||
- **The test name reads like a spec line.** A reader who sees only the
|
||||
`describe` + `it` titles understands the contract.
|
||||
|
||||
### Bad tests (anti-patterns)
|
||||
- **Mock internal collaborators** (`mockery::stub()` on a function inside
|
||||
the same module, just to "isolate" it). The test now passes when the
|
||||
real wiring is broken.
|
||||
- **Reach past the public interface** to verify — e.g. call a non-exported
|
||||
`pkg:::private_helper()`, inspect an environment slot, regex over
|
||||
`capture.output()` of an internal `print` method.
|
||||
- **Assert on call counts or call order** of internal helpers. Refactor
|
||||
the helper away and the test fails for no behavior change.
|
||||
- **Fail during refactor despite unchanged behavior.** The clearest
|
||||
diagnostic: if `git diff` shows only renames or extractions and the
|
||||
suite goes red, those tests were testing implementation.
|
||||
- **Test names describe mechanics**, not intent: "iterates twice over the
|
||||
list", "calls `purrr::map` with the right lambda".
|
||||
- **Verify through a side channel** instead of the function's return
|
||||
value — e.g. read a temp file the function wrote, then parse it,
|
||||
instead of asserting on the function's exposed result.
|
||||
|
||||
The single-sentence test: *if a colleague refactors the function's internals
|
||||
without changing its contract, this test should stay green.* If you can't
|
||||
honestly say yes, the test is coupled to implementation.
|
||||
|
||||
---
|
||||
|
||||
## 1. Make the "Given" explicit — fixture, not environment
|
||||
|
||||
### Bad — depends on a real project directory
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user