Minimum MSTest versions for VS 17.6
After I updated my copy of Visual Studio 2022 to the latest version 17.6, I couldn't run the tests from the Test Explorer anymore that worked just fine in version 17.5. In the Tests category of the Output window, there was the following error message:
========== Starting test discovery ==========
An exception occurred while test discoverer 'MSTestDiscoverer' was loading tests. Exception: Could not load type 'Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior' from assembly 'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
No test is available in C:\Users\damir\Git\Blogposts\MsTestVs176\MsTestVs176\bin\Debug\net7.0\MsTestVs176.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
========== Test discovery finished: 0 Tests found in 813,2 ms ==========
I had similar experience in the past, so I immediately suspected that the problems were related to the versions of test-related NuGet packages in my project:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
For comparison, I tried creating a new project from the MSTest Test Project template. To no surprise, it came with newer versions of all the packages:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
I experimented a bit with upgrading different packages and soon figured out that the main culprit were the two MSTest.*
packages. Upgrading both of them to version 2.2.8 was enough to get the tests working again:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
If you decide to update everything to the latest version instead, keep in mind that there are some breaking changes in MSTest v3.
You can find a sample project my GitHub repository with the two different versions of NuGet packages in different commits. In Visual Studio 17.6 the tests will only work in the latest commit, but in the version before that the tests will work in the previous commit as well.
There are quite a few new features in Visual Studio 17.6, but you might also need to upgrade some of your NuGet package dependencies to keep your tests running. Fortunately, you don't need to do any changes to your code if you're fine with using the minimal working version instead of the latest one.