20.1. Your First Test Case

Writing test cases is not a task that is easily automated, as the test cases have to mirror the functionality of the software being developed. However, at several steps in the process code stubs can be generated by a tool. To illustrate this, start with a fairly straightforward snippet of code to learn to write test cases that fully exercise the code. Setting the scene is a Subscription class with a private property called CurrentStatus, which returns the status of the current subscription as an enumeration value:

Public Class Subscription
    Public Enum Status
        Temporary
        Financial
        Unfinancial
        Suspended
End Enum

    Private _PaidUpTo As Nullable(Of Date)

    Public Property PaidUpTo() As Nullable(Of Date)
        Get
            Return _PaidUpTo
        End Get
        Set(ByVal value As Nullable(Of Date))
            _PaidUpTo = value
        End Set
    End Property

    Public ReadOnly Property CurrentStatus() As Status
        Get
            If Not Me.PaidUpTo.HasValue Then Return Status.Temporary
            If Me.PaidUpTo.Value > Now Then
                Return Status.Financial
            Else
                If Me.PaidUpTo >= Now.AddMonths(-3) Then
                    Return Status.Unfinancial
                Else
                    Return Status.Suspended
                End If
            End If
        End Get
    End Property
End Class

As you can see from the code snippet, four code paths need to be tested for the CurrentStatus property. If you were to perform the unit testing manually, you would have to create a separate SubscriptionTest class, either in the same project or in a new project, into which you would manually write code to instantiate a Subscription object, set initial ...

Get Professional Visual Studio® 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.