How to: Configure a Data-Driven Unit Test

You can configure data-driven unit tests in the following ways:

  • Set properties on the test using the Properties window. For more information, see the section "Setting Properties for Data-Driven Unit Tests."

  • In the source code for the unit test, provide a [DataSource] attribute, in which you specify the data that is required to access the data table. For more information, see Coding a Data-Driven Unit Test.

  • In the source code for the unit test, provide a [DataSource] attribute. But instead of using this attribute to supply the data-table access information, specify an entry from the microsoft.visualstudio.TestTools/dataSources section in the application-configuration file for the test assembly. This method gives you the flexibility to change the data-table access information without recompiling the unit test. For more information, see Walkthrough: Using a Configuration File to Define a Data Source.

Setting Properties for Data-Driven Unit Tests

You can configure a data-driven unit test by starting with any existing unit test—either generated or hand-authored—and setting specific properties on the test.

To configure a data-driven unit test

  1. Select the test in Test Manager window or Test View window.

  2. Press F4 to open the Properties window.

    The properties for the unit test are displayed in the Properties window.

  3. For the unit test, set the following properties.

    Data Connection String

    To edit the Data Connection String property, click the property in the Properties window and then click the ellipsis (…). This opens the Choose Data Source dialog box, which lists several possible data sources, including ODBC, Microsoft SQL Server, and Microsoft Access. Choosing a data source opens a dialog box specific to the type of data source; you use this dialog box to configure the connection properties for the data source. When you have finished configuring the data connection, the connection string appears as the value for Data Connection String. This string is also stored as an attribute of the unit test method.

    Note

    Using a plain-text password in your connection string is not secure. Instead, use Windows Authentication if possible. You can also use the DataSourceAttribute together with an application configuration file in which you encrypt the <connectionStrings> section. For more information, see Implementing a Protected Configuration Provider.

    Data Provider Name

    This value is set as a result of your setting the Data Connection String. Therefore, the Data Provider Name property is read-only in the Properties window. However, you can change the Data Provider Name value in an attribute of the unit test method.

    Data Table Name

    You can select a data table after you have established a connection to a data source. When you click the drop-down list in the values column of the Properties window, the tables in the connected database are listed. The table you select from this list is the table whose rows will be retrieved when the unit test is run. As with other properties such as Data Connection String, Data Table Name is stored as an attribute of the unit test method.

    Data Access Method

    For the Data Access Method, select either Sequential or Random; the default value is Sequential. This setting represents the order in which records are retrieved from the table in the data source and is used when the unit test is run repeatedly.

See Also

Tasks

Walkthrough: Using a Configuration File to Define a Data Source

Reference

DataSourceAttribute

Concepts

Overview of Data-Driven Unit Tests
Coding a Data-Driven Unit Test