5

I've got a C# MsTest project that targets net48. I've change the project it tests to the newer style Microsoft.NET.Sdk csproj format and that went ok. Now I want to convert the unit test project too.

I get errors about namespace Microsoft.VisualStudio.TestTools.UnitTesting cannot be found: error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft'

To solve that, I added the assembly reference to the csproj:

  <ItemGroup>
    <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
  </ItemGroup>

But I get errors that it can't be found. warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.VisualStudio.QualityTools.UnitTestFramework". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

This was found ok when the old csproj format was used.

How can I get this assembly reference to be found, or what is the preferred way of making this work?

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • Did you try [MSTest.TestFramework](https://www.nuget.org/packages/MSTest.TestFramework/) package? – Pavel Anikhouski May 12 '20 at 20:13
  • Does this answer your question? [Where to find "Microsoft.VisualStudio.TestTools.UnitTesting" missing dll?](https://stackoverflow.com/questions/13602508/where-to-find-microsoft-visualstudio-testtools-unittesting-missing-dll) Seems to be an exact duplicate – Pavel Anikhouski May 12 '20 at 20:14
  • @PavelAnikhouski Thanks for the link. No it didn't answer the question, I've effectively tried the first answer there, but I'm using a different project format and the assembly can't be resolved. – Scott Langham May 12 '20 at 20:37
  • @Pavel I tried a different package, I'm having more success with MSTest.TestFramework. It now builds, thanks. – Scott Langham May 12 '20 at 20:51
  • I'm now just having this issue: Starting test execution, please wait... Test run will use DLL(s) built for framework .NETFramework,Version=v4.0 and platform X64. Following DLL(s) do not match framework/platform settings. MyTest.VsTests.dll is built for Framework .NETFramework,Version=v4.8 and Platform AnyCPU. – Scott Langham May 12 '20 at 20:52
  • Ok, I also added package MSTest.TestAdapter and it now works – Scott Langham May 12 '20 at 20:57
  • Yes, test adapter is needed – Pavel Anikhouski May 12 '20 at 21:02

2 Answers2

1

Add the nuget packages MSTest.TestFramework and MSTest.TestAdapter.

The test framework allows the unit tests to build and brings in the required namespaces. The second allows the test runner to find the tests in the project.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • The nuget packages you linked to are MSTestV2 but `Microsoft.VisualStudio.QualityTools.UnitTestFramework` is MSTestV1. – Mark Aug 31 '22 at 21:38
1

Here are 3 possible solutions:

  1. Upgrade to MSTestV2 by adding the nuget packages MSTest.Framework and MSTest.TestAdapter.
  • CAVEAT: MSTestV2 only supports .NET Framework 4.5 and later.
  1. Continue using MSTestV1 by helping visual studio find the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll reference, by adding this to your csproj: <PropertyGroup><AssemblySearchPaths></AssemblySearchPaths></PropertyGroup>
  1. Continue using MSTestV1 by browsing to the dll's path when adding the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
  • Depending on your version of VS the path will be something like: Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\PublicAssemblies

I used #2 because we target net40 and have multiple developers working on different versions of VS.

Mark
  • 374
  • 4
  • 9