How to test .NET Standard 2 library with either NUnit, xUnit or MSTest from either Rider or VS 2017?

SayusiAndo picture SayusiAndo · Feb 20, 2018 · Viewed 10.3k times · Source

I have a project where I use Azure Durable Functions, and they are available only on .NET Standard 2. So, it defines which class library can be used in testing projects. But, I cannot put together a library where either xUnit, NUnit or MSTest is used in unit/integration testing.

Adding NUnit to a project where .NET Standard 2 is class library fails with the following error:

INFO: Restoring packages for C:\VSTS\github.com\netstandardXunitMsTestNunit\src\Netstandard2xUnitMsTestnUnit\nunit\nunit.csproj... DEBUG: Restoring packages for .NETStandard,Version=v2.0... DEBUG: Resolving conflicts for .NETStandard,Version=v2.0... ERROR: Cycle detected. nunit -> NUnit (>= 3.9.0). DEBUG: Checking compatibility of packages on .NETStandard,Version=v2.0. DEBUG: Checking compatibility for nunit 1.0.0 with .NETStandard,Version=v2.0.

The error is the same for xUnit (just the error message talks about xUnit cycle).

Both error can be reproduced in Rider and Visual Studio 2017 Enterprise too. I tried it again after I cleaned nuget cache. The result is the same.

In case of MsTest, possible to add ms test libraries, but test discovery does not work neither Rider and nor Visual Studio.

Is it even possible unit test a .NET Standard 2 library?

Is there anything I can do beside waiting for these projects to pick up .NET Standard 2 stuff?

I created a small sample project, can be found here: https://github.com/SayusiAndo/netstandard2xunitresharper

Answer

NightOwl888 picture NightOwl888 · Feb 20, 2018

There is no runtime for .NET Standard, so it will not execute your tests.

Your test assembly must target an executable platform, such as a version of .NET Framework or .NET Core.

<TargetFramework>net470</TargetFramework>

Or

<TargetFramework>netcoreapp2.0</TargetFramework>

See Running .NET Standard binaries on different frameworks for more details.