0

I'm trying to create unit tests for views in my WindowsAppSDK application. I want to be able to run tests with the UITestMethodAttribute.

I'm running into build errors and the test host process crashing in Visual Studio 2022.

I tried using Template Studio to give me example code to follow but the test host crashes: https://github.com/microsoft/TemplateStudio/issues/4711

I tried following this Microsoft blog which worked until you reference a project that also builds an application https://devblogs.microsoft.com/ifdef-windows/winui-desktop-unit-tests/

Here is part of the project file I am trying to add unit tests for:

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>root</RootNamespace>
    <ApplicationIcon>Assets/WindowIcon.ico</ApplicationIcon>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
    <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
    <GenerateAppInstallerFile>True</GenerateAppInstallerFile>
    <AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
    <AppxSymbolPackageEnabled>True</AppxSymbolPackageEnabled>
    <GenerateTestArtifacts>True</GenerateTestArtifacts>
    <AppxBundle>Auto</AppxBundle>
    <AppInstallerUri>C:\Published\x\</AppInstallerUri>
    <AppxBundlePlatforms>x86</AppxBundlePlatforms>
    <AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
<AppxPackageSigningTimestampDigestAlgorithm>SHA256</AppxPackageSigningTimestampDigestAlgorithm>
    <PackageCertificateThumbprint>x</PackageCertificateThumbprint>
    <AppxPackageDir>C:\Published\x</AppxPackageDir>
</PropertyGroup>

And this is the test project following the Microsoft blog. This works until I add reference to the project under test.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
    <RootNamespace>root.UnitTests</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
  </PropertyGroup>

    <ItemGroup>
        <ProjectCapability Include="TestContainer" />
    </ItemGroup>
    
    <ItemGroup>
        <AppxManifest Include="Package.appxmanifest">
            <SubType>Designer</SubType>
        </AppxManifest>
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="MSTest.TestAdapter">
            <Version>2.2.8</Version>
        </PackageReference>
        <PackageReference Include="MSTest.TestFramework">
            <Version>2.2.8</Version>
        </PackageReference>
        <PackageReference Include="Microsoft.TestPlatform.TestHost">
            <Version>16.10.0</Version>
            <ExcludeAssets>build</ExcludeAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
        <Content Include="Assets\SplashScreen.scale-200.png" />
        <Content Include="Assets\LockScreenLogo.scale-200.png" />
        <Content Include="Assets\Square150x150Logo.scale-200.png" />
        <Content Include="Assets\Square44x44Logo.scale-200.png" />
        <Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
        <Content Include="Assets\StoreLogo.png" />
        <Content Include="Assets\Wide310x150Logo.scale-200.png" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.2.230217.4" />
        <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
        <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

    <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
        <ProjectCapability Include="Msix" />
    </ItemGroup>

    <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
        <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
    </PropertyGroup>

</Project>

eric-v
  • 3
  • 3

0 Answers0