Unit Testing using Visual Studio 2012
In this article, I am going to explain Unit Testing and how it can be performed using Visual Studio 2012. As you all know, Unit Testing is a method by which individual unit of source code or set of modules to test if they are ready for use. The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use.
Now we will try to create some unit test using Visual Studio 2012. The Unit Test which we will create will be a very basic one which is just to explain you the process and nothing else.
Here, I am using a simple console project named Calculator which contains a .cs file named Program.cs which contains four basic methods of a calculator, specifically add, subtract, multiply, divide. the file looks something like this:
Now, right click on the solution of the project in the Solution Explorer and then create a new Project from the Add option in the context menu. then create a new Unit Test Project by selecting Test under Visual C# as shown below and give it a name.
Now as soon as you click “OK” , a new project will be created along with a UnitTest1.cs file. Now this UnitTest1.cs contains a TestClass and a Test Method as hown below:
Replace the above code by the following code:
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Calculator; namespace Calculator_test { [TestClass] public class calculator_test { [TestMethod] public void add_test() { var test_object = new Program(); int expected = 5; int actual = test_object.add(2, 3); Assert.AreEqual<int>(expected, actual, "Did not get the right va lue from add method"); } [TestMethod] public void subtract_test() { var test_object = new Program(); int expected = 3; int actual = test_object.subtract(6, 3); Assert.AreEqual<int>(expected, actual, "Did not get the right val ue from subtract method"); } [TestMethod] public void multiply_test() { var test_object = new Program(); int expected = 15; int actual = test_object.multiply(5, 3); Assert.AreEqual<int>(expected, actual, "Did not get the right val ue from multiply method"); } [TestMethod] public void divide_test() { var test_object = new Program(); int expected = 3; int actual = test_object.divide(9, 3); Assert.AreEqual<int>(expected, actual, "Did not get the right valu e from divide method"); } } }
Now in the above line- “var test_object = new Program();” , Program signifies the file Program.cs and also the Program class of the file Program.cs must be public, otherwise it will throw an error.
Also, you should notice “[TestClass]” and “[TestMethod]” at the top of each class and method of Unit Testing Project. This is compulsory otherwise the program will not be able to detect the Unit Test class and Unit Test method
Now, build the program. and then go to to the “Test” and run all test as shown below
After sometime, a “Test Explorer” window will come where you will be able to show the status of all your test i.e whether thay passed or failed. The passed test will look like (shown below):
The failed test will look like(shown below):
As you can see in the picture above. We get a detailed description of what should be the expected value and what is the actual value.
This is how you should write Unit Tests in Visual Studio. This was a begining to the Unit Testing using Visual Studio 2012. I will come come up with more advanced articles on Unit Testing in my coming articles.
Share the post "Unit Testing using Visual Studio 2012"
- tweet
5 comments
Awesome ! Well done..
Thanks for writing cleared the basic concept. Please write for how to write test cases for this.
Thanks for the interesting and knowledgeable content. I am facing certain challenges in executing the test scripts.
I am using visual studio 2012 express edition and vstest.console.exe to execute the automation test scripts in my project. I am getting the following exception:
Stack overflow exception.
The above scenario occurs when about 3 or 4 test scripts fail consecutively or in random . I am trying to figure out the issue. If you have any idea regarding the above mentioned problem , Please let me know , It will be a great help.
Pingback: Windows Store Developer Links – 2013-01-31 | Dan Rigby
Pingback: Unit Testing using Visual Studio 2012 | Trần Quang Trung