C# MsTest 项目可以转换为 Microsoft.NET.Sdk 项目格式吗?

Sco*_*ham 6 c# unit-testing visual-studio

我有一个针对 net48 的 C# MsTest 项目。我已将其测试的项目更改为更新样式的 Microsoft.NET.Sdk csproj 格式,一切顺利。现在我也想转换单元测试项目。

我收到有关无法找到命名空间 Microsoft.VisualStudio.TestTools.UnitTesting 的错误:error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft'

为了解决这个问题,我添加了对 csproj 的程序集引用:

  <ItemGroup>
    <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

但我收到错误消息,无法找到它。 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.

当使用旧的 csproj 格式时,发现这是可以的。

我怎样才能找到这个程序集引用,或者完成这项工作的首选方法是什么?

Mar*_*ark 1

以下是 3 种可能的解决方案:

  1. 通过添加 nuget 包MSTest.FrameworkMSTest.TestAdapter.
  • 注意: MSTestV2仅支持 .NET Framework 4.5 及更高版本。
  1. 通过帮助 Visual Studio 找到参考Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll,并将其添加到您的 csproj 中,继续使用 MSTestV1: <PropertyGroup><AssemblySearchPaths></AssemblySearchPaths></PropertyGroup>
  • 请参阅此解释以了解为什么这是必要的
  1. 添加引用时,浏览到 dll 的路径,继续使用 MSTestV1Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
  • 根据您的 VS 版本,路径将类似于:Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\PublicAssemblies

我使用 #2 是因为我们的目标是 net40 并且有多个开发人员在不同版本的 VS 上工作。