Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TestContext.DataRow Property

 

When overridden in a derived class, gets the current data row when test is used for data driven testing.

Namespace:   Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

Public MustOverride ReadOnly Property DataRow As DataRow

Property Value

Type: System.Data.DataRow

A DataRow object.

The DataRow property returns values for the current row by column. This will return data identified by column. The column is identified either by name, as in the following example, or by its index location. Using the sample data table shown in following example section, an index identifier of DataRow[0] will return the value for the Name column in Table1.

Legacy Code Example

The following code contains the class and method to test.

Imports System

Namespace BankAccountNS
   Public Class BankAccount
      Private m_customerName As String
      Private m_balance As Double

      Public Sub New(ByVal customerName As String, ByVal balance As Double)
         m_customerName = customerName
         m_balance = balance
      End Sub

      Public ReadOnly Property Balance() As Double
         Get
            Return m_balance
         End Get
      End Property

        Public Sub Debit(ByVal amount As Double)
            If amount < 0 Then
                Throw New ArgumentOutOfRangeException("amount")
            End If
            m_balance -= amount
        End Sub
    End Class
End Namespace

The following test will pass. It uses the sample.mdb access database that contains Table1 with the following data.

Name

Balance

Amount

Becky

100

25

John

70

60

Larry

75

71.25

Stephanie

159

158

Mr. Bryan Walton

11.99

11.22

Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports BankAccountNS
Imports System

Namespace TestProject1
   <TestClass()> _
   Public Class BankAccountTest
      Private testContextInstance As TestContext
      Public Property TestContext() As TestContext
         Get
            Return testContextInstance
         End Get
         Set(ByVal Value As TestContext)
            testContextInstance = Value
         End Set
      End Property

      <TestMethod()> _
      <DataSource("System.Data.OleDb", _
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""D:\ sample.mdb""", _
      "Table1", DataAccessMethod.Sequential)> _
      Public Sub DebitTest()
         Dim customerName As String = testContextInstance.DataRow("Name").ToString()

         Dim balance As Double = Convert.ToDouble(testContextInstance.DataRow("Balance"))
         Dim amount As Double = Convert.ToDouble(testContextInstance.DataRow("Amount"))

         Dim NewBalance As Double = balance - amount

         Dim target As BankAccount = New BankAccount(customerName, balance)
         target.Debit(amount)

         Assert.AreEqual(NewBalance, target.Balance, 0.0)
      End Sub
   End Class
End Namespace
Return to top
Show: