Test Driven Development In Visual Studio 2019

Introduction

 
In today’s article we will look at the concept of Test-Driven Development (TDD). We will see how we can write tests for the various functions we create. We will use Visual Studio 2019 as the development environment. We will create a simple class and add some functions to it. We will then write some tests for these functions.

What is TDD?

 
Over the years I have seen many definitions for Test Driven Development, or simply TDD. The main concept is to write tests and after that write the real code. While this is a good idea in theory, I have rarely seen it implemented in the real world. The process mostly seen is that we write our classes and functions and after that we write tests to ensure the code works properly in most scenarios. This also protects our code from future changes as any breaking change that might not be evident in the application flow will be caught while running the various tests we have written. In the next section, we will see how to write tests for a class which has a few functions. The example is simple, but the same concept can be applied to complex classes and functions.
 

Creating a solution and adding tests for it

 
Let us start by creating a .NET core console application in Visual Studio 2019 Community Edition. This is the free version of Visual Studio available to download and work with.
 
Test Driven Development In Visual Studio 2019
 
Test Driven Development In Visual Studio 2019
 
Test Driven Development In Visual Studio 2019 

Below, is the skeleton project created for us.
 
Test Driven Development In Visual Studio 2019 

We then add a new class to the existing project.
 
Test Driven Development In Visual Studio 2019 
 
Once the new class has been created, add the below code to it,
  1. namespace ConsoleAppTDD {  
  2.     public class MathCalculator {  
  3.         public int AddValues(int valueA, int valueB) {  
  4.             return valueA + valueB;  
  5.         }  
  6.         public int SubtractValues(int valueA, int valueB) {  
  7.             return valueA - valueB;  
  8.         }  
  9.         public int MultiplyValues(int valueA, int valueB) {  
  10.             return valueA * valueB;  
  11.         }  
  12.     }  
  13. }  
We now have a class and some functions added to it. Next, we will create a Test project and add tests for these functions.
 
Select the solution, right click, and select Add and then new project. Type “tests” in the search box and select the “MSTest Test Project (.NET Core)” as below,
 
Test Driven Development In Visual Studio 2019
 
Test Driven Development In Visual Studio 2019 
 
Add a reference to the original project.
 
Test Driven Development In Visual Studio 2019 
 
Now, add the below code to the class created.
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  2. namespace ConsoleAppTDD.Tests {  
  3.     [TestClass]  
  4.     public class UnitTest1 {  
  5.         Math Calculator mc;  
  6.         public UnitTest1() {  
  7.             mc = new MathCalculator();  
  8.         }  
  9.         // Standard Tests  
  10.         [TestMethod]  
  11.         public void AddValues_Test() {  
  12.                 int val = mc.AddValues(10, 20);  
  13.                 Assert.AreEqual(val, 30);  
  14.             }  
  15.             [TestMethod]  
  16.         public void SubtractValues_Test() {  
  17.                 int val = mc.SubtractValues(10, 20);  
  18.                 Assert.AreEqual(val, -10);  
  19.             }  
  20.             [TestMethod]  
  21.         public void MultiplyValues_Test() {  
  22.             int val = mc.MultiplyValues(10, 20);  
  23.             Assert.AreEqual(val, 200);  
  24.         }  
  25.     }  
  26. }  
Now, lets add the Test Explorer as below,
 
Test Driven Development In Visual Studio 2019 
 
We then right click on the Unit tests in the Test explorer and click “Run Tests”. We see below all works fine.
 
Test Driven Development In Visual Studio 2019 
 
If we change some code which is not done correctly, the respective test will fail.
 
Test Driven Development In Visual Studio 2019 
 
In addition to running the tests, we can also debug the tests as done below,
 
Test Driven Development In Visual Studio 2019 
 
This way we can see values at each stage of the test to fix any issue we might have.
 
Test Driven Development In Visual Studio 2019 
 

Summary

 
In this article, we looked at the concept of Test-Driven Development, also known as TDD. We saw how we can add tests for our classes and functions. Adding tests is an especially important phase of any development task and must be considered while planning for any code related activity. These tests can be run manually and also be made part of the DevOps process to ensure all tests have passed before we proceed with any new deployment. Happy Coding!