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

4

30

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

Maybellemayberry answered 20/2, 2018 at 12:34 Comment(0)
C
40

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.

Chaddie answered 20/2, 2018 at 12:38 Comment(3)
Sounds reasonable. If my library project is implemented using netstandard2 as class library (without it I cannot reference it in Azure Func app), then netcoreapp2 is the better option then net470, or does not matter?Maybellemayberry
You should consider testing each .NET Standard-compatible platform you expect to see usage out of. Keep in mind that a few methods will throw a PlatformNotSupportedException on some platforms even though the APIs exist.Chaddie
Note that .NET Standard 2.0 is also supported in UWP. Do we also unit test it on UWP?Wayzgoose
B
10

.NET Standard is a specification that each .NET Standard version(such as .NET Framework, .NET Core and Xamarin) defines the set of APIs that all .NET implementations must provide to conform to that version. You library has a value for TargetFramework of netstandard2.0 means you can reference the logic library not only from a .NET Core app, but also from an app built for .NET Framework or Xamarin.

However, You can’t build apps for it, only libraries. Here's the MSDN doc about .NET Standard.

So if you want to test the library, you need to specify the targets of which your library would support. And if you want to support multiple .NET version then you should test them all to make sure your library can run on these targets correctly. Here's the configuration of target framework in .csproj:

Single target:

<TargetFramework>net461</TargetFramework>

Multiple targets:

<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
Brake answered 20/2, 2018 at 15:6 Comment(0)
B
5
  1. Create a new unit test project in the same solution that targets say .Net Framework 4.6.1 if your class library is to be used by an application that targets .Net Framework 4.6.1 so you test with the same combination of frameworks.
  2. Add a reference to the class library project under references in the unit test project.
  3. Add the xUnit and xUnit.runner.visualstudio nuget packages to the unit test project.
  4. Rename the unit test class to something relevant, and replace the using MSTest directive with using XUnit.
  5. Start writing and running tests.(build/re-build solution so it updates the tests list in the test explorer for each new test).
Bereft answered 4/4, 2018 at 14:35 Comment(0)
P
0

I have recently released my private unit test platform that does .NETStandard 2.0 just fine. Unless you have put a lot of time and effort into your tests you might want to have a look at Nuclear.Test.

Basically if you target your test project at .NETStandard it will execute these tests in a .NETFramework and .NETCore process to see if they both work correctly.

Unfortunately it requires .NETStandard 2.0 as a minimal version so far. Luckily that's what you are using.

This solution is neither NUnit nor xUnit nor MSTest but it will do exactly what you described.

Pussyfoot answered 16/8, 2019 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.