Error using Nunit test with WPF Windows app targeting .NET6

Viewed 66

I am using Visual Studio 2022 targeting .NET6 Windows using WPF When I added Nunit test, I selected .NET 6 as the target. When I add using WPF2022ht4fEditor to my Nunit test I get the error

Project '..\WPF2022ht4fEditor\WPF2022ht4fEditor.csproj' targets 'net6.0-windows10.0.17763.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v6.0'

If I remove the using statement, it compiles fine.

How do I fix this?

By the way, I recognize that the problem is that my Nunit test is targeting .NET version 6 and that the application is targeting net6.0-windows. The app .csproj is

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>`

The issue is that I can't find a way to change them so they match. For instance, if I change the .csproj file for the app and set the TargetFramework to net6.0, I get a different error.

1 Answers

The solution turned out to be extremely simple - change the Unit test to windows

The sequence is Open Solution Explorer and right click the solution and select "Add" then "new project" Select Nunit Test Project that runs on ".NET Core on Windows, Linux, and MacOS" Select Next Name the Unit test project and click Next Accept the .NET6.0 Framework option and click Create

Once the Unit test project is loaded, right-click the Unit test project Scroll to find Target OS Select Windows

Now you can add the project you want to test. Right Click on "Dependencies" under the test project Select "Add project reference" Select the project to test and press OK Add "using MyProject" (where MyProject is the name of the project to test)

Note: a reload may be necessary. Close the solution and start it again.

Dave

Related