The Green Stage: Making the Tests Pass
Explore the green stage of test-driven development by learning to write the minimal code required to pass existing tests using xUnit in .NET. Understand how to implement functionality quickly, validate tests, and prepare for future code refactoring to maintain clean and reliable software.
We'll cover the following...
The green stage of the TDD process is the phase that takes place when we already have tests that verify the behavior of the future code. Our goal for this stage is to write the minimal code required to pass the tests. We don’t look for a perfect solution but simply develop the quickest solution possible.
Implementing the green stage
In the playground below, we build an incomplete application to convert MD text into HTML. We have already implemented the red stage of the TDD process and completed tests for that functionality. Now, we must populate the actual application with logic to make our tests pass.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Note: The tests in the initial setup are expected to fail because we only have a solution that fulfills the red stage of TDD.
Our scenario is covered by a single test on line 6 of the MdToHtmlConverterTests.cs file in the MainApp.Tests project. The test verifies that the code can fulfill the following requirements:
Convert MD paragraphs into HTML paragraphs surrounded by the
<p>tag.Convert the bold MD text, which is surrounded by the ...