Rohit's Tech World

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:

spacer

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.

spacer

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:

spacer

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

spacer

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):

spacer

The failed test will look like(shown below):

spacer

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"

  • Facebook
  • Twitter
  • Google+
  • Pinterest
  • LinkedIn
  • E-mail
calculator test test object Testing using Visual Studio Testing using vs2012 unit test Unit Testing Unit Testing using Visual Studio 2012 vs 2012 unit testing vs2012 testing 2013-01-30
  • tweet
  • spacer
  • Previous: Create your own Windows 8 Start Button with zero memory usage
    Next: Anonymous Classes in C#

    5 comments

    1. spacer
      Ankush
      July 24, 2013 at 7:42 pm

      Awesome ! Well done..

      Reply
    2. spacer
      Ankit
      July 4, 2014 at 9:12 am

      Thanks for writing cleared the basic concept. Please write for how to write test cases for this.

      Reply
    3. spacer
      vishal
      July 17, 2014 at 12:46 pm

      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.

      Reply
    1. Pingback: Windows Store Developer Links – 2013-01-31 | Dan Rigby

    2. Pingback: Unit Testing using Visual Studio 2012 | Trần Quang Trung

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    *

    *

    You may use these HTML tags and attributes: <a class="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    @Follow Me

    • #Blogged #rohitshaw 2012 in review t.co/IDy2SKOavd 1 hour ago
    • #Blogged #rohitshaw Latest update for Windows Phone 7's: 7.10.8107.79 t.co/lEDGK6kbmR 4 hours ago
    • #Blogged #rohitshaw Review: Absolute Barbeque ( India's First Wish Grill) t.co/jg6p34eQT6 7 hours ago
    • #Blogged #rohitshaw Adobe Revel comes to Android t.co/gUoFYxai2z 10 hours ago
    • #Blogged #rohitshaw Delete All Empty Folders and Subfolders from a directory using Right Click t.co/w0Ez8CEG6Z 13 hours ago
    gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.