This article, by no means, is a complete document about how unit testing is done
in Visual Studio 2008. The article does not delve into the details / pros and cons
of unit testing in Visual Studio. It only provides pointers about how to get started
with unit testing using VS.Net 2008. The pointers are in the form of Attributes
(C#) being used under VS.Net 2008. The targeted audience is beginners wanting to
start automated unit testing. Audience are expected to apply their mind and make
use of attributes listed in this article.
As you might already 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, won’t 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 beginners who want to get a feel of
test driven development[
^]
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.
Attributes, like in NUnit, are used to denote various test methods and test classes
UI testing is not supported in the current version of unit testing with VS.NET 2008
You cannot run NUnit test cases from within VS.NET 2008. You will need separate
EXE to do that (The NUnit framework)
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 shown 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.
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()
{
}
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.
Like the error list, task list, immediate window or breakpoints window, Test Results
window appears at the bottom of the VS.NET 2008 screen. See screenshot below.
- NUnit Framework
- Test Driven Community
- Structure
of Unit Tests
-
Unit Testing namespace
- Jan 4, 2010 : Re-Formatted the article.
- Jan 5, 2010 : Inserted Disclaimer, Table of contents and change history.
- Jan 5, 2010 : Added "Go Top" links for better navigation.