Test Driven Development (TDD) in C# with Simple Example

Using the TDD (Test Driven Development) approach, we first write unit tests to cover all scenarios, and then we add the code to implement the required functionality.

By using the TDD approach, we can write maintainable code, and it is easy to refactor code without missing any scenario as we will have unit tests for each scenario and high test coverage, and with TTD, we can design code module-wise.

In this article, we discuss the TDD approach in C# with a simple example. We are going to implement a Checkout class where we apply a 10% discount if the total amount is more than 100 dollars.

Open Microsoft Visual Studio 2022 and add two new projects: one for the code base and another for the test project. Here, we are using xunit for the test project.

Add a new class with the unit test method (it should have a Fact attribute) to your test project as below.

public class CheckOutTests

{

   [Fact]

   public void GetDiscount_WhenPriceMoreThan100Dollars_Return10PercentDiscount()

    {

       // Arrange

       var checkout = new CheckOut();

 

       // Act

       double afterDiscount = checkout.GetAfterDiscount(150);

 

       // Assert

       Assert.Equal(135, afterDiscount);

    }

}

Here, we are calling the GetAfterDiscount method of the CheckOut class where we didn’t have the CheckOut class.

To add CheckOut class, right-click on the CheckOut class and select Generate new type as shown below.

A screenshot of a computer

Description automatically generated

Select your code project where you want to add the CheckOut class.

A screenshot of a computer

Description automatically generated

Generate code for GetAFterDiscount() method also in the same way.

A screenshot of a computer

Description automatically generated

CheckOut class looks like below.

internal class CheckOut

  {

     public CheckOut()

      {

      }

 

     internal double GetAfterDiscount(int v)

      {

         throw new NotImplementedException();

      }

  }

As you see here, GetAfterDiscount() method didn’t get implemented and throws not implemented exception. Now run the test case, and the test case fails with the error “System.NotImplementedException : The method or operation is not implemented.”

Now, we need to refactor our code to make the unit test case successful. Change the CheckOut.GetAfterDiscount() method as below.

internal class CheckOut

{

   public CheckOut()

    {

    }

 

   public double GetAfterDiscount(int value)

    {

       if (value > 100)

       {

           var percentage = ((100m - 10m) / 100m);

           return (double)(value * percentage); // 10%

       }

       else

           return (double)value;

}

Here, we added the required functionality to the GetAfterDiscount() method. Now run the unit test and it will run successfully.

There are simple steps in TDD approach.

  1. Write Unit test.
  2. Generate classes without implementation.
  3. Run the test case, and it should fail.
  4. Refactor your code to make unit tests successful.