More on Data Driving Coded UI Tests

In the previous post, i described how we use a CSV file to data drive a Coded UI Test. The functionality is achieved by adding the following Data Source attribute to Coded UI Test.

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

The Data Source Wizard helps you to construct this attribute and insert it into the code.

 

For using a Excel file as the data source, use the following Data Source attribute.

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]

NOTE: if you are using the Data Source Wizard, you may have to modify the generated attribute definition to match what is shown in the  line above.

IN the above Data Source Attribute, you can change the file to a Excel 2007 file (.xlsx) and it will work.

 

If you hit some issues while data driving, I recommend Troubleshooting Data-Driven Unit Tests. The tips on this page helped me to fix most of the issues i encountered.

 

For Data driving using an XML file, the data source attribute created by the Wizard will work. A sample XML content is shown below.

<?xml version="1.0"?>
<Iterations>

  <Iteration>
    <Add1>6</Add1>
    <Add2>3</Add2>
    <Sum>9</Sum>
  </Iteration>

  <Iteration>
    <Add1>5</Add1>
    <Add2>4</Add2>
    <Sum>9</Sum>
  </Iteration>

</Iterations>

I have two iterations in this XML.

 

Data driving Test Cases authored in Camano

When a test case is authored in Camano, we can data-drive a step by prefixing the parameter with the ampersand sign (@)

See below screenshot of a  data-driven Test Case authored in Camano. Note that @Zip, @FoodType , @Meal, @Within & @Restaurant are used to indicate data driven parameter values. The actual values are entered in the Parameter Values table below.

image

In this case the Data is stored in the Test Case itself.

 

When a Coded UI Test is generated from this Test Case, the following Data Source Attribute is added.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "https://vlm13261329:8080/tfs/DefaultCollection;Agile", "30", DataAccessMethod.Sequential), TestMethod]

This is a new Data Source we have added in Visual Studio 2010. It takes the following arguments

1. https://vlm13261329:8080/tfs/DefaultCollection;Agile – The Team Foundation Server & the Team Project

2. 30 – the Id of the Test Case

 

NOTE: Coded UI Test can be generated from a Test Case only if it has an action recording associated with it. This is done by default when you run the Test in “Start Test & Record” mode in Test Runner.