20.5. Advanced

Up until now, you have seen how to write and execute unit tests. This section goes on to examine how you can add custom properties to a test case, and how you can use the same framework to test private methods and properties.

20.5.1. Custom Properties

The testing framework provides a number of test attributes that you can apply to a method to record additional information about a test case. This information can be edited via the Properties window and updates the appropriate attributes on the test method. There are times when you want to drive your test methods by specifying your own properties, which can also be set using the Properties window. To do this, add TestProperty attributes to the test method. For example, the following code adds two attributes to the test method to enable you to specify an arbitrary date and an expected status. This might be convenient for ad hoc testing using the Test View and Properties window:

<TestMethod()> _
<TestProperty("SpecialDate", "1/1/2008")> _
<TestProperty("SpecialStatus", "Suspended")> _
Public Sub SpecialCurrentStatusTest()
    Dim target As Subscription = New Subscription

    target.PaidUpTo = CDate(Me.TestContext.Properties.Item("SpecialDate"))
    Dim val As Subscription.Status = _
   CType([Enum].Parse(GetType(Subscription.Status), _
   CStr(Me.TestContext.Properties.Item("SpecialStatus"))), _
             Subscription.Status)

    Assert.AreEqual(val, target.CurrentStatus, _
                    "Correct status not set for Paid up date {0}", target.PaidUpTo)
End Sub

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.