How to run a test many times with data read from .csv file (data driving)

The web has many tutorials on data driving Coded UI tests. The basic steps for data driving with a CSV file are as follows.

  • Create the CSV file.
  • Add the CSV file to the project.
  • Make sure the CSV file is deployed.
  • Add the CSV file as a data source for an individual test.
  • Read the CSV fields and use them in the test.

The detailed steps, with some variations, are explained below.

Visual Studio 2010 has a “data source wizard” that does some of these steps. Visual Studio versions 2012 and 2013 do not have the wizard and so all the steps have to be done manually.

Create the CSV file

One way is to create the file in a spreadsheet then save it as Comma Separated Values. Another way is to use a text editor and just write the file. I use a spreadsheet program for big data source files and a text editor for creating small files. Some editors add a byte order mark (BOM) at the start of a file, that will be added to the first field name of the CSV which appears to make the field unreadable. See this page for more about the BOM.

Add the CSV file to the project

Use the context menu in solution explorer, select Add -> Existing Item. Then browse to the required file. Note the file filter will probably need to be altered to be *.* or *.csv.

Make sure the CSV file is deployed

Open the properties panel for the CSV file from solution explorer. Set “Copy to output directory” to “Copy if newer” or to “Copy always“. Some documents recommend “Copy if newer” but I prefer “Copy always” as occasionally a file was not copied as I expected. The difference between the two copy methods is a little disk space and a little time, but disks are normally big and the time to copy is normally small. Any savings are, in my opinion, far outweighed by being sure that the file will be copied correctly.

Add the CSV file as a data source for an individual test

Replace the [TestMethod] attribute with the correct data source line. This Microsoft blog shows the replacement code for several possible data source file types. For CSV use:

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

Note that the file name occurs three times and one copy has a # rather than a .. I have not found any useful documentation about the different fields of the Datasource(...) attribute so cannot advise further on how to choose values for non-CSV data sources.

The |DataDirectory| part above is replaced by the directory where files are deployed when the tests run. The whole file name within the string quotes could be replaced by a full path name of a file, if required.

Read the CSV fields and use them in the test

The Coded UI record and generate tool creates classes with fields that hold values entered into text boxes or used in assertions. Each action method has a ...Params class and each assert method has an ...ExpectedValues class, where the ... is the method name. The default values of these fields are the values used when the test was recorded. The recorded values can be overwritten by an assignment before the action or assertion method is called. The fields of the current row of the data source are accessed from TestContext.DataRow[...].

Suppose a Coded UI test has an EnterValue method that writes text into two fields of the screen and it also has a CheckResult method that asserts one field. The test method might then be written as follows.

[DataSource...
    TestMethod]
public void CodedUITestMethod1()
{
    this.UIMap.EnterValueParams.UIItem0TextSendKeys = TestContext.DataRow["ValueOne"].ToString();
    this.UIMap.EnterValueParams.UIItem1TextSendKeys = TestContext.DataRow["ValueTwo"].ToString();
    this.UIMap.EnterValue();

    this.UIMap.CheckResultExpectedValues.UIItem0TextDisplayText = TestContext.DataRow["Result"].ToString();
    this.UIMap.CheckResult();
}

The ...Params and ...ExpectedValues classes allow the test to create values when the test runs. For example, if the EnterValue method also wanted to write tomorrow’s date into a field we could add the following line before it is called:

this.UIMap.EnterValueParams.UIItem2TextSendKeys = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");

Leave a Comment