unit-testingmstestwinui-3

Winui3 project cannot be referenced by a MSTest project that targets '.NETCoreApp,Version=v7.0'


I added a MSTest into a WinUI 3 Project. I created a unit test file using the context menu item "Create Unit Tests". Then I had following errors when building the MSTest Project. How to resolve it?

Error       Project '..\xxx\xxx\xxx.csproj' targets 'net6.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v7.0'.

 
error NU1201: Project xxx is not compatible with net7.0 (.NETCoreApp,Version=v7.0). Project xxx supports: net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)

WinUi 3 project file xxx.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>HP.EasyShell.Primus</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <UseWinUI>true</UseWinUI>
  </PropertyGroup>

MSTest project testproject.csproj

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

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
    <PackageReference Include="coverlet.collector" Version="3.2.0" />
  </ItemGroup>

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

</Project>

Solution

  • It looks like your WinUI project targets .NET 7, but your MS Test project targets .NET 6. The solution might be as simple as changing the target framework for your test project to .NET 7.

    1. Right-click on the unit test project in Solution Explorer.
    2. Click "Properties".
    3. In the "Application -> General" panel in the Properties window, change the target framework to match the WinUI project.
    4. Save the project file.
    5. Clean and rebuild.

    I've had some challenges with changing framework versions, which are easily solved by removing all files in the bin and obj folders in each of the affected projects. Consider removing the .vs folder at the root of the solution, and then reopening Visual Studio.

    You can always choose the nuclear option if using Git for version control, and run git clean -fdx from the command line. Just be aware this deletes all untracked files and folders, including changes to tracked files you told Git to ignore.