Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 667
AndrasCsanyi Avatar asked Feb 20 '18 12:02

AndrasCsanyi


People also ask

Which is better xUnit or NUnit or MSTest?

As far as NUnit vs. XUnit vs. MSTest is concerned, the biggest difference between xUnit and the other two test frameworks (NUnit and MSTest) is that xUnit is much more extensible when compared to NUnit and MSTest. The [Fact] attribute is used instead of the [Test] attribute.

What is difference between NUnit and MSTest?

MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn't have direct integration into the native APIs like MsTest does.

What are the differences between xUnit and NUnit test frameworks?

NUnit creates a new instance of the test class and then runs all of the test methods from the same instance. Whereas xUnit.net creates a new instance of the test class for each of the test methods.

What is the prerequisite to unit testing C# with MSTest and net?

Create the test project The test project requires other packages to create and run unit tests. dotnet new in the previous step added the MSTest SDK, the MSTest test framework, the MSTest runner, and coverlet for code coverage reporting. You can see the entire file in the samples repository on GitHub.


2 Answers

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.

like image 76
NightOwl888 Avatar answered Oct 13 '22 02:10

NightOwl888


.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> 
like image 20
Feiyu Zhou Avatar answered Oct 13 '22 01:10

Feiyu Zhou