Loading...
Loading...
Test-driven development methodology — red-green-refactor cycle, writing failing tests first, minimal implementation, and iterative refinement. Use when implementing features test-first, when the user asks for TDD, or when writing tests before code.
npx skill4agent add claude-code-community-ireland/claude-code-resources tdd-workflowit should return 404 when resource not foundreturn 42return 42if// Pattern: [unit]_[scenario]_[expected result]
test_calculateTotal_withEmptyCart_returnsZero
test_calculateTotal_withSingleItem_returnsItemPrice
test_calculateTotal_withDiscount_appliesDiscountToSubtotal
// BDD-style
describe("calculateTotal") {
it("should return zero for an empty cart")
it("should return the item price for a single item")
it("should apply discount to subtotal")
}
// Given-When-Then
given_emptyCart_when_calculateTotal_then_returnsZerodescribeRED: Write a test for a single function or method.
GREEN: Implement just that function.
REFACTOR: Clean up the function and its test.
Cycle time: 1-5 minutes.RED: Write a test that exercises two or more components together.
GREEN: Wire the components and implement any missing glue code.
REFACTOR: Extract shared setup, clean interfaces between components.
Cycle time: 5-15 minutes.RED: Write a test that sends an HTTP request and asserts on status + body.
GREEN: Implement the route handler with minimal logic.
REFACTOR: Extract validation, business logic, and serialization into separate layers.
Cycle time: 5-20 minutes.# RED: First test — empty slug
def test_shorten_rejects_empty_url():
with pytest.raises(ValueError):
shorten("")
# GREEN: Minimal implementation
def shorten(url):
if not url:
raise ValueError("URL cannot be empty")
pass # nothing else needed yet
# RED: Second test — returns a short code
def test_shorten_returns_six_char_code():
result = shorten("https://example.com")
assert len(result) == 6
# GREEN: Hardcode, then generalize
def shorten(url):
if not url:
raise ValueError("URL cannot be empty")
return url[:6] # naive but passing
# RED: Third test — codes are unique
def test_shorten_returns_unique_codes():
a = shorten("https://example.com/a")
b = shorten("https://example.com/b")
assert a != b
# GREEN: Now we need real logic
import hashlib
def shorten(url):
if not url:
raise ValueError("URL cannot be empty")
return hashlib.md5(url.encode()).hexdigest()[:6]
# REFACTOR: Extract hashing, add type hints, rename for clarityIs the behavior well-understood?
YES -> Classic TDD (test-first)
NO -> Spike first (throwaway prototype), then TDD the real implementation
Is the code interacting with external systems?
YES -> Write a contract/interface test, then use a fake/stub for unit TDD
NO -> Pure function TDD (easiest case)
Is the code algorithmically complex?
YES -> Start with simple examples, build up with property-based tests
NO -> Standard example-based TDD
Are you fixing a bug?
YES -> Write a test that reproduces the bug FIRST, then fix it
NO -> Normal TDD cycle