Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Unit Testing starter - VS.NET 2008

0.00/5 (No votes)
4 Jan 2010 1  
Article to get started with unit testing in VS.NET 2008

Unit testing starter - VS.NET 2008

Table of contents



Disclaimer

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.

Introduction

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[^]

What to expect while going for a new Unit Test

  • 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)

  • 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 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.

    Other attributes

    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 appears at the bottom of the VS.NET 2008 screen. See screenshot below.

    test_results_-_unit_testing_vsnet_2008.jpg


    Further Reading

    1. NUnit Framework
    2. Test Driven Community
    3. Structure of Unit Tests
    4. Unit Testing namespace


    Change History

    • 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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here