lukabor ef22a7dbdb feat(tdd-bdd): add Python test reference enforcing pytest fixtures + BDD
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 12:49:48 +02:00

4.2 KiB
Raw Blame History

name, description
name description
tdd-bdd 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

When to use

  • User says "tdd", "/tdd-bdd", "test first", "write a failing test first".
  • A bug fix or new behavior is requested and the repo has a test runner (or one can be added in < 5 min).
  • Behavior is observable from outside the unit (input → output, state change, side effect) — pure refactors are out of scope.

Language references — Python

Pytest only: fixtures via @pytest.fixture (no unittest.TestCase), parametrize, scenario as docstring. No Gherkin frameworks solo.

  • tests-py.md — paired good/bad examples + characteristics.

Language references — R

Testthat ed. 3+, box::use, describe/it.

Process

0. Frame

  • Restate the change as one sentence: " should be able to so that ".
  • List 2–5 scenarios in Given/When/Then form. Include at least one happy path and one edge / failure case.
    Scenario: <short name>
      Given <precondition>
      When  <action>
      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 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).

2. Green — minimum code

  • Write the smallest change that makes the failing test pass. No speculative abstractions, no extra cases.
  • Run the full test suite, not just the new test.

3. Refactor

  • With the suite green, clean up names, duplication, dead branches.
  • Re-run the suite after every non-trivial edit.

4. Next scenario

  • Loop back to step 1 with the next scenario. Stop when every scenario from step 0 is green.

5. Report

  • One-line summary per scenario: ✓ name → file:test_name.
  • Note any scenario that revealed a missing requirement and got added mid-loop.

Output / done criteria

  • Every scenario in step 0 has a matching, passing test.
  • The full test suite is green.
  • Each test carries its Given/When/Then docstring.
  • No production code exists that is not exercised by a test added in this loop.

Anti-patterns

See anti-patterns.md — horizontal slicing, passing on first run, refactoring while red, silently dropping an untestable scenario.

Guardrails

  • Never skip the red step (see anti-patterns.md "Passing on first run").
  • Never write more than one failing test at a time (see anti-patterns.md "Horizontal slicing").
  • Refactor only with a green bar (see anti-patterns.md "Refactoring while red").
  • If a scenario turns out untestable at the chosen level, surface it (see anti-patterns.md "Silently dropping…").

See also