3

I have created a brand new "Class Library (Win Ui in Desktop)" library via the Add New Project functionality of Visual Studio.

For the purposes of this question I have replaced the name of the library with <MYLIBRARY>.

After creating an "xUnit Test Project", and selecting ".NET Core 3.1 (Long-term support)" as the Target Framework, then adding a Project Reference to the Class Library, I get these errors:

Error NU1201 Project <MYLIBRARY> is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project <MYLIBRARY> supports: net5.0-windows10.0.19041 (.NETCoreApp,Version=v5.0) <MYLIBRARY>.UnitTests

Error Project '..\<MYLIBRARY>\<MYLIBRARY>.csproj' targets 'net5.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v3.1'. <MYLIBRARY>.UnitTests C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 1718

After deleting that test project, and creating a new "xUnit Test Project" and selecting ".NET 5 (Current)" as the Target Framework, then adding a Project Reference to the Class Library, I get these errors:

Error NU1201 Project <MYLIBRARY> is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Project <MYLIBRARY> supports: net5.0-windows10.0.19041 (.NETCoreApp,Version=v5.0) <MYLIBRARY>.UnitTests

Error Project '..\<MYLIBRARY>\<MYLIBRARY>.csproj' targets 'net5.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0'. <MYLIBRARY>.UnitTests C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 1718

In the .csproj of the unit test project I tried changing <TargetFramework>net5.0</TargetFramework> to <TargetFramework>net5.0-windows</TargetFramework> but that didn't fix anything.

I've looked around at various websites but I found some answers that seemed to say that I should be able to do what I want as "Unit testing frameworks like xUnit.net are generally agnostic of the libraries that your production code uses.", without explanation of how to get it working if it's not, and others which say that I can't do what I want because xUnit and Win Ui 3 are not compatible yet, e.g. "WinUI 3 does not currently support any of the .NET unit testing frameworks.".

I did some searches of Stack Overflow - for Win Ui / xUnit / etc. - but none of the results I found were of any use to me.

Is it possible for me to run xUnit unit tests against the classes in my Class Library?

(I might be getting some things mixed up because this is my first Win Ui project.)

GarryP
  • 143
  • 9

1 Answers1

3

Since your Win UI class library targets net5.0-windows10.0.19041, i.e. .NET 5 and an OS-specific version, your test project should also target this very same version.

Right-click on the test project in Visual Studio, select "Edit Project File" and change the target framework to:

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>

Then it should build.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Wonderful. That works and my tests run just fine now. Thank you very much. Just out of interest, where did the 10.0.19041.0 come from? I didn’t specify it when creating the Class Library. Is this something that might change and/or I need to keep an eye on? – GarryP Nov 18 '21 at 12:30
  • The OS version in the TFM indicates which Windows APIs are available to your library when when you develop and build it. It does not control the OS version that your library supports at run time. – mm8 Nov 19 '21 at 14:24
  • Ah, thanks. I think I will have to do some further reading about frameworks. – GarryP Nov 22 '21 at 12:20