Test-Driven Development (TDD) means writing your tests before you write the code.
🔁 The TDD Cycle
- Write a failing test (it describes what you want)
- Write just enough code to make the test pass
- Refactor your code (clean it up)
Repeat!
🧑💻 Example
Write a test for IsEven(int n)
:
1
2
3
4
5
[Fact]
public void IsEven_ReturnsTrue_ForEvenNumbers()
{
Assert.True(IsEven(4));
}
Then, write the code:
1
bool IsEven(int n) => n % 2 == 0;
📝 Why TDD?
- Forces you to plan before coding
- Catches bugs early
- Makes code easier to change
💡 Try It!
Pick a function you want to write. Write the test first, then the code!
Next: Continuous integration basics!