I created a WinUI project for testing some BitmapImage-related code using MSTest along the lines recommended here: https://devblogs.microsoft.com/ifdef-windows/winui-desktop-unit-tests/ and immediately ran into problems trying to use UITestMethod with async calls (e.g. BitmapImage.SetSourceAsync) and went down a rabbit hole using TaskCompletionSource and dispatching the bulk of the test to the UI thread, and then tried to address issues with waiting for XAML to render (because, in WinUI, you can't get at the bits in a BitmapImage, but you can render it into the Xaml tree and then use a RenderTargetBitmap to get the rendered pixels). What a joke.
I'd really prefer to use Xunit for my tests (as I've done for non-UI code -- though this is only UI code by necessity as described above).
So...
This led me to modify this: https://github.com/AArnott/Xunit.StaFact/blob/main/test/Xunit.StaFact.Tests/WindowsDesktop/WpfFactTests.cs to use the WinUI synchronization context to try to get it to work on a WinUI project, like so:
using DesktopSyncContext = Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext;
I also had to remove the "ShouldShowWindow" test case because that's not how WinUI does what that test is trying to assert. With only those two changes, it fails to run. I get no errors, but see these warnings:
Starting test discovery for requested test run
========== Starting test discovery ==========
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.5+1caef2f33e (64-bit .NET 7.0.5)
No test is available in WinUI3TestApp1.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
========== Test discovery finished: 0 Tests found in 4 sec ==========
========== Starting test run ==========
========== Test run finished: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========
The no test available message is odd, given that all the tests show up in the test explorer.
If I set a breakpoint and debug the first test, it's never hit. To reiterate, this is not my code, but code from the Xunit.StaFact repository with the two changes noted above. I'm at a loss as to how to get this to work.
Is this supported? Has anyone been able to do WinUI/WindowsAppSdk 1.3/.Net 6+ UI testing with Xunit, with or without Xuint.StaFact? If so, what are the steps? If it's not supported, what would it take to add that support. I'm probably willing to participate in doing the work if it comes to that.
Thanks in advance.