oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Unit Testing in Visual Studio 2005 Team System

by Wei-Meng Lee
Use ClickOnce to Deploy Windows Applications

Unit testing is one of the tasks that every programmer worth their salt needs to do. For example, after writing a class, you often need to write additional code to instantiate the class and test the various methods and properties defined within it. With Visual Studio 2005 Team System, you can now use the new Unit Testing feature to auto-generate the code needed to test your application.

In this article, I will show you how unit testing is performed in Visual Studio 2005 Team System. Note that you need the Microsoft Visual Studio 2005 Beta 2 Team Edition to try the example in this article.

Creating the Application

Launch Visual Studio 2005 and create a new Windows project using the Visual Basic 2005 language. Name the project UnitTesting.

Let's start by first adding a class to the project. Right-click on the project name in Solution Explorer and select Add->New Item…. Select the Class template and name it as Point.vb.

Populate the Point.vb file with the following class definition:

Public Class Point
    Private pX, pY As Integer

    '---default constructor
    Public Sub New()

    End Sub

    '---constructor with two parameters
    Public Sub New(ByVal x As Integer, ByVal y As Integer)
        pX = x
        pY = y
    End Sub

    '---sets the X coordinate
    Property x() As Integer 
            Return pX
        End Get
        Set(ByVal Value As Integer)
            pX = Value
        End Set
    End Property

    '---sets the Y coordinate
    Property y() As Integer 
            Return pY
        End Get
        Set(ByVal Value As Integer)
            pY = Value
        End Set
    End Property

    '---calculates the length between 2 points
    Public Function length(ByVal pointOne As Point) As Single
        Return Math.Sqrt(Math.Pow(pX - pointOne.x, 2) + _
               Math.Pow(pY - pointOne.y, 2))
    End Function
End Class

This class represents points on a two-dimension system, with x and y coordinates and a method to calculate the distance between two points.

Visual Studio Hacks

Related Reading

Visual Studio Hacks
Tips & Tools for Turbocharging the IDE
By James Avery

Pages: 1, 2, 3, 4

Next Pagearrow