question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WinUI 3 Tests fail to run after upgrade to Windows App SDK 1.1

See original GitHub issue

Description

After upgrading my test project to Windows App SDK 1.1 the execution of tests fails.

Steps to reproduce

I followed https://devblogs.microsoft.com/ifdef-windows/winui-desktop-unit-tests/

UITestMethod attributed tests, fail to run after I upgrade to Windows App SDK 1.1 with error message:

Exception thrown while executing test. If using extension of TestMethodAttribute then please contact vendor. 

Error message: 

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., 

Stack trace:    
at Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer.UITestMethodAttribute.Execute(ITestMethod testMethod)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodRunner.ExecuteTest(TestMethodInfo testMethodInfo)

The exact same project work with Microsoft.WindowsAppSDK 1.0.3.

I tried to add [assembly: WinUITestTarget(typeof(App))] but that doesn’t work either.

The DispatcherQueue should get set in the OnLaunched method of my App class: UITestMethodAttribute.DispatcherQueue = m_window.DispatcherQueue;

Expected behavior

Test should get executed.

Actual behavior

Running tests fails with message above. No window ever pops up and the App class seems to be not created.

Environment

WinAppSDK 1.1.0 VS 17.2.3 Microsoft.TestPlatform.TestHost 17.2.0 MSTest.TestFramework 2.2.10 MSTest.TestAdapter 2.2.10

.csProj (click to expand)
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.18363.0</TargetPlatformMinVersion>
    <RootNamespace>Telani.UITests</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
	<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
	<ImplicitUsings>enable</ImplicitUsings>
    <UseWinUI>true</UseWinUI>
  </PropertyGroup>
  <ItemGroup>
	  <ProjectCapability Include="TestContainer" />
  </ItemGroup>
  <ItemGroup>
    <AppxManifest Include="Package.appxmanifest">
      <SubType>Designer</SubType>
    </AppxManifest>
  </ItemGroup>
  <ItemGroup>
    <Content Include="Images\LockScreenLogo.scale-200.png" />
    <Content Include="Images\SplashScreen.scale-200.png" />
    <Content Include="Images\Square150x150Logo.scale-200.png" />
    <Content Include="Images\Square44x44Logo.scale-200.png" />
    <Content Include="Images\Square44x44Logo.targetsize-24_altform-unplated.png" />
    <Content Include="Images\StoreLogo.png" />
    <Content Include="Images\Wide310x150Logo.scale-200.png" />
  </ItemGroup>

  <ItemGroup>
	<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

    <ItemGroup>
	    <PackageReference Include="MSTest.TestAdapter">
		    <Version>2.2.10</Version>
	    </PackageReference>
	    <PackageReference Include="MSTest.TestFramework">
		    <Version>2.2.10</Version>
	    </PackageReference>
	    <PackageReference Include="Microsoft.TestPlatform.TestHost">
		    <Version>17.2.0</Version>
		    <ExcludeAssets>build</ExcludeAssets>
	    </PackageReference>
    </ItemGroup>
</Project>
App.xaml.cs (click to expand)
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using System.Runtime.InteropServices.WindowsRuntime;
using Telani.UITests;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

[assembly: System.Runtime.Versioning.SupportedOSPlatform("windows10.0.18363.0")]
//[assembly: WinUITestTarget(typeof(App))]

namespace Telani.UITests;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="args">Details about the launch request and process.</param>
    protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
    {
        Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();

        m_window = new MainWindow();

        // Ensure the current window is active
        m_window.Activate();

        UITestMethodAttribute.DispatcherQueue = m_window.DispatcherQueue;

        // Replace back with e.Arguments when https://github.com/microsoft/microsoft-ui-xaml/issues/3368 is fixed
        Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(Environment.CommandLine);
    }

    private Window m_window;
}

Tests.cs (click to expand)
using Microsoft.UI.Xaml.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using System.Text;

namespace Telani.UITests;

[TestClass]
public class QuickTest
{

    [TestMethod]
    public void TestMethod1()
    {
        Assert.AreEqual(0, 0);
    }

    [UITestMethod]
    public void TestMethod2()
    {
        var grid = new Grid();
        Assert.AreEqual(0, grid.ActualWidth);
    }
}

AB#1643611

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:30 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
Evangelinkcommented, Aug 1, 2022

Just a small message to let you know that we have seen this ticket and we will handle as fast as we can (we are a little short staffed with holidays and some other high priority tasks we need to handle).

2reactions
Evangelinkcommented, Nov 8, 2022

Hey there! We identified a root cause of this error and filled a Dev Community bug for a fix. We also identified some improvements on WinUI and MSTest sides and we also discussed a way to improve our testing matrix to reduce likelihood of such issue in the future.

Thanks again for your patience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stable channel release notes for the Windows App SDK
The following releases of the stable channel are currently available: Version 1.3; Version 1.2; Version 1.1; Version 1.0; Version 0.8; Version ...
Read more >
Exe published from default WinUI 3/Windows App SDK ...
So I made a new project (which has a window with a button in the middle of it), changed nothing in it, tried...
Read more >
What's New in Windows App SDK 1.1
The Windows App SDK is the starting point for building and shipping Windows desktop apps (spanning C++ and .NET) with WinUI 3 controls...
Read more >
Newest 'windows-app-sdk' Questions - Page 2
My updated WinUI 3 programs now produce the following message when run: This application requires the Windows App Runtime ... windows-app-sdk.
Read more >
Live unit testing does not work with Source Generators
This works fine when the application is running and when manually running unit tests. When I run the live unit tests I receive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found