Data Driving Coded UI Tests

Frequently, we have to repeat a test with different data values. This ‘data-driving’ is made very easy in Coded UI Test. In the Tutorial, we created a Coded UI Test to verify the addition of two numbers in the calculator. Let us now see how we can convert it into a data-driven test.

Step 1:- Create the Coded UI Test – See Tutorial


Step 2:- Create the data sets. Coded UI Test supports multiple data sources. The data sets may be defined in a CSV (comma separated values) file, an Excel worksheet, an XML file, database table or from a test case on TFS. For this walkthrough, we will use a CSV file with the following data.














Step 3:- Add the Data Source binding in Coded UI Test.

NOTE: In Visual Studio 11 Beta, the Test View Window and Data Source Wizard are no longer available.

You will have to insert the DataSource attribute directly in the code in the line immediately above your testmethod.

Sample Data Source strings are given below. Copy them to your code and make the necessary customizations.

Data Source Type Data Source attribute
CSV [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]
Excel DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};dbq=|DataDirectory|\\Data.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "Sheet1$", DataAccessMethod.Sequential), TestMethod]
Test Case in TFS [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "https://vlm13261329:8080/tfs/DefaultCollection;Agile", "30", DataAccessMethod.Sequential), TestMethod]

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\data.xml", "Iterations", DataAccessMethod.Sequential), DeploymentItem("data.xml"), TestMethod]

SQL Express

[DataSource("System.Data.SqlClient", "Data Source=.\\sqlexpress;Initial Catalog=tempdb;Integrated Security=True", "Data", DataAccessMethod.Sequential), TestMethod]

Other types See this MSDN article

If you are using Visual Studio 2010, continue with the other actions in Step 3.

If you are using Visual Studio 11 Beta, skip to Step 4.


a. Open the Test View window (from Test -> Windows -> Test View


b. Choose the Coded UI Test that we created and from the context menu click Properties.


c. In the Properties Window click on the button in Data Connection String property to create a new Data Connection.


d. This brings up the New Data source Wizard


e. Choose CSV file and click Next

f. Select the CSV file that we created in Step 2. A preview of the contents is shown in the Wizard.


g. Click Finish. A prompt comes up to add the Data file into the project.


h. Click Yes.

i. A Data Source attribute is added to the Coded UI Test.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]

Step 4:- Use the data in the Coded UI Test.

a. Open the Coded UI Test file. Add the following snippet of code before the invocation of this.UIMap.TestAdd().

this.UIMap.CalulatorWindow.Item7Window.item7Button.SearchProperties[WinProperties.Button.Name] = TestContext.DataRow[“Add1”].ToString();

This modifies the first button click action in the test. It will now search for a button whose value will be picked from the CSV file.

NOTE: Test Context object contains a handle to all the Data that is present in the Data Source. We can reference it with the column name (e.g:- “Add1”)

b. Similarly add the following code snippet which modifies the second button click in calculator

this.UIMap.CalulatorWindow.Item2Window.item2Button.SearchProperties[WinProperties.Button.Name] = TestContext.DataRow[“Add2”].ToString();

c. Add the following line before the invocation of this.UIMap.AssertSum()

this.UIMap.AssertSumExpectedValues.ItemEditText = TestContext.DataRow[“Sum”].ToString();

This now verifies the result of the action from the CSV file.


Step 5:- Run the data driven test.

Right click inside the Coded UI Test Method and choose ‘Run Tests’


The test will run 3 times (as many iterations as there rows in the Data Source). The Test Results will show each iteration details.


You have seen in this walkthrough how to create a data driven tests. If the Test Case is authored in Camano, there is an even simpler way to make it data-driven. More about this workflow in the next article.