Unit Test Automation With Visual Studio

Microsoft Visual Studio and .NET has its own UnitTest framework that is separate from the NUnit or other automation testing frameworks.

Let's first understand what a framework is. Framework is an architectural term and it means "collection of components".

Code Example of Test Automation using Microsoft Visual Studio 2010

I have business logic that does some math's calculation and is available in the DLL MathLibrary.dll.

The source code of this MathLibrary business logic component is as in the following:

using System;
namespace MathLibrary
{
    public class MathClass
    {
        public int Sum(int n1, int n2)
        {
            if(n1<=0 && n2<=0)
            {
                 throw new ArgumentException("No Zero or Negative are allowed");
            }
            else
            {
                 return n1 + n2;
            }
      }
}


Since it is a DLL component, test coverage of this is very critical to ensure that business logic is intact and returning correct results.

To do so, instead of relying on a Manual Test Process through a UI that will be very cumbersome and tedious to continuously test the sanity of the business logic, we can choose to automate this test process.

As you might have noticed, we have written the Sum function (in MathLibrary.dll) in a Test Driven style; i.e. the parameters passed are also being validated for all the possible test scenarios:

  1. If argument is positive and non-zero then return the sum of the passed arguments.
  2. If argument passed is 0 "zero" then throw an exception
  3. If argument passed is negative then throw an exception

How to approach Test Automation in general

Irrespective of what Unit Test tool and development technology you use, the test process depends on the following two things:

  1. Expected: Before we really perform a test our test case tells us what to expect by this test case to verify success; in the case of a Sum taking two ints, we expect it to return the exact result of adding the two numbers.
  2. Actual: When you perform the test what your result is.

If Expected meets the Actual then your Test Case has Passed, otherwise your Test Case has Failed.

Hence we must achieve Test Coverage for all the Test Cases in the test scenarios specified above.

To do so, we will use Microsoft Visual Studio 2008 or 2010 and then open a New Test Project as shown in the image below:

image.jpg

Now once you have this project loaded, it will have a file UnitTest1.cs in it, this is the file you will be automating all the Test Cases for the MathLibrary.dll (our business logic) we have created.

In order to automate the Test Cases in Microsoft Visual Studio using the Unit Testing Framework, you need to understand few more things:

  1. Test Automation Framework: Microsoft has its Unit Testing Framework implemented in the namespace "Microsoft.VisualStudio.TestTools.UnitTesting"
  2. [TestMethod] Attribute: This is the attribute that needs to be present on each method that will actually represent a TestCase.
  3. Assert class: This class is part of Microsoft's Unit Testing framework, and we can use this class to verify if our Actual and Expected are the same or not etc.

As mentioned above for each and every Test Case we must write Automation code.

Note: Before we start coding the Automated TestCases, don't forget to add all the References, WebReferences etc. that your Test infrastructure will use.

I have covered four Test Cases for the Sum function in MathLibrary.dll.

// Test Case#1: to verify if passed ints are returning Right Result
[TestMethod]
public void TestSumResultAreEqual()
{
   MathLibrary.MathClass objMath = new MathClass(); 
   Assert.AreEqual(24, objMath.Sum(12, 12));

// Test Case#2: to verify if passed ints are Not returning Right Result 
[TestMethod]
public void TestSumResultAreNotEqual()
{
    MathLibrary.MathClass objMath = new MathClass(); 
    Assert.AreNotEqual(25, objMath.Sum(12, 12));
}
// Test Case#3: to verify if passed ints are actually Zero 
[TestMethod]
public void TestSumThrowExceptionWhenZero()
{
     MathLibrary.MathClass objMath = new MathClass(); 
     try
     {
         objMath.Sum(0, 0);
     }    
     catch (ArgumentException)
     {          
         // logging code will go here
     }

// Test Case#4: to verify if passed ints are actually Negative
 
[TestMethod]
public void TestSumThrowExceptionWhenNegative()
{
    MathLibrary.MathClass objMath = new MathClass();  
    try
    {
       objMath.Sum(-123, -456);
    }
    catch (ArgumentException)
    {
       // logging code will go here
    }
}


Once you have coded all the Test Cases to test the functionality, it's time to build the code and a TestProjectName.dll will be produced.

There are two ways to Test your automation code now; they are:

  1. Using Visual Studio IDE

    Build and then Run the project within Visual Studio, you will see all the TestMethods being executed and showing Test Results as shown in the following image:

    image1.jpg
     
  2. Using Visual Studio's command prompt

    Open Visual Studio's command prompt and navigate to the folder where the TestProjectName.dll is located and then run this command:
    mstest /testcontainer:TestProjectName.dll

    image2.jpg

Summary: This article explained the aproach to Test Automagtion using Microsoft Visual Studio's Unit Testing Framework. Some basic fundamentals about Framework, what is expected out of a test and how to automate a business logic in a DLL form. The same approach can be used to even automate a WebService component etc.


Similar Articles