Hi Everybody,
As you know, VS.NET 2008 comes with unit testing integrated within. To start with a test
driven approach, we need to understand how unit testing works in VS.NET 2008. Many of us
might be using already available tools / frameworks for this, viz - NUnit (Most Popular). So all
those who want to shift to VS.NET 2008 unit testing, wont be unhappy. Similar kind of
approach is followed in VS.NET 2008. You have to use attributes to mark-up test classes and
methods. You can you Assert class for checking conditions. Some of the features are described
below.
Many of the features provided in NUnit, at present, are missing from this implementation. For
example, you cannot test message boxes, rather UI elements, from within VS.NET 2008. So we
need to wait until it is included in future releases. Targeted audience for this post is beginers
who want to get a feel of test driven development
1. You can create unit test cases by writing code from scratch or using a unit test wizard.
Unit test wizard does ask you some project for which unit test cases are to be generated.
2. Attributes, like in NUnit, are used to denote various test methods and test classes.
3. UI testing is not supported in the current version of unit testing with VS.NET 2008
4. You cannot run NUnit test cases from within VS.NET 2008. You will need separate EXE to do
that (The NUnit framework)
Steps required for writing unit test cases in VS.NET 2008
1. Create new class representing a unit test case. The attribute used is – TestClass.
For example –
[TestClass()]
public class Window1Test
You can do this by either writing the code yourself or generating it through the wizard.
2. Create new test methods. The attribute used is – TestMethod. For example –
[TestMethod()]
public void Window1ConstructorTest()
If you have generated test cases through wizard, most of the test methods are generated
for you automatically. If you want, you can always add some custom test methods as s
hown above.
3. To check some condition, Assert class is available with all kind of static methods.
If the condition is false, exception will be thrown with error message provided by you.
For example –
Assert.IsFalse(blnSample, "The boolean value should not be true.");
The meaning of this statement is, if “blnSample” boolean value is true, throw exception
with "The boolean value should not be true." as exception message.
Other attributes, which can be used are –
1. Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
}
2. Use ClassCleanup to run code after all tests in a class have run
[ClassCleanup()]
public static void MyClassCleanup()
{
}
3. Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
}
4. Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup()
{
}
How to run unit test cases you have written ?
There is a separate menu provided for unit testing, named Test.
Under this menu, a sub menu, Run is available, using which you can run tests within
current context or all tests within current solution.
Please see the screen shot for clear understanding.
Where to see test execution results ?
Like the error list, task list, immediate window or breakpoints window, Test Results window
appear at the bottom of the VS.NET 2008 screen. See screenshot below.
