microsoft / microsoft/microsoft-ui-xaml

InvalidOperationException when running unit tests unpackaged

Open
#11,267 0 comments 0 reactions 0 assignees View on GitHub
bug needs-triage
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

When I create a WinUI Unit Test App from the template and try to run it unpackaged all tests with the UITestMethod attribute fail with a InvalidOperationException.

### Why is this important?

Unit tests should be able to run unpackaged to test unpackaged applications. I do not want to refactor my project only to be able to run tests.

### Steps to reproduce the bug

1. Create a WinUI Unit Test App from the template
2. Run the tests and see both samples run succesfully
3. add `None` and `true` to the csproj
4. Build the tests and run again
5. See TestMethod2 fail with InvalidOperationException

### Actual behavior

Any UITestMethod which is supposed to run on the UI thread runs into InvalidOperationException.

### Expected behavior

TestMethod2 should run successfully, same as when packaged.

### Screenshots

_No response_

### NuGet package version

Microsoft.WindowsAppSDK 2.3.1

### Windows version

Windows 10 (21H2): Build 19044

### Additional context

Ignore everything about RPC_E_WRONG_THREAD that I edited out. That was from some experiments where I forcefully created a DispatcherQueueController. The DispatcherQueue is not even getting created in the unpackaged application.

This is the full error thrown by the test:
`An unhandled exception was thrown by the 'Execute' method. Please report this error to the author of the attribute 'Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer.UITestMethodAttribute'.
System.InvalidOperationException: UITestMethodAttribute.DispatcherQueue should not be null. To use UITestMethodAttribute within a WinUI Desktop App, remember to set the static UITestMethodAttribute.DispatcherQueue during the test initialization.
at Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer.UITestMethodAttribute.ExecuteAsync(ITestMethod testMethod) in /_/src/TestFramework/TestFramework.Extensions/Attributes/WinUI_UITestMethodAttribute.cs:line 48
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodRunner.ExecuteTestAsync(ITestContext executionContext, TestMethodInfo testMethodInfo) in /_/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs:line 511`

Contributor guide

Open the contributing guide

Research direction

Start with the WinUI Unit Test App template, its csproj settings for WindowsPackageType and WindowsAppSDKSelfContained, and the UITestMethodAttribute.DispatcherQueue initialization described in the exception. Reproduce the failure with TestMethod2 in an unpackaged test app, then verify that the UI-thread test runs successfully without a null DispatcherQueue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.