How to write Unit Tests in VSTS?

No matter which framework you chose fundamentally things remain same:-

You need one or more classes where you will write methods with code that will test other classes and functions of your main code. These classes are called TestClass in VSTS (and TestFixture in NUnit). Such classes are marked with TestClass attribute (or TestFixture in NUnit).

The methods of the Test Class that execute a unit of the main code and verify the result are called TestMethod (or Test in NUnit). These methods are marked with TestMethod attribute (or Test in NUnit).

At times there might be a need to perform certain initialization operations before running each of the Test methods such initialization operation can be written inside a TestInitialize method (or Setup in NUnit). Correspondingly the cleanup operations that should be performed after each Test can be written in TestCleanup method (or TearDown in NUnit).

VSTS Test example:-

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class SampleUnitTest
{
[TestInitialize()]
public void Init() { }

[TestMethod]
public void TestOperation() { }

[TestCleanup()]
public void Destroy() { }
}

NUnit Test example:-

using NUnit.Framework;

[TestFixture]
public class SampleUnitTest
{
[SetUp]
public void Init() { }

[Test]
public void TestOperation() { }

[TearDown]
public void Destroy() { }
}

Apart from these commonly used methods there can be additional methods provided based on the selected framework, like – static ClassInitialize (or TestFixtureSetup in NUnit) and static ClassCleanup (or TestFixtureTearDown in NUnit). ClassInitialize executes only once before any test method in the test class is executed and ClassCleanup executes only once after all test methods in the test class has been executed.

Further, apart from the above attribute classes of the framework another very important class is the Assert class that contains static methods for comparison and verification of results of the test against some expected values.

Having the above fundamental knowledge of Unit Testing, we are now ready to write some example.

I do not prefer replicating the classic LoginInfoTest example by Mark Michaelis and extended by authors at codeproject.com and other technical blogs. Rather I have something very simple (and my favorite) a variation of “Hello World”, the “Hello User” example.

Step 1: Create our main application

Lets create and assembly HelloUser with a class Greeting with a method Greet that will return greeting message string for the given user.

namespace HelloUser
{
/// <summary>
/// Greets User
/// </summary>
public class Greeting
{
/// <summary>
/// returns greetings based on given user
/// </summary>
/// <param>name of the user</param>
/// <returns>greeting for the user</returns>
public string Greet(string userName)
{
if (string.IsNullOrEmpty(userName))
return “Hello User”;

switch (userName)
{
case “Kamal”:
return “Hi Kamal”;
case “Shameer”:
return “Hey Shameer”;
case “Roy”:
return “Hoi Roy”;
default:
return “Hello ” + userName;
}
}
}
}

Note: In a Test Driven Development approach we would not have implemented the method before writing the following test, but for simplicities sake we will not get into TDD.

Note: Although I have marked the class and method as public for simplicities sake, the auto code generation of VSTS is also capable of generating test code stubs for non public members, however the generated code stub will be quite complex to understand.

Step 2: Creating Unit Tests

[TestMethod()]
public void GreetTest()
{
Greeting target = new Greeting();
string userName = “Bart”;
string expected = “Hello Bart”;
string actual;
actual = target.Greet(userName);
Assert.AreEqual(expected, actual);
}



For further reading refer: http://www.indiaosl.com/blog/?p=307