Support Multiple frameworks UT
Background
we have a Project name Plan Visualization, It is a plugin project , This project have 2 framework to support different platform, .net framework 4.6.1 for desktop , .net core 2.2 for Azure Web service. both of them sharing some common library. the common library framework is .net standard 2.0. we need do Unit testing for the common library , because .net standard no run time library, so we cannot create a Unit testing project with .net standard framework. so we need use Multiple frameworks.
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.
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:
net461
Multiple targets:
netcoreapp2.0;net461
How to do
- Create a SDK Style Project:
- create a SDK Style project, since MSBuild 15.0, a new kind of project called SDK-Style project is available. It is a slimmer version of the project file and works directly with dotnet CLI.
https://kimsereyblog.blogspot.com/2018/08/sdk-style-project-and-projectassetsjson.html - moving to SDK-style projects and package references
http://hermit.no/moving-to-sdk-style-projects-and-package-references-in-visual-studio-part-2/
-
Set multiple frameworks in csproj file
open project file, change to , add the frameworks you want to support.here is the framework list:https://docs.microsoft.com/en-us/dotnet/standard/frameworks
-
Reference Test SDK to project
Microsoft.NET.Test.Sdk
NUnit3TestAdapter
NunitXml.TestLogger -
Run UT in Commaindline
run all framework test: dotnet test
run specific framework test: dotnet test -f netcoreapp2.0