P3.NET

Upgrading from MSTest to MSTest v2

MSTest v2 is the new version of the MSTest framework that has been shipping with Visual Studio for years. Unlike the previous version, the new version is a set of NuGet packages that do not have a dependency on the version of Visual Studio installed. This article will discuss the process of upgrading from the “old” version to v2.

NOTE: As of Oct 2017, MSTest v2 and package references do not work correctly with TFS builds. The project file traditionally has a .targets file for the test adapter and test framework. The framework .targets file resides in a Visual Studio folder but the adapter’s .targets file comes from the package. Because package references do not create a packages folder in the solution, the .targets file cannot be found. For TFS builds this results in tests not being run because no adapter can be found. Running tests in Visual Studio work correctly.

Updating the Root Project Settings

Before getting started it is assumed that the project has been updated to use package references. While you can use the packages.config pattern with MSTest v2, this article assumes you have updated to the newer approach.

Open the project file in a text editor (Visual Studio works great) if you have the Project File Tools extension. In the root ProjectGroup (the one that is not conditionally included) add the following if they are not there yet.

<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>

Personally I also remove the following elements. I have not had any issues but they may be required by some projects.

  • IsCodedUITest
  • TestProjectType

Cleaning Up the Project

Add an ImportProject element to the project file.

<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />

Go through the project file and remove any references to the *QualityTools* assemblies in Visual Studio. This includes the following.

  • Reference elements to the assembly
  • Choose elements that are selecting different versions of the assembly and/or target files
  • Any references to .targets files other than the one added earlier

Update NuGet Packages

Reload the project into Visual Studio. The project will not compile yet so we need to update the packages. Add the following packages from NuGet into the test project.

  • MSTest.TestFramework
  • MSTest.TestAdapter

At this point the test project should compile so compile and then run all the tests to ensure they pass. Note that the TestAdapter is what allows Visual Studio to find the projects so ensure that all the tests are discovered properly as well.

You have now upgraded to MSTest v2.