Search code examples
xunitwinui-3

Can I run xUnit tests against classes in my Win Ui 3 Class Library?


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


Solution

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