/tdd -- Red-Green-Refactor for .NET
What
Guides a strict test-driven development cycle for .NET features. Instead of writing implementation first and bolting on tests after, this command flips the order: write a failing test that defines the desired behavior, implement the minimum code to make it pass, then refactor with confidence.
Every cycle uses the .NET testing stack:
- xUnit v3 -- Test framework with
[Fact]and[Theory] - WebApplicationFactory -- Integration tests against the real HTTP pipeline
- Testcontainers -- Real databases (PostgreSQL, SQL Server) in tests
- Verify -- Snapshot testing for complex response structures
- FakeTimeProvider -- Deterministic time in tests
When
- User says "TDD", "test-driven", "let's TDD this", "write the test first"
- Building a new feature with clear acceptance criteria
- Fixing a bug (write a test that reproduces the bug first, then fix)
- Adding behavior to an existing feature (test the new behavior first)
- Any time the user wants proof that code works before it ships
Skip TDD for: Trivial config changes, scaffolding without logic, documentation.
How
Cycle: Red -> Green -> Refactor
Each feature goes through one or more TDD cycles. A cycle covers one discrete behavior.
Step 1: Red -- Write the Failing Test
Write a test that describes the desired behavior. The test MUST fail because the implementation does not exist yet.
Run the test and confirm it fails:
If the test passes without implementation, the test is not testing what you think. Rewrite it.
Step 2: Green -- Minimal Implementation
Write the minimum code to make the test pass. Do not add features, optimizations, or edge case handling. The goal is a green test, nothing more.
- Create the endpoint, handler, request/response types, and EF config as needed
- Use the simplest logic that satisfies the test assertion
- Do not refactor yet -- ugly passing code is fine at this stage
Run the test and confirm it passes:
Step 3: Refactor -- Clean Up with Confidence
Now that the test is green, refactor freely:
- Extract methods, rename variables, improve structure
- Apply modern C# patterns (primary constructors, records, collection expressions)
- Add validation, error handling, and edge cases (with new tests for each)
- Run the full test suite after each refactor step to catch regressions
If any test goes red during refactoring, undo the last change and try a smaller step.
Multi-Cycle Features
Most features require multiple TDD cycles. Plan the cycles upfront:
Each cycle adds one behavior. Never combine multiple behaviors in a single cycle.
Test Infrastructure Setup
If the project lacks test infrastructure, set it up before the first cycle:
- Create test project with xUnit v3 and required packages
- Configure
WebApplicationFactorywith Testcontainers for the real database - Add a shared fixture to avoid spinning up containers per test class
- Verify the infrastructure with a simple health-check test
Example
Related
/verify-- Run full verification after completing all TDD cycles/scaffold-- Generate initial feature structure that tests will drive
