TDD (Test Driven Development) Using MSTest

Introduction

TDD is a simple testing methodology for developers to make sure their code won’t break.

As its name - Test Driven Development - suggests, developers need to write test cases first, which should lead to development without any errors.

Below is a simple flow chart for TDD.

TDD

We will see a calculator example using MS Test Framework.

Requirement

Develop the calculator to take N number of inputs and provide the result.

We will do this above requirement with the TDD approach.

From Visual Studio create 2 projects.

  • Create a test project with MS Test (uses .Net Core) with the name “Calculator. Test”
    Calculator.Test
  • Create an empty class library “Calculator”
    Calculator
  • After creating a project your solution explorer looks like the below.
    Solution explorer

Now start writing test cases in the Test Project. Create a class with CalciTest.cs.

In CalciTest class create the Test method as below.

Test method

Inside CalculatorTestAdd, create a Calci class instance. Then it will show a compile error. Click on the bulb symbol and click on “Generate new type”

Generate new type

On clicking “Generate new type” you will get the “Generate Type” pop-up window, and fill in the info below to auto-generate the Calci class.

Generate Type

  1. Provide the access modifier as per the requirement.
  2. Select Class, as we are writing a unit test case for class.
  3. Select Project, i.e. Assembly where we want to add the class.
  4. Select Create a new file as we are creating the class the first time.
  5. Click OK.

after this Calci class will be auto-generated in the Calculator Project.

Now in test cases, call the method that we want to implement with parameters like the below, and click on “Generate Method ‘Calci. Add’” for auto-generation of the Add method.

After clicking on “Generate Method ‘Calci. Add” Add method will auto-generate in Calci.cs file.

Generate Method

After this code builds successfully, then execute the test in Test Explorer. The test will fail as below stating that:

“Test method Calculator.Tests.CalciTest.CalculatorTestAdd threw exception: System.NotImplementedException”

CalculatorTest

Fix this issue like below and execute it again.

Excute

What we did above was.

Code

This test is passed.

Passed

Does this test have enough inputs to meet all our requirements? No, because we need to give any number of inputs to methods.

Now try with 3 parameters.

Parameters

For this, again the code won’t compile and again it asks us to create a new method with 3 parameters with the same name; i.e. method overloading concept which we need to follow, if we want to make the code compile.

If we follow method overloading here, we need to keep on writing the N number of methods with N number of inputs; i.e. nightmare.

So we need to refactor our code like below to support any number of inputs.

Inputs

What we dis above was.

Test

Now execute the test and test cases will pass.

 Test cases

Let me add one more test with 0 inputs and execute the test.

Add

With 3 possibilities I believe, I handled the requirement well. If it still has to improve please download zipped source and give it a try by fixing and refactoring the code.

What I did above was.

Stop

Summary

I showed you the TDD approach with very simple examples. I hope it helps.


Similar Articles