5,401,186 members and growing! (18,645 online)
Email Password   helpLost your password?
General Programming » Programming Tips » Testing and Quality Assurance     Beginner License: The Code Project Open License (CPOL)

Unit Testing starter - VS.NET 2008

By coolestCoder

Article to get started with unit testing in VS.NET 2008
C# (C# 3.0, C#), VB (VB 9.0, VB), Windows, Visual Studio (Visual Studio, VS2008), Dev

Posted: 25 Jan 2008
Updated: 25 Jan 2008
Views: 10,191
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 2.02 Rating: 2.12 out of 5
4 votes, 44.4%
1
3 votes, 33.3%
2
0 votes, 0.0%
3
1 vote, 11.1%
4
1 vote, 11.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Unit Testing starter - VS.NET 2008

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.
Test_Menu_-_unit_testing_vsnet_2008.JPG


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.

Test_Results_-_unit_testing_vsnet_2008.JPG

Further Reading

NUnit Framework

Test Driven Community

Structure of Unit Tests

Unit Testing namespace

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

coolestCoder


coolestCoder is working at ITCube Solutions Pvt. Ltd., India. He has total experience of 2+ years developing various types of .Net applications, viz. Windows Service, Web Application and at present working on Windows Forms applications. He likes to face challenges while coding and always ready to help others. He would like to see himself as a Technical Architect in near future. His main interests lies in understanding the internal of any program / component / technology.
He loves to read articles related to new topics, for example, Framework 3.5, LINQ, etc.....
Occupation: Software Developer
Company: ITC
Location: India India

Other popular Programming Tips articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralReduce width of last picture!memberamx30003:53 25 Jan '08  
GeneralRe: Reduce width of last picture!membercoolestCoder5:11 25 Jan '08  
GeneralScrollbarmemberN a v a n e e t h2:51 25 Jan '08  
GeneralRe: ScrollbarmembercoolestCoder5:13 25 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jan 2008
Editor:
Copyright 2008 by coolestCoder
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project