无法单元测试.NET MAUI类库。

huangapple go评论57阅读模式
英文:

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 &#39;net7.0-windows10.0.19041.0;net7.0-windows10.0.22621.0&#39;. It cannot be referenced by a project that targets &#39;.NETCoreApp,Version=v7.0&#39;

My .csproj file for the project I want to test has the following code:

&lt;PropertyGroup&gt;
	&lt;TargetFrameworks&gt;net7.0-android;net7.0-ios;net7.0-maccatalyst;net7.0&lt;/TargetFrameworks&gt;
	&lt;TargetFrameworks Condition=&quot;$([MSBuild]::IsOSPlatform(&#39;windows&#39;))&quot;&gt;net7.0-windows10.0.19041.0&lt;/TargetFrameworks&gt;
	&lt;UseMaui&gt;true&lt;/UseMaui&gt;
	&lt;SingleProject&gt;true&lt;/SingleProject&gt;
	&lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
	&lt;SupportedOSPlatformVersion Condition=&quot;$([MSBuild]::GetTargetPlatformIdentifier(&#39;$(TargetFramework)&#39;)) == &#39;ios&#39;&quot;&gt;14.2&lt;/SupportedOSPlatformVersion&gt;
	&lt;SupportedOSPlatformVersion Condition=&quot;$([MSBuild]::GetTargetPlatformIdentifier(&#39;$(TargetFramework)&#39;)) == &#39;maccatalyst&#39;&quot;&gt;14.0&lt;/SupportedOSPlatformVersion&gt;
	&lt;SupportedOSPlatformVersion Condition=&quot;$([MSBuild]::GetTargetPlatformIdentifier(&#39;$(TargetFramework)&#39;)) == &#39;android&#39;&quot;&gt;21.0&lt;/SupportedOSPlatformVersion&gt;
	&lt;SupportedOSPlatformVersion Condition=&quot;$([MSBuild]::GetTargetPlatformIdentifier(&#39;$(TargetFramework)&#39;)) == &#39;windows&#39;&quot;&gt;10.0.17763.0&lt;/SupportedOSPlatformVersion&gt;
	&lt;TargetPlatformMinVersion Condition=&quot;$([MSBuild]::GetTargetPlatformIdentifier(&#39;$(TargetFramework)&#39;)) == &#39;windows&#39;&quot;&gt;10.0.17763.0&lt;/TargetPlatformMinVersion&gt;
	&lt;SupportedOSPlatformVersion Condition=&quot;$([MSBuild]::GetTargetPlatformIdentifier(&#39;$(TargetFramework)&#39;)) == &#39;tizen&#39;&quot;&gt;6.5&lt;/SupportedOSPlatformVersion&gt;
	&lt;Platforms&gt;AnyCPU;x86;x64;ARM64;ARM32&lt;/Platforms&gt;
&lt;/PropertyGroup&gt;

If I remove the line

&lt;TargetFrameworks Condition=&quot;$([MSBuild]::IsOSPlatform(&#39;windows&#39;))&quot;&gt;net7.0-windows10.0.19041.0&lt;/TargetFrameworks&gt;

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

&lt;OutputType&gt;Exe&lt;/OutputType&gt;

To

&lt;OutputType Condition=&quot;&#39;$(TargetFramework)&#39; != &#39;net7.0&#39;&quot;&gt;Exe&lt;/OutputType&gt;

答案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

huangapple
  • 本文由 发表于 2023年6月8日 06:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定