将UnitTest项目添加到解决方案:无法识别指定的RuntimeIdentifier

问题描述 投票:0回答:1

几周前我为 .NET 8.0 设置了一个 WinUI 3 项目。

一开始我遇到了一个问题,之前已经在 StackOverflow 上描述过这里

搜索错误代码(NETSDK1083)也会引导我到一个微软页面,其中解释了错误here

阅读这些文章并调整我的项目的 csproj 文件,从 RID 中删除“10”后,项目成功启动,直到现在我才听说这个错误。

今天我通过 MSTestv2 添加了一个 UnitTest 项目,并且还通过 NUnit 进行了不同的尝试。 两者都会导致同样的问题: 当在 UnitTest 项目中添加主项目作为参考时,我再次遇到了许多前面描述的具有相同错误代码的错误。

添加UnitTest项目时的错误码

我今天收到的所有错误,链接到文件 C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk argets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets 中的第 90 行

这里的标签内部有 RuntimeIdentifier 属性,它从名为“RuntimeIdentifier”的变量中读取。由于这个文件应该保持不变,并且我尝试将 win-x64 硬编码到属性中,但这仍然不起作用,恐怕这不是问题的根本原因。

另一种尝试是查找新添加的测试项目的 csproj 文件,看看是否有提及仍然具有发行版特定 RID 的 RuntimeIdentifiers(例如 win10-x64 而不是 win-x64)。但不,即使对整个解决方案进行全文搜索也没有显示任何提及这一点。

有人可以帮我吗?我的项目确实需要单元测试,但我无法想象这个错误是特定于我的 Windows 安装的。

编辑: 今天出现了这个警告(之前没有显示过) 有关 RID 的警告

unit-testing winui-3 runtime-identifier
1个回答
0
投票

您的测试项目似乎是一个基本的 .NET 项目,而不是 WinUI 3 应用程序项目。

它可能仍处于 预览版,但我可以在 Visual Studio 2022 预览版上为 WinUI 3 应用程序添加单元测试项目:

enter image description here

我的单元测试项目如下所示:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>SampleAppTests</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &gt;= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers> 
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
  </PropertyGroup>

  <ItemGroup>
    <Page Remove="UnitTestApp.xaml" />
    <ApplicationDefinition Include="UnitTestApp.xaml" />
    <ProjectCapability Include="TestContainer" />
  </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.Windows.SDK.BuildTools" Version="10.0.22621.756" />
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
    <PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
    <PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
    <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.7.2">
      <ExcludeAssets>build</ExcludeAssets>
    </PackageReference>
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

  <!-- 
    Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
    Tools extension to be activated for this project even if the Windows App SDK Nuget
    package has not yet been restored.
  -->
  <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
    <ProjectCapability Include="Msix" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\SampleApp\SampleApp.csproj" />
  </ItemGroup>

  <!-- 
    Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution 
    Explorer "Package and Publish" context menu entry to be enabled for this project even if 
    the Windows App SDK Nuget package has not yet been restored.
  -->
  <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
    <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
  </PropertyGroup>
</Project>
© www.soinside.com 2019 - 2024. All rights reserved.