Skip to content

Resource · Guide or article

Test-Driven Development Example

One worked C# example of test-driven development: building a password validator test by test through red, green and refactor. Suits early-career engineers who want to see the small steps before describing TDD in an interview.

Reviewed

Open the original: Test-Driven Development Example (opens the original site in a new tab)

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

With this method, rather than writing all your tests up front, you write one test at a time and then switch to write the system code that would make that test pass. It's important to write the bare minimum of code necessary even if it is not actually "correct". Once the test passes you can refactor the code to make it maybe make more sense, but again the logic should be simple. As you write more tests, the logic gets more and more complex, but you can continue to make the…

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Lesson

What it covers

  • Four xUnit tests added one at a time: null input, too short, valid length, and a valid-length password with no digit
  • Writing the minimum code to pass, then refactoring safely, such as inverting a length check and merging two conditions
  • Why negative tests come before positive ones, and why test inputs should stay valid as rules are added

How to use it

PrepKit study advice, not a claim made by the source.

Redo the exercise in your own language with an extra rule, such as requiring an uppercase letter, and commit after each red and green step. You can then narrate the cycle from your commit history when asked whether you have used TDD.

Scope and limitations

A single C# example, not interview material. The stated rule says longer than 8 characters while the final code accepts exactly 8, so check the boundary yourself.

Access and format

Free to read online in English; part of Microsoft's Code-With Engineering Playbook, licensed CC BY 4.0.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 016770e43d8a

Evidence behind this overview
  • docs/automated-testing/unit-testing/tdd-example.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email