Skip to content

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

@Marv51

Description

@Marv51

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions