How to test .NET Standard 2 library with either NUnit, xUnit or MSTest from either Rider or VS 2017?
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.
.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>
- 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.
- Add a reference to the class library project under references in the unit test project.
- Add the xUnit and xUnit.runner.visualstudio nuget packages to the unit test project.
- Rename the unit test class to something relevant, and replace the using MSTest directive with using XUnit.
- Start writing and running tests.(build/re-build solution so it updates the tests list in the test explorer for each new test).