Monday, February 3

Test Driven Development (TDD) in Visual Studio

Introduction: Visual studio 2012/2013 supports Test Driven Development (TDD) through its unit testing framework.This demonstrates how to build test method in c# using unit testing framework and viewing results in test explorer.

Steps:
 Create a new project as shown below:


The solution explorer should look like this:

Create a new Class: Right click on TDD| Add New Item| Add Class and name it Calculator.

Write a basic test method: Double click UnitTest1.cs under TDD.Tests folder.

Generate the method from test:

Place the cursor on sum and then on the short cut menu choose Generate, Method Stub.
This will create a sum method in Calculator.cs.


Run the unit Test: On the Test menu,choose Unit Test,All Tests. The Test Explorer opens and displays the result.

The test fails because sum() is not implemented.
Let's implement the sum() in Calculator.cs file.

Now run the unit test again.

It fails because the expected result and actual result is not same.
Lets modify the sum() as follows:


Run the unit test again. The method passes this time.































No comments:

Post a Comment