Coding a Data-Driven Unit Test

A unit test functions as data-driven test if it has the attributes that a data-driven unit test requires. You can assign these attributes and their values either by using the Properties window or by adding the attributes directly to the test's code.

For more information on configuring a unit test as data-driven by editing its properties, see How to: Configure a Data-Driven Unit Test.

This topic describes how to code a unit test as a data-driven unit test, using the DataSource attribute and the TestContext class.

Using Data from a Data Source

When a data-driven unit test is running, data is retrieved from the rows of a data source. The data then is available to the running unit test through the DataRow and DataConnection properties of the TestContext class.

In the following example, DataRow is of the type DataRow, and LastName is the name of a valid column in the row associated with the current iteration of the data-driven test. 

TestContext.DataRow["LastName"]

While LastName refers to a column by name, you can also refer to columns by column number.

For each row in the table, any number of columns can be accessed. You can, for example, retrieve several columns of data at once, use them in a calculation, and then compare the result with a final column that contains an expected return value.

Coding a Data-Driven Unit Test

To create a data-driven unit test, you can start with either a unit test that you have created by hand or a generated unit test. For more information, see How to: Author a Unit Test and How to: Generate a Unit Test.

To configure your existing unit test, add attributes that define the data source you want it to use, the way you want that data to be accessed, and the table whose rows you want your test to use as input. For more information on configuring these attributes, see How to: Configure a Data-Driven Unit Test.

For example, the following code is from a data-driven unit test that uses data from the Northwind database.

namespace TestProject1
{
    [TestClass]
    public class TestClass
    {
        private TestContext m_testContext;
        public TestContext TestContext
        {
            get { return m_testContext; }
            set { m_testContext = value; }
        }
        [TestMethod]
        [DeploymentItem("FPNWIND.MDB")]
        [DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"FPNWIND.MDB\"", "Employees", DataAccessMethod.Sequential)]
        public void TestMethod()
        {
            Console.WriteLine( "EmployeeID: {0}, LastName: {1}", TestContext.DataRow["EmployeeID"],  TestContext.DataRow["LastName"] );
        }
    }
}

The code within the test method in this example uses values from the LastName and EmployeeID columns in the "Employees" table of the data source. The test method accesses these values through a TestContext property, which is defined in the test class that contains the method.

See Also

Tasks

How to: Author a Unit Test
How to: Generate a Unit Test
How to: Configure a Data-Driven Unit Test

Reference

DataSourceAttribute
TryUrlRedirection

Concepts

Overview of Data-Driven Unit Tests
Structure of Unit Tests