Monday, December 1, 2008

Unit test in VS 2005

If you didn't know it already, it is not a difficult one to learn.Tremendous progress is being made on several fronts: IDE integration, process integration, and new test fixtures. In here I will cover unit testing in Visual Studio 2005, including VSTS unit testing, NUnit and MBUnit--the Superman of unit testing. First post I’ll cover NUnit testing.

NUnit
NUnit is the unit testing framework that has the majority of the market share. It utilizes attributes to identify what a test is.
The TestFixture attribute is used to identify a class that will expose test methods.
The Test attribute is used to identify a method that will exercise a test subject.
Let's get down to business and look at some code. First we need something to test.
public class Subject
{
public Int32 Add(Int32 x, Int32 y)
{
return x + y;
}
}
That Subject class has one method: Add.
We will test the Subject class by exercising the Add method with different arguments.
[TestFixture]
public class tSubject
{
[Test]
public void tAdd()
{
Int32 Sum;
Subject Subject = new Subject();
Sum = Subject.Add(1,2);
Assert.AreEqual(3, Sum);
}
}
The class tSubject is decorated with the attribute TestFixture, and the method tAdd is decorated with the attribute Test.
You can compile this and run it in the NUnit GUI application. It will produce a successful test run.
That is the basics of what NUnit offers. There are attributes to help with setting up and tearing down your test environment:
SetUp, SetUpFixture, TearDown, and TearDownFixture. SetUpFixture is run once at the beginning when the fixture is first created; similarly,
TearDownFixture is run once after all tests have completed. SetUp and TearDown are run before and after each test.
NUnit tests can be run several different ways: from the GUI application, from the console's application, and from a NAnt task. NUnit has been integrated into Cruise Control .NET as well. In the last product review, you will see how it has been integrated into the VS.NET IDE as well.

No comments:

Post a Comment

MS CRM 2011 KB Article customization Issue.

Recently I have encountered some issue while customizing Kb Article Entity. I was doing following configuration in Article form. 1. Add Ba...