英文:
Unable to unit test .NET MAUI Class Library
问题
我有一个.NET MAUI类库,我试图使用XUnit进行单元测试。该类库的目标是iOS、Android和Windows。
我已经创建了一个XUnit测试项目,但是当我尝试运行测试时,我收到以下错误信息:
项目的目标为'net7.0-windows10.0.19041.0;net7.0-windows10.0.22621.0'。不能被一个以'.NETCoreApp,版本=v7.0'为目标的项目引用
我要测试的项目的.csproj文件包含以下代码:
<PropertyGroup>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst;net7.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net7.0-windows10.0.19041.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
<Platforms>AnyCPU;x86;x64;ARM64;ARM32</Platforms>
</PropertyGroup>
如果我删除以下行:
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net7.0-windows10.0.19041.0</TargetFrameworks>
我可以运行单元测试,但是然后我无法在Windows上运行我的MAUI项目。
我做错了什么?任何帮助将不胜感激。
英文:
I have a .NET MAUI Class Library that I am trying to Unit Test with XUnit. The Class Library targets iOS, Android and Windows.
I have created a XUnit Test project but when I try and run a test I get the following error:
Project targets 'net7.0-windows10.0.19041.0;net7.0-windows10.0.22621.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v7.0'
My .csproj file for the project I want to test has the following code:
<PropertyGroup>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst;net7.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net7.0-windows10.0.19041.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
<Platforms>AnyCPU;x86;x64;ARM64;ARM32</Platforms>
</PropertyGroup>
If I remove the line
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net7.0-windows10.0.19041.0</TargetFrameworks>
I can run the unit tests but then I can't run my MAUI project on Windows.
What am I doing wrong? Any help would be greatly appreciated
答案1
得分: 0
替换下面的代码:
<OutputType>Exe</OutputType>
为:
<OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
英文:
Go to .Net Maui project .csproj, replace below code
<OutputType>Exe</OutputType>
To
<OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
答案2
得分: 0
感谢 @LiqunShen-MSFT,我发现通过将我的测试项目中的以下行从:
<TargetFramework>net7.0</TargetFramework>
更改为:
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
它解决了我的问题。
英文:
Thanks to @LiqunShen-MSFT I found that by changing the following line in my test project from:
<TargetFramework>net7.0</TargetFramework>
To:
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
it solved my problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论