Testing a Windows application is not limited to checking whether individual methods return the expected results. A WinUI application also depends on its UI thread, application lifecycle, XAML controls, window activation, and Windows App SDK components. These requirements make testing Windows desktop applications different from testing a regular .NET class library.
MSTest is a popular testing framework for .NET applications. Microsoft.Testing.Platform (MTP) provides the test execution infrastructure that runs tests and reports their results. Together, they provide a way to test UWP and WinUI 3 applications while keeping the application’s actual UI dispatcher and process lifecycle available to the tests.
The important development is that recent MSTest and Microsoft.Testing.Platform versions support several Windows application models, including packaged and unpackaged WinUI 3 applications. MSTest 4.5.1 and Microsoft.Testing.Platform 2.5.1 provide the updated support described in Microsoft's October 2026 guidance.
This article explains how the two testing components work together, how to configure a WinUI test project, how to write UI-thread tests, and how to execute tests locally and in a CI environment.
Understanding MSTest and Microsoft.Testing.Platform
MSTest and Microsoft.Testing.Platform have different responsibilities.
MSTest is the testing framework. It provides attributes, assertions, test lifecycle methods, data-driven tests, and test organization features.
Microsoft.Testing.Platform is the test execution platform. It provides the infrastructure required to discover and execute tests, handle command-line arguments, produce test results, and integrate with development and CI tools.
The MSTest SDK connects these components and simplifies project configuration.
What is MSTest?
MSTest is Microsoft's testing framework for .NET applications. Developers use it to create unit tests and verify application behavior.
A basic MSTest class looks like this:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CalculatorTests
{
[TestMethod]
public void Add_TwoNumbers_ReturnsExpectedResult()
{
// Arrange
int firstNumber = 10;
int secondNumber = 20;
// Act
int result = firstNumber + secondNumber;
// Assert
Assert.AreEqual(30, result);
}
}
The [TestClass] attribute identifies a class containing tests, while [TestMethod] identifies an individual test.
MSTest also provides attributes such as [TestInitialize], [TestCleanup], [ClassInitialize], and [DataTestMethod] to support different testing requirements.
What is Microsoft.Testing.Platform?
Microsoft.Testing.Platform is a lightweight, extensible test platform designed to execute .NET tests.
It provides capabilities such as:
- Test execution and discovery.
- Command-line configuration.
- Test result reporting.
- Extensions for additional test functionality.
- Integration with build and CI workflows.
- Support for custom test hosts and application models.
One important difference is that Microsoft.Testing.Platform can run inside the application process. This is useful for WinUI because the application itself owns the UI thread and dispatcher.
In a traditional test-host arrangement, the test runner and the application can be separate processes. A test that needs to create or access WinUI controls then has to deal with the application's threading and lifecycle requirements.
With the MTP approach described here, the WinUI application can host the test platform directly. This allows UI-related tests to execute in the process that owns the actual UI dispatcher.
Why WinUI Applications Need Special Testing Support
WinUI 3 uses the Windows App SDK and XAML-based UI components. Many types under the Microsoft.UI.Xaml namespace must be created and accessed from the application's UI thread.
Consider this example:
var button = new Microsoft.UI.Xaml.Controls.Button();
This code looks simple, but creating a WinUI control from an ordinary background thread can cause a threading exception.
A normal .NET unit test does not automatically run on the UI thread of a WinUI application. Creating a separate STA thread is also not sufficient in every situation because WinUI requires the application's actual UI dispatcher.
This distinction matters when testing:
- XAML controls and layouts.
- Window creation and activation.
- Dispatcher-related operations.
- UI-bound application services.
- Asynchronous operations that interact with UI components.
- Components that depend on the WinUI application lifecycle.
MSTest provides the [UITestMethod] attribute for tests that need access to the WinUI UI thread.
TestMethod versus UITestMethod
Use [TestMethod] for tests that do not require WinUI objects or the UI dispatcher.
Use [UITestMethod] when the test needs to create or access WinUI objects on the application's UI thread.
For example:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.UI.Xaml.Controls;
using System.Threading.Tasks;
[TestClass]
public class WinUIControlTests
{
[TestMethod]
public void BasicCalculation_ReturnsExpectedValue()
{
int result = 10 + 20;
Assert.AreEqual(30, result);
}
[UITestMethod]
public async Task ButtonCanBeCreatedOnUIThread()
{
await Task.Yield();
var button = new Button
{
Content = "Save"
};
Assert.IsNotNull(button);
}
}
The first test does not need the UI thread. The second test creates a WinUI control and therefore uses [UITestMethod].
The asynchronous UI test also demonstrates that the test can continue after an asynchronous operation while retaining the UI-thread testing context.
Do not use [UITestMethod] for every test simply because the application is built with WinUI. Keeping business logic tests independent of the UI makes them easier to execute and maintain.
Understand the Supported Windows Application Models
MSTest's updated Microsoft.Testing.Platform integration supports several Windows application models. The correct configuration depends on how the application is packaged and hosted.
| Application model | Packaging | Test execution approach |
|---|
| Classic UWP | Packaged AppContainer | MTP sidecar registers and activates the application |
| Modern UWP | Packaged AppContainer with .NET 10 | MTP sidecar activates the application |
| WinUI 3 packaged full-trust | MSIX packaged | Packaged application launcher activates the test host |
| WinUI 3 unpackaged | Regular Windows executable | MTP launches the executable directly |
| WinUI 3 AppContainer | Packaged AppContainer | MTP sidecar handles package activation and communication |
These models should not be treated as interchangeable.
For example, an unpackaged WinUI application can start as a regular executable. A packaged application has an MSIX identity and must be registered and activated using its application identity.
MSTest.Sdk handles much of this application-model configuration, but the machine still needs the correct Windows development tools, runtime components, and package registration permissions.
Configure MSTest With Microsoft.Testing.Platform
For a new WinUI test project, use the MSTest SDK rather than manually adding every test infrastructure package.
The following setup uses the MSTest 4.5.1 toolchain and Microsoft.Testing.Platform 2.5.1.
Pin the MSTest SDK version
At the repository root, create or update global.json:
{
"test": {
"runner": "Microsoft.Testing.Platform"
},
"msbuild-sdks": {
"MSTest.Sdk": "4.5.1"
}
}
This configuration selects Microsoft.Testing.Platform as the test runner and pins the MSTest SDK version.
Version alignment is important. If you manage MTP packages and extensions directly rather than relying on MSTest.Sdk, keep their versions compatible.
The .NET 10 test experience can use different runners depending on the project configuration. Explicitly selecting Microsoft.Testing.Platform avoids assuming that dotnet test will automatically use the intended runner.
Create a WinUI test project
For a WinUI 3 test application, the project needs a Windows-specific target framework and the Windows App SDK configuration.
A simplified project file can look like this:
<Project Sdk="MSTest.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<UseWinUI>true</UseWinUI>
<OutputType>WinExe</OutputType>
</PropertyGroup>
</Project>
This example represents a WinUI 3 project using the MSTest SDK. A real project must also include the appropriate Windows App SDK references, application files, manifest configuration, and deployment settings for its chosen packaging model.
For a packaged application, retain the required package manifest and packaging configuration. For an unpackaged application, configure the project accordingly:
<PropertyGroup>
<WindowsPackageType>None</WindowsPackageType>
</PropertyGroup>
The unpackaged configuration allows the test application to start as a normal executable. It does not require an MSIX identity for launching.
Do not copy a simplified project file over an existing WinUI application without reviewing its Windows App SDK version, target architecture, runtime identifiers, and packaging settings.
Host Microsoft.Testing.Platform Inside a WinUI Application
A WinUI test application needs to provide the UI environment before running tests that depend on it.
The application creates and activates its window, publishes the window's dispatcher queue, and then starts the testing platform.
A simplified example of the important startup operations is:
using Microsoft.UI.Xaml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public partial class App : Application
{
private Window? _window;
protected override async void OnLaunched(
LaunchActivatedEventArgs args)
{
_window = new Window();
_window.Activate();
UITestMethodAttribute.DispatcherQueue =
_window.DispatcherQueue;
Environment.ExitCode =
await MicrosoftTestingPlatformApplication.RunAsync(
Environment.GetCommandLineArgs()[1..]);
_window.Close();
Exit();
}
}
This illustrates the main lifecycle responsibilities:
- Create the WinUI window.
- Activate the window.
- Publish its dispatcher queue for UI-thread tests.
- Start Microsoft.Testing.Platform.
- Store the returned test result in the process exit code.
- Close the window and exit after test execution completes.
This is a focused illustration of the startup pattern, not a complete replacement for the generated or sample application setup. The exact activation-argument handling depends on the application model. For example, modern UWP requires special handling of its activation string, while WinUI 3 uses normal process arguments.
For a self-hosted WinUI test application, do not add the separate WinUITestTarget attribute. That attribute starts a WinUI application for a separate test host, which conflicts with an application that has already started its own WinUI environment.
Write and Organize WinUI Tests
A good testing strategy separates application logic from UI behavior.
Business rules, validation, calculations, and data transformations should normally be tested without creating WinUI controls. UI-thread tests should focus on behavior that actually depends on the dispatcher or XAML environment.
Test a WinUI control
The following example verifies that a control can be created and configured:
using Microsoft.UI.Xaml.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ButtonTests
{
[UITestMethod]
public void Button_CanBeCreatedWithExpectedContent()
{
var button = new Button
{
Content = "Submit"
};
Assert.IsNotNull(button);
Assert.AreEqual("Submit", button.Content);
}
}
This test verifies that the control can be created on the UI thread and that its content is configured correctly.
It does not verify the complete visual appearance, user interaction, or whether the button is correctly displayed in a real application window. Those requirements need additional UI-level testing.
Test asynchronous UI operations
WinUI applications frequently perform asynchronous work. For example, a view model may load data from a service and update a bound property.
Keep the service logic separate from the visual control so it can be tested independently.
public class UserService
{
public async Task<string> GetUserNameAsync()
{
await Task.Delay(100);
return "Developer";
}
}
A regular MSTest method can verify this service:
[TestClass]
public class UserServiceTests
{
[TestMethod]
public async Task GetUserNameAsync_ReturnsExpectedName()
{
var service = new UserService();
var result = await service.GetUserNameAsync();
Assert.AreEqual("Developer", result);
}
}
The service test does not need a WinUI dispatcher because it does not create or access XAML objects.
If the test also updates a UI control or accesses a UI-bound object, use [UITestMethod] and ensure that the operation remains on the correct dispatcher.
Run WinUI Tests Locally
The test command depends on the application model and packaging configuration.
For a full-trust WinUI 3 application, the generated application host can execute the testing platform.
For an unpackaged WinUI application, run:
dotnet run
You can also build and execute tests with:
dotnet build -p:Platform=x64
dotnet test --project . --no-build -p:Platform=x64
Use an architecture supported by the application and the installed Windows App SDK components.
For unpackaged WinUI applications, do not use dotnet exec as the launch mechanism. WinUI resource loading depends on the process path, and placing dotnet.exe between the test command and the application can cause resource-loading problems.
For packaged WinUI applications, MSTest.Sdk provides the packaged-app launcher. It can register and activate the application using its Application User Model ID (AUMID).
For UWP and AppContainer scenarios, use the InvokeTestingPlatform MSBuild target from a suitable Developer PowerShell environment. The sidecar process handles package registration, application activation, and test result collection.
Configure CI for WinUI Testing
Running WinUI tests successfully on a developer machine does not guarantee that they will work on a CI agent.
A developer machine may already have the Windows App SDK runtime, Windows SDK, package registration permissions, and other required components installed.
A clean CI machine may not have these dependencies.
Before adding WinUI tests as a required CI step, validate the exact application model and test environment.
Check CI prerequisites
Verify the following:
- The agent runs a supported Windows version.
- The required .NET SDK is installed.
- The Windows SDK and development workload are available where required.
- The matching Windows App SDK runtime is installed for framework-dependent applications.
- The build architecture matches the test execution architecture.
- Developer Mode or an appropriate sideloading policy is enabled for unsigned packaged test output.
- The test process runs with the permissions required for package registration and activation.
- The CI agent can access the required NuGet package sources.
For packaged and AppContainer tests, the machine setup is particularly important. Run the controller under a non-elevated user where required by the application model.
Example CI workflow
A basic GitHub Actions workflow for a Windows-based WinUI test project can look like this:
name: WinUI Tests
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '10.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build application
run: dotnet build -c Release -p:Platform=x64
- name: Run tests
run: dotnet test --project . -c Release --no-build -p:Platform=x64
This is a starting point for an unpackaged or otherwise directly runnable WinUI test application. It is not a complete CI configuration for every packaged UWP, MSIX, or AppContainer model.
For packaged applications, configure the agent's package registration and sideloading requirements and use the appropriate MSBuild invocation. Confirm the exact command and permissions against the application model before making the workflow a required pull-request check.
Troubleshoot Common WinUI Testing Problems
WinUI test failures can originate from the test framework, the Windows application model, package activation, or UI-thread configuration.
| Problem | Possible reason | Recommended action |
|---|
| UI control creation fails | Test is not running on the UI dispatcher | Use [UITestMethod] |
| UI-thread access is unavailable | Dispatcher queue was not published | Assign the application's dispatcher queue before running tests |
| Packaged application does not start | Package registration or AUMID activation failed | Check manifest, Developer Mode, and package identity |
| Unpackaged app reports a missing manifest | The wrong test runner or launch mechanism is being used | Verify MTP configuration and unpackaged project settings |
| Test process remains open | Window or application lifecycle was not completed | Close the window and call Exit() after test execution |
| Failed tests return exit code zero | MTP result was not assigned to the process exit code | Set Environment.ExitCode to the returned result |
| CI tests fail but local tests pass | CI agent lacks runtime or package registration requirements | Validate the exact agent image and application model |
| AppContainer test cannot communicate with controller | Version or permission requirements are not met | Use compatible MSTest and MTP versions and run non-elevated |
Verify package activation
Packaged WinUI applications need a valid package manifest and application identity. Confirm that the manifest contains the expected application entry and that the test machine can register the build output.
For packaged activation, the Windows target framework should meet the documented minimum. The Windows Developer Mode or sideloading policy must also permit the registration of the unsigned development package.
Check the application lifecycle
If the test run completes but the process remains open, review the application shutdown logic.
The test application should close its window and exit after the testing platform returns. Also ensure that the test result is assigned to Environment.ExitCode; otherwise, CI may report a successful process exit even when tests have failed.
Verify versions
The updated Windows application support depends on compatible versions of MSTest and Microsoft.Testing.Platform.
For the configuration in this article, use MSTest 4.5.1 and MTP 2.5.1 through the corresponding MSTest SDK setup. If you manage platform packages directly, verify that the platform and extension versions are aligned.
Best Practices for MSTest and Microsoft.Testing.Platform
A reliable WinUI testing setup requires more than installing the packages. Follow these practices to keep tests stable and maintainable.
- Use
[TestMethod] for business logic and service tests that do not need XAML objects. - Use
[UITestMethod] only when a test needs the actual WinUI dispatcher. - Keep the UI test host responsible for creating the application window and publishing its dispatcher.
- Use the MSTest SDK to simplify test platform configuration.
- Keep MSTest and Microsoft.Testing.Platform versions compatible.
- Validate packaged and unpackaged application models separately.
- Test on a clean CI agent instead of relying only on a developer workstation.
- Keep the Windows SDK, Windows App SDK runtime, and .NET SDK versions consistent.
- Make sure test failures produce a nonzero process exit code.
- Avoid using UI-thread tests for functionality that can be tested with ordinary unit tests.
Also remember that UI-thread testing and UI automation are different. [UITestMethod] helps execute code on the WinUI dispatcher. It does not automatically simulate mouse clicks, keyboard input, accessibility interactions, or complete end-to-end user journeys.
For full UI automation, consider tools such as Appium with the Windows driver or other Windows UI Automation-based solutions. Use those tools when the requirement is to validate how a user interacts with the running application.
Advantages and Disadvantages
| Area | Advantages | Disadvantages |
|---|
| MSTest | Familiar attributes, assertions, and lifecycle support | UI testing requires additional application-specific setup |
| Microsoft.Testing.Platform | Supports in-process test execution and extensible runners | Requires understanding the application host and runner configuration |
| UITestMethod | Provides access to the actual UI dispatcher | Does not replace full UI automation |
| MSTest.Sdk | Simplifies package and runner configuration | Requires compatible SDK and platform versions |
| Packaged application testing | Validates behavior closer to the deployed application model | Package registration and machine policies add setup work |
| Unpackaged testing | Supports direct executable startup | Still requires the correct Windows App SDK and runtime configuration |
Conclusion
Testing WinUI applications requires attention to the UI thread, application lifecycle, and Windows packaging model. A regular .NET test runner does not automatically provide the environment required by XAML controls and other WinUI components.
MSTest and Microsoft.Testing.Platform provide a practical approach to this problem. With the updated MSTest SDK support, developers can run tests inside UWP and WinUI 3 application processes and use [UITestMethod] when tests need the actual UI dispatcher.
Start by separating business logic tests from UI-thread tests. Configure the correct MSTest SDK and runner, choose the appropriate packaged or unpackaged execution model, and validate the setup on the same Windows environment used by CI.
This approach makes WinUI testing more predictable and helps teams catch application problems earlier without making every test depend on a running user interface.
Join the conversation! Your thoughts help the community grow.