Export (0) Print
Expand All

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 abstract DataRow DataRow { get; }
/** @property */
public abstract DataRow get_DataRow ()

public abstract function get DataRow () : DataRow

Property Value

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 example below, or by its index location. Using the sample data table shown in the example section below, an index identfier of DataRow[0] will return the value for the Name column in Table1.

The following code contains the class and method to test.

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;
      }
   }
}

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

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);
      }
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft