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 Data-Driven Unit Tests.
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.
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
Select the test in the Test List Editor or Test View window.
Press F4 to open the Properties window.
The properties for the unit test are displayed in the Properties window.
Click the ellipsis button (...) in the row for Data Connection String.
The New Test Data Source Wizard is displayed.
Choose Database, CSV File, or XML File, and then click Next.
Respond to the wizard's prompts to choose the database connection string, CSV file, or XML file to use as your data source. For databases and for XML files, you also select the table that supplies the data to use in your data-driven unit test.
(Optional) Preview the data from your data source in the Preview data pane.
Click Yes if you see a prompt that asks Copy the database file into the current project and add as deployment item?
The data source for your data-driven unit test is now configured. The properties that define your data source are displayed in the Properties window.
(Optional) You can change the configuration of your data-driven unit test by changing the values of 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 re-opens the New Test Data Source Wizard, with which you can specify a new data source as you did in step 4. 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.
Using a plain-text password in your connection string is not secure. Instead, use Windows Authentication if you can. 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 the DataSource attribute of the unit test method.
Data Table Name
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.