Lesson 2
Generalizing Solutions with Test Driven Development (C#)
Introduction to Generalization in TDD

Welcome back to our course on Test-Driven Development (TDD) in C# using xUnit. In our previous lesson, we introduced the fundamentals of TDD and the Red-Green-Refactor workflow. Now, we will advance our TDD skills by focusing on generalizing solutions and enhancing the complexity of our testing scenarios.

As a brief reminder, TDD involves a repetitive cycle known as Red-Green-Refactor:

  • Red: Write a failing test to clarify the new functionality you aim to implement.
  • Green: Develop the smallest amount of code needed to make that test pass.
  • Refactor: Clean up the code, enhancing its quality while maintaining its functionality and ensuring all tests remain passing.

In this lesson, we will expand upon the Sum method, demonstrating how to generalize it while following these TDD principles.

Examining the Current Code Structure

Before we dive into coding, let's review our current setup. You are already familiar with the Sum method from Math.cs and its corresponding test in MathTests.cs:

C#
1// MathTests.cs 2using Xunit; 3 4public class MathTests 5{ 6 [Fact] 7 public void Sum_ShouldAddTwoNumbersCorrectly() 8 { 9 var math = new Math(); 10 var result = math.Sum(2, 3); 11 Assert.Equal(5, result); 12 } 13}
C#
1// Math.cs 2public class Math 3{ 4 public int Sum(int a, int b) 5 { 6 return 5; 7 } 8}

This existing setup serves as a foundation. Now, we'll focus on expanding your understanding by generalizing the approach using TDD principles. Understanding where you've come from will help ensure future changes enhance our function without straying too far from the core logic.

Example: Red Phase - Adding a New Failing Test

To embrace the Red phase, let's introduce a new test case that will fail.

Update MathTests.cs to include more input scenarios:

C#
1// MathTests.cs 2using Xunit; 3 4public class MathTests 5{ 6 [Fact] 7 public void Sum_ShouldAddTwoNumbersCorrectly() 8 { 9 var math = new Math(); 10 var result = math.Sum(2, 3); 11 Assert.Equal(5, result); 12 } 13 14 [Fact] 15 public void Sum_ShouldAddTwoMoreNumbersCorrectly() 16 { 17 var math = new Math(); 18 var result = math.Sum(5, 6); 19 Assert.Equal(11, result); 20 } 21}

By including a new scenario with new inputs, this step is intentionally set to fail to define our target goal clearly.

Running this test:

Output:

1[xUnit.net 00:00:00.48] MathTests.Sum_AddsTwoMoreNumbersCorrectly [FAIL] 2 Failed MathTests.Sum_AddsTwoMoreNumbersCorrectly [1 ms] 3 Error Message: 4 Assert.Equal() Failure 5Expected: 11 6Actual: 5

This failure confirms that the new functionality needs addressing.

Example: Green Phase - Implementing a Solution to Pass Tests

Now, let's transition to the Green phase, where our goal is to ensure all tests pass, including our new one. We can update our Sum method to use the generic response because it is the minimal solution.

C#
1// Math.cs 2public class Math 3{ 4 public int Sum(int a, int b) 5 { 6 return a + b; 7 } 8}

Output:

1Passed! - Failed: 0, Passed: 2, Skipped: 0, Total: 2, Duration: 3 ms

Success! The Green phase is complete, illustrating how effective writing minimal code to pass tests can be.

Example: Refactor Phase - Generalizing the Solution

Finally, let's advance into the Refactor stage. The Sum method is very simple, so there's nothing we can make better with the implementation. We do have a lot of duplication in our tests, however.

Let's introduce "parameterized testing." A parameterized test is a single test that has several inputs and outputs. It makes it easy to add a lot of test cases without duplicating too much test code.

C#
1// MathTests.cs 2using Xunit; 3 4public class MathTests 5{ 6 [Theory] 7 [InlineData(2, 3, 5)] 8 [InlineData(5, 6, 11)] 9 public void Sum_ShouldAddTwoNumbersCorrectly(int a, int b, int expected) 10 { 11 var math = new Math(); 12 var result = math.Sum(a, b); 13 Assert.Equal(expected, result); 14 } 15}

For now, there's no functional change — but strategically refactoring in this way offers benefits, maintaining clarity as complexity escalates over iterations.

Important: The [Fact] decrotator has changed to [Theory] to indicate that this is a paramaterized test and not a standalone test.

Review and Preparation for Practice

In this lesson, we delved into using TDD to generalize solutions in C# by:

  • Reviewing and examining the initial state of the Sum method and its associated test cases.
  • Engaging in the Red phase by adding a new failing test to identify necessary functionality improvements.
  • Advancing to the Green phase by implementing minimal code changes to pass the new test.
  • Moving to the Refactor phase, where we utilized parameterized testing to reduce test duplication and improve maintainability.
Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.