Export (0) Print
Expand All

How to: Create a Data-Driven Coded UI Test

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 Creating Coded UI Tests.

Requirements

  • Visual Studio Ultimate, Visual Studio Premium

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.

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:

Input1

Input2

ExpectedResult

3

4

7

5

6

11

1200

24000

25200

Create the file and add it to your coded UI test project.

To create a data-driven coded UI test

  1. In Solution Explorer, open the solution that contains your coded UI test project and then open the coded UI test for editing.

  2. Insert the DataSource attribute directly in the code, in the line immediately above your test method. You can use the sample data source strings in the table below by copying them to your code and make the necessary customizations.

    Date 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 Team Foundation Server

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

    XML

    [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]
    

    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"]
    
  3. 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 = 
          TestContext.DataRow["Input1"].ToString();
      this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
          TestContext.DataRow["Input2"].ToString();
      
    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 = 
          TestContext.DataRow["ExpectedResult"].ToString();
      

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

      [DeploymentItem("DataDriven.csv"), 
          DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", 
              "|DataDirectory|\\DataDriven.csv", "DataDriven#csv", 
              DataAccessMethod.Sequential), 
          TestMethod]
      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 = 
              TestContext.DataRow["Input1"].ToString();
          this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
              TestContext.DataRow["Input2"].ToString();
          this.UIMap.AddTwoNumbers();
      
          this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 
              TestContext.DataRow["ExpectedResult"].ToString();
          this.UIMap.AssertforAdd();
      }
      
  4. Save the changes to the CodedUITest1.cs source code file.

  5. To run your coded UI test, right-click the coded UI test in the code editor and choose Run Unit Tests.

    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.

Guidance

For additional information, see Testing for Continuous Delivery with Visual Studio 2012 – Chapter 2: Unit Testing: Testing the Inside and Testing for Continuous Delivery with Visual Studio 2012 – Chapter 5: Automating System Tests

Show:
© 2014 Microsoft