
What is Unit Testing ?
Unit testing is testing the basic functionality of modules (methods) to ensure that they are performing as expected.
Why Unit Testing ?
Unit Test is important in the process of software development to ensure the accuracy of basic functionality during the development period.
Unit Testing in VS 2010
Visual Studio 2010 got a very user friendly Unit Testing environment. The Unit Testing frame work used by VS 2010 is Ms-test. Let’s look one simple example here.
Create a simple project
Let’s create a very minimal project with a class called “Calculator” and a method called “Add” to add two integer values.
So Finally the simple class will appear like
class Calculator
{
public static int Add(int val1, int val2)
{
return (val1 + val2);
}
}
Create a Test Project
Now we have to create a Test Project in Visual Studio 2010
1) Go to Menu “Test” and click “New Test”
2) Now “Add New Test” will appear.

"Add New Test" Dialog Box
3) Now click “Unit Test Wizard” and click “Ok“.
4) Then “Create Unit Test” wizard will appear on screen.

"Create Unit Test" Wizard
This wizard is where you are going to specify the classes and methods you need test.
5) Carefully select the classes and methods and hit “Ok”
Now you should be able to see a new test project in the Solution Explorer.

Main Project and Test Project in Solution Explorer
Preparing the Test Method
Now you can explore and see for Test Methods Stubs created by Visual Studio automatically. But still the test implementation is remains uncompleted.
My Test Method Stub for Add Method is appearing like
[TestMethod()]
public void AddTest()
{
int val1 = 0; // TODO: Initialize to an appropriate value
int val2 = 0; // TODO: Initialize to an appropriate value
int expected = 0; // TODO: Initialize to an appropriate value
int actual;
actual = Calculator.Add(val1, val2);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
As you can see here visual studio created some sample code as well. But just delete them all. We will add our own fresh test implementation. Then it’s easy to understand 
So our implementation will look some thing like below
[TestMethod()]
public void AddTest()
{
int ExpectedValue = 10;
int ActualValue = Calculator.Add(5,5);
Assert.AreEqual(ExpectedValue,ActualValue,"Test Case Failed");
}
In a simple test case there will be 3 things.
- Expected Value
- Actual Value
- Assert Comparison
So in our example we are expecting 10 from our add method.
And in out Actual Value we are calling the actual method implementation by passing two value ( 5+5 = 10)
int ActualValue = Calculator.Add(5,5);
And Finally we are Comparing Actual And Expected value with the “Assert.AreEqual()” Method of MS Test Framework.
Assert.AreEqual(ExpectedValue,ActualValue,"Test Case Failed");
Running the Test Cases
Now just right click on the test case method and click “Run Tests” option.
Now you should see your test result in Test Results Window.

Test Result
Test Fail Messages
Assert.AreEqual(ExpectedValue,ActualValue,"Test Case Failed");
You may noticed some text like “Test Case Failed” in the AssertEqual() method. These are Failure Messages. In case of a test method failure these messages will give detailed description of the failure. It is a good practice to write failure messages.
We are done. I’ll dig into unit testing in my following posts as well. and i love to here from you back as well so put your comments and corrections.
Happy Testing
Download Source Code Example
Recent Comments