How to: Create a Data-Driven Coded UI Test

Using Visual Studio Premium or Visual Studio Ultimate, you can create a coded UI test that tests whether the user interface (UI) for an application functions correctly. The coded UI test performs actions on the user interface controls for an application and verifies that specified controls display the correct values. For more information about how to create a coded UI test, see How to: Create a Coded UI Test.

After you create your coded UI test with specified data, you might want to run your coded UI test multiple times with different sets of data to test different conditions. To do this you can add parameter values from a data source to create a data-driven coded UI test. Each row in the data source is an iteration of the coded UI test. The overall result of the test will be based on the outcome for all the iterations. For example, if one test iteration fails, the overall test result is failure.

For more general information about data-driven tests, see How to: Create a Data-Driven Unit Test.

When you generate methods or assertions for your coded UI test by recording them, all constants referred to in the recorded methods are parameterized into parameter classes. For example, if you add a method named AddTwoNumbers, the coded UI testing framework generates another class named AddTwoNumbersParams and adds a field to this class for each constant value that you used when recording the method. If you generate an assertion method named AssertForAdd, the framework generates a class named AssertforAddExpectedValues for the expected values. These classes are generated automatically and are used in the following steps.

All changes to your test to enable it to use a data source for the parameters will be made to your test's source code file (i.e. CodedUITest1.cs). You cannot modify the code in the UIMap.Designer.cs file.

After you have created a coded UI test, you can use the steps in the following procedure to add your data source and parameters to your test. This example assumes that you have these code elements in your solution:

  • A coded UI test class named CodedUITest1.

  • A test method named CodedUITestMethod1().

The test method is for a simple calculator application that adds two numbers and verifies that they are added together correctly for this test to pass.

The data source is a .csv file that contains the following data:













Create the file and add it to your test project.

Create a Data-Driven Coded UI Test

To create a data-driven coded UI test

  1. In Solution Explorer, open the solution that contains your test project.

  2. Click the Test menu, point to Windows, and then click Test View.

    The Test View window that shows your coded UI test opens.

  3. In the Test View window, right-click the coded UI test method, and click Properties.

    The Properties window opens.

  4. To select a data source to use for your coded UI test, click the ellipsis (…) to the right of Data Connection String in the Properties window.

    The New Test Data Source Wizard opens.

  5. Select the type of data source that you want to use. For this example, select the .csv data source type. Click Next and enter the path and file name for your .csv file, then click Finish.

    The data source is now available for you to use in this test method. You use TestContext.DataRow to access the current row of data from your data source. You can access each column using the following syntax:

    string paramVal = TestContext.DataRow["Input1"]
  6. In Solution Explorer, open the CodedUITest1.cs file. Make the following changes to the CodedUITestMethod1() method:

    1. Add the following two lines of code before the call to the AddTwoNumbers method to provide values for the numbers to add.

      this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
      this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
    2. Add the following line of code before the call to the AssertforAdd method to provide the value for the assert method.

      this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 

      This is how the coded UI test method should look with the parameters and the data source added to it:

              "|DataDirectory|\\DataDriven.csv", "DataDriven#csv", 
      public void CodedUITestMethod1()
          // To generate code for this test, select "Generate Code for 
          // Coded UI Test" from the shortcut menu and select one of 
          // the menu items.
          this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
          this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
          this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 
  7. Save the changes to the CodedUITest1.cs source code file.

  8. To run your coded UI test, right-click the coded UI test in the Test View window and click Run Selection.

    After the tests have run, the overall test result for all iterations of the test displays in the Test Results window. To see the details of each iteration, double-click the test in the Test Results window.

See Also


How to: Create a Coded UI Test





How to: Create a Data-Driven Unit Test

Testing the User Interface with Automated UI Tests

Best Practices for Coded UI Tests

Supported Configurations and Platforms for Coded UI Tests and Action Recordings