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>
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.
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.