Test-Driven Development (TDD)
Test-driven development is a way of working in which the test comes before the code: first a failing test, then the code that turns it green, then cleaning up. You repeat the three steps every few minutes.
Test-driven development (TDD) is a way of working in which you write the test before the code. Every few minutes, you repeat three steps: red, green, clean up.
The cycle
- Red: You write a test for the next small piece of behavior that is still missing and watch it fail. A test that does not compile because the method is still missing is red too.
- Green: You write as little code as needed until all tests pass. Not pretty, just green.
- Clean up: With green tests, you improve code and tests, merge duplicates, and find better names. The tests stay green while you do.
Then the cycle starts again with the next test. The rules behind it: no code without a red test that demands it, and only as much of the test as it takes to make it red.
The benefit is tangible. Every test was red once, so you know it can fail. You think first about how someone calls the method, and only then about how it works inside. And a new error is almost always in the last few lines.
TDD in .NET
In .NET, you usually write the tests with xUnit and run them with dotnet test; the tests in the cycle are ordinary unit tests. For Visual Studio and the Test Explorer, Microsoft has a guide of its own. Because you write the tests first, you end up with methods that are easy to call: a rule that knows only pure inputs and outputs needs neither a database nor a clock.
A red test, before the method exists:
[Theory]
[InlineData("", 0)]
[InlineData("5", 5)]
public void Add_SingleNumber_ReturnsSameNumber(string input, int expected)
{
var calculator = new StringCalculator();
var actual = calculator.Add(input);
Assert.Equal(expected, actual);
}
The class StringCalculator does not exist yet, the test does not compile, so it is red. Only now do you write the simplest method that turns it green.
Misconceptions
- TDD is not test coverage. High coverage can also be reached afterward. TDD is a design method: the test states what the code is to be able to do.
- TDD does not mean testing everything first. A form, a user interface, or an Azure service are checked differently, see quality assurance. TDD pays off where there are rules with clear inputs and outputs.
- No substitute for integration tests. Unit tests do not see the registration in the dependency injection container or the real database.
An example from the Kladde
The public appointment booking of the Kladde needs a rule that calculates free times: opening hours, duration, lead time, lunch break, and times already taken. It is written as a pure function in the core of the Kladde and is developed test-first, with the edge cases of a past day, the daylight saving time change, the lunch break, and a full day. Only after that comes the Blazor page, which merely offers this rule.
In the course
You learn TDD in K1, module 1.8, in the lesson Test-driven development, with a kata and a new rule of the Kladde. Before it come Why tests and Good tests. You build the rule for free times in K2, module 2.5, in The public appointment booking.