Edit

Test WinUI apps built with the Windows App SDK

In this topic we provide some recommendations for how to test and validate functionality in apps created with the Windows App SDK using WinUI 3 user interface (UI) features. Testing is an essential part of the app development process—it helps you catch bugs early, maintain code quality, and ensure a reliable user experience as your app evolves. By incorporating unit tests into your workflow, you can confidently refactor code, add new features, and ship updates knowing that existing functionality continues to work as expected.

Tutorial: Create a WinUI 3 unit test project.

Most object types under the Microsoft.UI.Xaml namespaces must be used from a UI thread in a XAML application process. (For details on testing apps created with Windows App SDK that don't use WinUI 3, see the following section, Testing non-WinUI functionality.)

Note

We recommend that you refactor any code to be tested by pulling it out of the main app project and placing it into a library project. Both your app project and your unit test project can then reference that library project. This section describes how to create unit tests for WinUI 3 apps in Visual Studio using the built-in unit test project templates.

Note

The unit test app described here is written in the context of a WinUI 3 application. This is required for any tests that execute code requiring the XAML runtime. This project will create a XAML UI Thread and execute the tests.

In this tutorial, you learn how to:

  • Create a WinUI Unit Test App project in Visual Studio.
  • Use the Visual Studio Test Explorer.
  • Add a WinUI Class Library project for testing.
  • Run tests with the Visual Studio Test Explorer.

Prerequisites

You must have Visual Studio installed and setup for WinUI development. See Quick start: Set up your environment and create a WinUI 3 project.

Create a WinUI Unit Test App project

To start, create a unit test project. The project type comes with all the template files you need.

  1. Open Visual Studio and select Create a new project in the Start window.

    Screenshot of the Visual Studio start window.

  2. In the Create a new project window, filter projects to C#, Windows, and WinUI, select the WinUI Unit Test App template, and then select Next

    Screenshot of the Visual Studio 'Create a new project' window.

  3. [Optional] On the Configure your new project window, change the Project name, Solution name (uncheck Place solution and project in the same directory) and the Location of your project.

  4. Select Create.

Run tests with Test Explorer

When you create the test project, your tests appear in Test Explorer, which is used to run your unit tests. You can also group tests into categories, filter the test list, create, save, and run playlists of tests, debug unit tests and (in Visual Studio Enterprise) analyze code coverage.

The UnitTests.cs file contains the source code for the unit tests used by Test Explorer. By default, the basic sample tests shown here are created automatically:

namespace WinUITest1
{
   [TestClass]
   public class UnitTest1
   {
      [TestMethod]
      public void TestMethod1()
      {
         Assert.AreEqual(0, 0);
      }

      // Use the UITestMethod attribute for tests that need to run on the UI thread.
      [UITestMethod]
      public void TestMethod2()
      {
         var grid = new Grid();
         Assert.AreEqual(0, grid.MinWidth);
      }
   }
}
  1. If you haven't done so already, build your solution. This will allow Visual Studio to "discover" all available tests.

  2. Open Test Explorer. If it's not visible, open the Test menu, and then choose