Test-Driven Development Workflow
Ensures all code development follows TDD principles with comprehensive test coverage.
When to Activate
- •Writing new features or functionality
- •Fixing bugs or issues
- •Refactoring existing code
- •Adding API endpoints
TDD Workflow Steps
Step 1: Write Test First (RED)
go
func TestProcessOrder(t *testing.T) {
order := NewOrder(100, SideBuy)
result, err := ProcessOrder(order)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Status != "filled" {
t.Errorf("got status %q; want %q", result.Status, "filled")
}
}
Step 2: Run Test — Verify FAIL
bash
go test ./... # Expected: FAIL
Step 3: Write Minimal Implementation (GREEN)
go
func ProcessOrder(order *Order) (*Result, error) {
// Minimal implementation to pass the test
return &Result{Status: "filled"}, nil
}
Step 4: Run Test — Verify PASS
bash
go test ./... # Expected: PASS
Step 5: Refactor (IMPROVE)
- •Remove duplication
- •Improve naming
- •Optimize performance
- •Keep tests green
Step 6: Verify Coverage
bash
go test -cover ./... # Target: 80%+
Coverage Requirements
| Code Type | Target |
|---|---|
| Critical business logic | 100% |
| Public APIs | 90%+ |
| General code | 80%+ |
| Generated code | Exclude |
Edge Cases Checklist
- • Nil/zero value inputs
- • Empty collections
- • Boundary values (min/max)
- • Error conditions
- • Concurrent access
- • Large datasets
Common Mistakes to Avoid
- •Testing implementation details instead of behavior
- •Tests depending on each other (shared state)
- •Skipping error path testing
- •Using
time.Sleep()instead of proper synchronization - •Not running with
-raceflag