Search⌘ K
AI Features

Solution Review: Adding an Automated Test

Explore how to add automated tests using xUnit in .NET by writing parameterized tests with the Theory and InlineData attributes. Learn to verify outputs efficiently for TDD practice by working through a complete solution example that uses Assert to confirm expected results.

We'll cover the following...

The completed solution is presented in the following code playground:

namespace MainApp;

public class TextProcessor
{
    public int CountNotWhitespaceCharacters(string? text)
    {
        if (string.IsNullOrWhiteSpace(text))
            return 0;

        int count = 0;

        foreach (var character in text.Trim())
        {
            if (char.IsWhiteSpace(character))
                continue;

            count++;
        }

        return count;
    }
}
Completed solution

  • Line 5: We mark our method with the [Theory] attribute ...