kerry
kerry

Reputation: 105

Unable to unit test .NET MAUI Class Library

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

Upvotes: 3

Views: 1235

Answers (2)

kerry
kerry

Reputation: 105

Thanks to @LiqunShen-MSFT I found that by changing the following line in my test project from: net7.0 To: net7.0-windows10.0.19041.0 it solved my problem

Upvotes: 1

Guruprasad
Guruprasad

Reputation: 76

Go to .Net Maui project .csproj, replace below code

<OutputType>Exe</OutputType>

To

<OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>

Upvotes: 1

Related Questions