CUIT: Getting Started with Data Driven Tests

Data Driven CUITs allow you to record a set number of steps and then use a data source as a list of parameters to change the values that are either chosen, entered, or have been asserted in a test. If numerous sets of parameters are supplied, the test will run itself multiple overs using the list of supplied parameters.

Since this is my first attempt at creating a Data Driven variation of CUIT, I created a simple CUIT which contains two methods. I used Windows Calculator as my test dummy in this. The first method adds 1 + 2 by clicking ‘1’  -> ‘+’ -> ‘2’ -> ‘=’. The second method asserts that 1 + 2 equates to 3.

The process can be summed up in about 3 (relatively) simple steps

  1. Create a data set
  2. Add the data set to a test method
  3. Map the test values from the Data Set

Requirement
A using statement must be added when dealing with Data Driven tests

using Microsoft.VisualStudio.TestTools.UITesting.WinControls;

Creating the Data Set

In order to create the file to be  used, simply add a new text file to the solution by right clicking on it. The file will need a .csv extension (Yes, add a text file to the solution and during creation append .csv instead of .txt). Once the file has been created, a table of values can be created. For each parameter that you want to supply a new value for, add a new column for it. For each new repetition of the test, a new row of values to fill the columns is needed.

On my test there are three parameters that I want to give new values to. These values are: The first number pressed, the second number pressed, and the value asserted. This means that my data file will need to include columns for those parameters. If I want the test to run 3 times, then the file will need 3 rows of data.

This is what the contents of the file would look like:
Num1, Num2, Sum
2, 3, 5
3, 4, 7
4, 5, 9

Add the Data Set to a TestMethod

So far there’s only a file with the values we wish to use for the parameters. Technically, the file could be used for multiple Test Methods. In order to tell the test which method we wish to associate it with, a Data attribute must be appended to the Test method attribute.

[DataSource(“Microsoft.VisualStudio.TestTools.DataSource.CSV”, @”c:\users\phillipwolf\documents\visual studio 2013\Projects\CodedUITestProject1\CodedUITestProject1\data.csv”, “data#csv”, DataAccessMethod.Sequential), DeploymentItem(“data.csv”), TestMethod]

In the DataSource attribute, the first string determines the type of data source, the second is the file path (which requires an @ symbol due to the spaces), and I’m not entirely sure what the last one is yet aside from knowing to match it to the file’s name. I found the DataSourceAttribute class help page and those constructors don’t seem to match these.

If the test was ran at this moment, with only the Data Source have being added – The test would run through three times, but with the default values each time. This is because the test realizes that there are three rows of values in my data source but it doesn’t know what parameters to match to which column.

Map the Values from the Data Set

Before the methods run in the Test Method, the values on the UIMap that the methods in the test use need to be mapped to the correct columns in the data set.

Currently, my UIMap looks like this

1

So my three values to map where the first number button click, the second number button click, and the value asserted.

The First Value

According my UIMap, UIItem1Button is the object that selects the ‘1’ button (Under AddNumbers there’s the actions taken during recording. If you select one of those options, the code equivalent will highlight on the right side). Objects that interact with the GUI use the properties of the item on the screen in order to find them. This means that we need to change the search properties of UIItem1Button.

this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow[“Num1”].ToString();

The first part is pretty easy to figure out. UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button is directly from the UIMap image above. SearchProperties[] is then a property of the button which the test uses to find the control on the application. The controls property is then placed within the brackets, in this case the name will be replaced. After the = contains the column reference via TestContext.DataRow[“NameOfTheColumn”].ToString(); This makes the Search Properties equal to the respective column .

The Second Value

The second number button would have it’s parameters set exactly the same way as the first number button

this.UIMap.UICalculatorWindow.UIItemWindow2.UIItem2Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow[“Num2”].ToString();

The Assertion

The assertion throws in a curveball and you don’t use the UICalculatorWindow on this one. Instead, assertions have a class created called assertionNameExpectedValues and then the name of the text control.

this.UIMap.ValidateSumExpectedValues.UIItem3TextDisplayText = TestContext.DataRow[“Sum”].ToString();

After the equals follows the same pattern as before for mapping the correct column’s values.

Running the test now will run it three times in a row, adding the first two column values from the data source and then asserting the third column.

The final product of the code looks like

[DataSource(“Microsoft.VisualStudio.TestTools.DataSource.CSV”, @”c:\users\phillipwolf\documents\visual studio 2013\Projects\CodedUITestProject1\CodedUITestProject1\data.csv”, “data#csv”, DataAccessMethod.Sequential), DeploymentItem(“data.csv”), 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.UICalculatorWindow.UIItemWindow.UIItem1Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow[“Num1”].ToString();

this.UIMap.UICalculatorWindow.UIItemWindow2.UIItem2Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow[“Num2”].ToString();

this.UIMap.ValidateSumExpectedValues.UIItem3TextDisplayText = TestContext.DataRow[“Sum”].ToString();

this.UIMap.AddNumbers();

this.UIMap.ValidateSum();

}

Advertisements

One thought on “CUIT: Getting Started with Data Driven Tests”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s