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!
- MSTest v2: Setup a test project and run tests
- MSTest v2: Exploring asserts
- MSTest v2: Data tests
- MSTest v2: Test lifecycle attributes
- MSTest v2: Create new asserts
- MSTest v2: Customize test execution
- MSTest v2: Execute tests in parallel
- MSTest v2: Testing against multiple frameworks (this post)
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.
Open the csproj of the test project.
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)
After:csproj (MSBuild project file)
Run the tests using the command line:Shell
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!