MSTest v2: Testing against multiple frameworks

 
 
  • Gérald Barré

This post is part of the series 'MSTest v2'. Be sure to check out the rest of the blog posts of the series!

When you create a .NET library, you can target multiple frameworks: .NET Framework, .NET Core, Universal Windows Platform, etc., and their different versions. You can now write code that targets .NET Standard to target a maximum of platforms at once. However, you should test your code runs well on all these platforms. For example, if you create a .NET Standard 2.0 library, you may want to test it on .NET Core 2.0 and .NET Framework 4.6.1. You'll find the compatibility table of .NET Standard in the documentation

Using the new csproj, you can set multiple target frameworks. The test runner will use all of them.

  1. Open the csproj of the test project.

  2. Replace TargetFramework by TargetFrameworks (plural) and set the desired frameworks separated by a semicolon. The list of frameworks is available in the documentation.

    Before:

    csproj (MSBuild project file)
    <TargetFramework>netcoreapp2.0</TargetFramework>

    After:

    csproj (MSBuild project file)
    <TargetFrameworks>net461;net47;netcoreapp2.0</TargetFrameworks>
  3. Run the tests using the command line:

    Shell
    dotnet test

The unit tests are run three times, once per platform. This way you are sure your code will behave the same way on all platforms.

Note: The test explorer of Visual Studio doesn't support multiple frameworks. It will only display and execute the test of one platform.

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub