简体   繁体   中英

How can I test a .net 5 WPF project using a console project in Visual Studio 2019?

I'm writing a simple tool used to modify file names of some specical files. I created a WPF project in .NET 5 in Visual Studio 2019 (version 16.8.3).

This project is created using the " WPF App(.NET) " template, and by right-clicking this project in Solution Explorer and choose "Properties", I set the "Target framework" to " .NET 5.0 ". The "Output type" is " Windows Application ". The content of .csproj file of this WPF project:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

In this WPF project, there are several classes I want to test. In the .NET framework age, we can simply create a new Console project in this solution, and reference the WPF project. The console project contains all test code. But, when I do this as usual, the problem arises:

Whether I choose the " Console App (.NET Core) " template or the " Console App (.NET Framework) " template (there are only these two templates for console application in Visual Studio 2019 (version 16.8.3)), the test project cannot reference the WPF project correctly. When building the solution, error occurs:

Project ... targets 'net5.0-windows'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0'.

Or,

Project ... targets 'net5.0-windows'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.7.2'.

What should I do?

You can create a Console application or specific Unit test project , eg for NUnit, xUnit or MSTest:

  • Console App
  • NUnit Test Project
  • xUnit Test Project
  • MSTest Test Project
  • ...

Make sure to use the .NET Core variant of each project to get the new SDK project format automatically. Then change the target framework of these projects to .NET 5 like this:

  • Console application

    <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0-windows</TargetFramework> <RootNamespace>MyConsoleAppliaction</RootNamespace> </PropertyGroup> </Project>
  • Test project (here NUnit as an example) or other class libraries

    <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net5.0-windows</TargetFramework> <RootNamespace>MyUnitTests</RootNamespace> </PropertyGroup> <ItemGroup> <PackageReference Include="NUnit" Version="3.12.0" /> <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> </ItemGroup> </Project>

Referencing the WPF application will work just fine, since they now share the same target framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM