Condividi tramite


Proprietà TestContext.DataRow

Una volta sottoposto a override in una classe derivata, ottiene la riga di dati corrente quando il test viene utilizzato per test basato sui dati.

Spazio dei nomi:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

Sintassi

'Dichiarazione
Public MustOverride ReadOnly Property DataRow As DataRow
public abstract DataRow DataRow { get; }
public:
virtual property DataRow^ DataRow {
    DataRow^ get () abstract;
}
abstract DataRow : DataRow with get
abstract function get DataRow () : DataRow

Valore proprietà

Tipo: System.Data.DataRow
In DataRow oggetto.

Note

DataRow la proprietà restituisce i valori della riga corrente dalla colonna.Verranno restituite i dati identificati dalla colonna.La colonna viene identificata tramite nome, come nell'esempio seguente, o dalla posizione di indice.Utilizzando la tabella di dati di esempio illustrata nella sezione seguente di esempio, un identificatore di indice di DataRow [0] restituirà il valore per la colonna name in Table1.

Esempi

Il codice seguente contiene la classe e il metodo da testare.

using System;

namespace BankAccountNS
{
   public class BankAccount
   {
      private string m_customerName;
      private double m_balance;

      public BankAccount(string customerName, double balance)
      {
         m_customerName = customerName;
         m_balance = balance;
      }

      public double Balance
      { get { return m_balance; } }

      public void Debit(double amount)
      {
         if (amount < 0)
            throw new ArgumentOutOfRangeException("amount");
         m_balance -= amount;
      }
   }
}
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

Il successivo test avrà esito positivo.Utilizza il database di accesso di sample.mdb che contiene Table1 con i dati seguenti.

nome

bilanciamento

importo

Becky

100

25

John

70

60

Larry

75

71.25

Stephanie

159

158

Mr.Bryan Walton

11.99

11.22

using Microsoft.VisualStudio.TestTools.UnitTesting;
using BankAccountNS;
using System;

namespace TestProject1
{
   [TestClass()]
   public class BankAccountTest
   {
      private TestContext testContextInstance;
      public TestContext TestContext
      {
         get { return testContextInstance; }
         set { testContextInstance = value; }
      }

      [TestMethod()]
      [DataSource("System.Data.OleDb",
         "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"D:\\sample.mdb\"",
         "Table1",
         DataAccessMethod.Sequential)]
      public void DebitTest()
      {
         string customerName = testContextInstance.DataRow["Name"].ToString();

         double balance = Convert.ToDouble(testContextInstance.DataRow["Balance"]);
         double amount = Convert.ToDouble(testContextInstance.DataRow["Amount"]); 

         double newBalance = balance - amount;

         BankAccount target = new BankAccount(customerName, balance);
         target.Debit(amount);

         Assert.AreEqual(newBalance, target.Balance, .00);
      }
   }
}
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

Sicurezza di .NET Framework

Vedere anche

Riferimenti

TestContext Classe

Spazio dei nomi Microsoft.VisualStudio.TestTools.UnitTesting

Altre risorse

Procedura: creare uno unit test basato sui dati

How to: Configure a Data-Driven Unit Test

Using the TestContext Class