I’ve been banging my head against a wall trying to run XUnit tests on my Mac, for my Xamarin Forms app.

When I tried to build the project I got an error The FrameworkReference 'Microsoft.WindowsDesktop.App' was not recognized:

dotnet build
/usr/local/share/dotnet/sdk/7.0.102/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(376,5): error NETSDK1073: The FrameworkReference 'Microsoft.WindowsDesktop.App' was not recognized [/Users/damian/Projects/MyApp.XUnitTestProject/MyApp.XUnitTestProject.csproj]

Somehow, somewhere, a dependency on Microsoft.WindowsDesktop.App was being picked up.

I looked at the obj/project.assets.json file in the XUnit project directory, and searched for Microsoft.WindowsDesktop.App.

Sure enough, there it was under the Xamarin Community Toolkit:

      "Xamarin.CommunityToolkit/2.0.5": {
        "type": "package",
        "dependencies": {
          "Xamarin.Forms": "5.0.0.2291",
          "Xamarin.Forms.Platform.WPF": "5.0.0.2291"
        },
        "compile": {
          "lib/netcoreapp3.1/Xamarin.CommunityToolkit.dll": {
            "related": ".xml"
          }
        },
        "runtime": {
          "lib/netcoreapp3.1/Xamarin.CommunityToolkit.dll": {
            "related": ".xml"
          }
        },
        "frameworkReferences": [
          "Microsoft.WindowsDesktop.App"
        ]
      },

A quick search later led me to the culprit: [Bug] Unit Test projects now reference Xamarin.Forms.Platform.WPF

I followed the workaround there and updated my csproj to include GenerateErrorForMissingTargetingPacks:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

Now all my tests run on my Mac, meaning I can code with confidence, running tests whenever I want, rather than having to switch over to Windows to run them from time to time.