Share via

How to: Create a Coded UI Test

Using Visual Studio Premium or Visual Studio Ultimate, you can create a coded UI test that can test that the user interface for an application functions correctly. The coded UI test performs actions on the user interface controls for an application and verifies that the correct controls are displayed with the correct values. For more information about which platforms and configurations are supported by coded UI tests, see Supported Configurations and Platforms for Coded UI Tests and Action Recordings.

Creating a coded UI test generates a UIMap object that is specific to your test and represents the windows, controls, parameters, and assertions that are in the UI or that you created during the test recording. You can then perform actions on these UI objects to automate your user interface. For example, you can have your test method click a hyperlink in a Web application, type a value in a text box, or branch off and take different testing actions based on a value in a field.


You can add multiple coded UI tests and multiple UI map objects and files to facilitate testing a large application. For more information, see Testing a Large Application with Multiple UI Maps.

A coded UI test class is identified by a CodedUITestAttribute applied to the class.

Each coded UI test is a test method in a coded UI test class. You can add multiple test methods to each coded UI test class and identify each coded UI test method by using the TestMethodAttribute.

Your test method can also add validation code for a UI test control to obtain the value of a property of a UI test control. The test method can use an Assert statement to compare the actual value of the property to an expected value. The result of this comparison determines the outcome of the test. Every time that you run a coded UI test, you can analyze the test result and if the test fails, you can see or store the details of which assertion failed.

When you create a coded UI test, these files are added to your test project:




Contains the coded UI test class, test methods and assertions.


Contains the XML model for the UIMap class, including all windows, controls, properties, methods, parameters, actions, and assertions.


Contains the code representation of the XML contained in the UIMap.uitest file. Do not edit this file.


Contains more of the code for the UIMap class. You can put any customizations for the UI map in this file.

These assemblies are added as references to your test project:

  • Microsoft.VisualStudio.QualityTools.CodedUITestFramework

  • Microsoft.VisualStudio.QualityTools.UnitTestFramework

  • Microsoft.VisualStudio.TestTools.UITest.Common

  • Microsoft.VisualStudio.TestTools.UITest.Extension

  • Microsoft.VisualStudio.TestTools.UITesting

To Create a Coded UI Test

To create a coded UI test

  1. Perform any one of the following tasks:

    1. In Solution Explorer, right-click a test project, point to Add, and then click Coded UI Test.

    2. In the Test List Editor or the Test View window, right-click the window, and then click New Test. In the Add New Test dialog box, click Coded UI Test and then click OK.

    3. On the Test menu, click New Test. In the Add New Test dialog box, click Coded UI Test and then click OK.

    The Generate Code dialog box appears.

  2. From this dialog box, you can select the method that you want to use to create the UI test controls in your coded UI test:



    Next steps

    Record actions in your application under test, modify the UI map or add assertions

    Click Record actions, edit UI map or add assertions

    How to: Generate a Coded UI Test by Recording the Application Under Test

    Use an existing action recording

    Click Use an existing action recording

    How to: Generate a Coded UI Test from an Action Recording

    All these methods create a coded UI test class, which has a [CodedUITest] attribute, in your test project and opens the file for this class. The class is populated with a test method that has a [TestMethod] attribute. Solution Explorer displays the new test file in your test project.


    If you click Cancel, a coded UI test class is also created. You can manually add code to this class to create your coded UI tests. Or, you can use the other methods that are listed in the table by following the steps in the procedures in the Next Steps column.


The following code example shows a coded UI test class and test method that is assigned a test category value of Priority1. 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 code for the two UI map methods, AddTwoNumbers() and AssertForAdd(), are in the non-editable portion of the UI map and cannot be edited directly. However, you can copy the generated code from the UI map to modify it and create alternate or additional methods in the CodedUITest1 class. See Best Practices for Coded UI Tests for more information.

After you create your coded UI test with specific data, you might want to run your coded UI test several times with different sets of data to test different conditions. To do this you can add parameters from a data source to your coded UI test to create a data-driven coded UI test. For more information, see How to: Create a Data-Driven Coded UI Test.

public class CodedUITest1
    public CodedUITest1()

    [TestCategory("Priority1"), TestMethod]
    public void CodedUITestMethod1()
        // To generate code for this test, select "Generate Code" from 
        // the shortcut menu and select one of the menu items.

See Also





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

Testing a Large Application with Multiple UI Maps