How to write your very first Unit Test in Visual Studio using MSTest Framework easily ?

How to write your very first Unit Test in Visual Studio using MSTest Framework easily ?

Writing Unit Test in Visual Studio is very easily and it comes up with Microsoft Unit Test Framework that gives us an added advantages. Visual Studio provides lots of tool and utility to Write, Execute and Verify your code. Also there are multiple things that we need to consider with respect to writing unit test, for an example mocking, code coverage etc. In this post lets have a look how quickly and easily you care write your very unit test.

Consider you have following method that sum up two positive number. In case, if any of the number is less than 0, the method would return -1

public int AddTwoPostiveNumber(int a, int b)
{
if (a < 0 || b < 0)
{
return -1;
}
return a + b;
}

Once you have your method ready and you want to write a new unit test for this method, there are several ways to start with. You can add a new Unit Test Project and add the current project as a reference and start writing your test by adding a new test class. However, the simplest ways would be, select “Create Unit Tests” from the context menu by right clicking on the method name.

image

This will bring the “Create Unit Tests” project dialog window, where you will be asked several options to select.  By default the “Test Framework” would be “MSTest” and we don’t want to change anything on that.  Rest of the options are self explanatory, like if we want to have new project, new file etc. For this walk through, keep everything as default along with “Code for Test Method: Empty Body”.

image

Click on Ok. This will add a new unit test project in the solution as shown in the below solution explorer image.  The newly added unit test project will have all the configuration ready, along with the reference of actual project as well as a stub of unit test method.

image

Here is teh empty method definition of the Unit Test method that generates from the  templates.

namespace MyTestClass.Tests
{
[TestClass()]
public class CustomLogicTests
{
[TestMethod()]
public void AddTwoPostiveNumberTest()
{

}
}
}

Replace the TestMethod with following block that create an instance of the class, and call the actual AddTwoPostiveNumber() method to verify the sum.  Finally use Assert to verify the result.

[TestMethod()]
public void AddTwoPostiveNumberTest()
{
CustomLogic customLogic = new CustomLogic();
var result = customLogic.AddTwoPostiveNumber(10, 20);
Assert.IsTrue(result == 30);
}

As you can see we have passed 10 and 20 as parameter, so the result returns from the method would be 30. Assert would check the value, and in this case it will be true.

So our unit test cases passes here.

You can Run / Debug the Unit Test case by right click in the Test method.

image

To Visualize and check the sequence of execution during debug of test, enable the code map in Visual Studio. That will show you how the call from test methods comes to actual method and returns goes back.

image

You can now use the CodeLens to verify the status of the actual method as well as in test methods as shown in image below.

Check the Unit Test Status Easily and Effectively – Test Status Indicator in Visual Studio 2013 Preview

image

image

Hope this will help you to get started with Unit Test. In next post, we will cover more on Test explorer, test coverage and important aspect of unit test that every developer should know.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

8 Comments to “How to write your very first Unit Test in Visual Studio using MSTest Framework easily ?”

  1. Jeff

    Excellent article, but when you refer to CodeLens you might want to note that it is still restricted to Visual Studio 2013 Ultimate for some reason I can not fathom…

Comments are closed.