How to properly unit test a .NET project with multiple target frameworks, given implementation differences among targets?

First, test projects can multi-target as well by making use of the <TargetFrameworks>property just like you used for the library. While the VS test runner currently only shows/runs one framework (the first one to be specific), any invocation of dotnet test will execute all frameworks (xunit is also developing custom console runner – dotnet xunit – for a better console UX).

Also keep in mind that test projects need to specify a target framework that has a runtime. So your test projects can either target netcoreapp* or net* but never netstandard* since it does not have a runtime associated to run the tests. I’m not sure about the portable target since the Microsoft.NET.Tests.Sdk that ever test project needs to reference to get test runner support doesn’t have conditions for NETPortable.

So you’d need to pick all the versions of net and netcoreapp that you need to test on / support – e.g. net20;net45;net46;netcoreapp1.0 would cover all your .net framework and .net core versions.

There is a way to force a dependency to a specific target framework by setting a Properties="TargetFramework=netstandard1.3" on the <ProjectReference> element, but this may be hard to get working correctly for restore and multi-targeting.

Leave a Comment