共用方式為


TestContext.DataRow 屬性

在衍生類別中覆寫時,會在使用測試進行資料驅動型測試時取得目前的資料列。

命名空間:  Microsoft.VisualStudio.TestTools.UnitTesting
組件:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)

語法

'宣告
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

屬性值

型別:System.Data.DataRow
DataRow 物件。

備註

DataRow 屬性會傳回目前資料行中每個資料列的值。 這將傳回由資料行所識別的資料。 資料行是由名稱 (如以下範例所示) 或是其索引位置識別。 如果使用以下範例區段中所顯示的資料表範例,DataRow[0] 的索引識別項將會傳回 Table1 中 Name 資料行的值。

範例

下列程式碼包含要測試的類別和方法。

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

下列測試將會通過。 它使用名為 sample.mdb 的 Access 資料庫,其中資料表 Table1 包含以下資料。

名稱

Balance

Amount

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

.NET Framework 安全性

請參閱

參考

TestContext 類別

Microsoft.VisualStudio.TestTools.UnitTesting 命名空間

其他資源

如何:建立資料驅動型單元測試

How to: Configure a Data-Driven Unit Test

Using the TestContext Class