Collyn Brenner
Collyn Brenner

Reputation: 11

MAUI/WPF, Error NU1201 (Project TargetFramework Compatibility Issue) when Unit Testing OS-specific Project

I'm having this issue in .NET MAUI and WPF. I just want to set up a unit test project that targets either the MAUI or WPF project (these are in separate solutions btw). The issue is the MSTest project targets .NET8 while the project being tested targets .NET8-windows ('windows' being the required OS target of both the MAUI project and the WPF project). I have also tried adding xUnit and NUnit test projects as a workaround but all three produce the same result. I've cleaned solution multiple times and I have restarted VS multiple times. I am on Day 2 of troubleshooting this. I wish I could target the included OS platform from the test project, but I don't know how or the things I've tried have not worked. I don't understand why this is happening; I've never had this problem when adding test projects before. Here is the WPF project .csproj file (because it is simpler than the MAUI one; note that the SDK is because this is a Blazor Hybrid WPF project):

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
      <RootNamespace>SuccessfulSetup</RootNamespace>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
    <!--  <ItemGroup>    <None Remove="Components\NazareneStyle.razor.css" />  </ItemGroup>  <ItemGroup>    <Compile Include="Components\NazareneStyle.razor.css" />  </ItemGroup>-->
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="8.0.7" />
  </ItemGroup>

</Project>

Here is the MSTest .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="6.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
    <PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\SuccessfulSetup\SuccessfulSetup.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
  </ItemGroup>

</Project>

And these are the error messages: Unit Test Error Messages

Upvotes: 0

Views: 233

Answers (1)

Pedro Ferragut
Pedro Ferragut

Reputation: 1

I had the same problem with WPF. The WPF application was targeting .NET8-windows, while MSTest targeted .NET8 and the tests wouldn't work. What worked for me was to change the MSTest version in its .csproj from .net8.0 to .net8.0-windows. The PropertyGroup from MSTest.csproj would be like this:

<PropertyGroup>
  <TargetFramework>net8.0-windows</TargetFramework>
  <ImplicitUsings>enable</ImplicitUsings>
  <Nullable>enable</Nullable>

  <IsPackable>false</IsPackable>
  <IsTestProject>true</IsTestProject>
</PropertyGroup>

Upvotes: 0

Related Questions