本文介绍了VS2017 无法加载文件或程序集 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 或其依赖项之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试在 VS2017 中打开旧的解决方案时,有一个旧的单元测试项目在构建时给我带来了问题.

我在构建此测试项目时不断收到以下错误:

无法加载文件或程序集file:///C:\Projects\MyProj\Test\DAL\UnitTestProj\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll"或其依赖项之一.系统找不到指定的文件.

我检查了项目的引用,它似乎引用了 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll.此外,没有代码错误.我怎么知道它是否是它找不到的依赖项之一?

解决方案

我在一个最初使用 VS2010 开发的项目中遇到了类似的问题(附加消息 The "BuildShadowTask" 任务意外失败),并且必须在最后几个小时内学习构建过程的另一个遗留方面.

您很有可能正在处理 private访问器文件 (.accessor),在 VS2012 中已弃用(原始来源).这在 来自 VS2010 团队的声明,他们不再致力于这些功能.

您也有可能只是在处理对错误版本的 UnitTestFramework 的错误引用,但 NuGet 还原应该可以解决此问题.如果没有,请参阅 this GitHub thread 以获取可能的修复(手动将 ref 更改为 public文件夹),或移动到新的 MSTest.TestAdapter 和 MSTest.TestFramework 包(参见 MSDN 支持线程).

解决方案

A.编辑单元测试 .csproj 并从 Shadow => 更改项目 Include 引用.:

B.更好的是,只需从单元测试项目的 Test References 文件夹中删除所有 .accessor 文件.

理想情况下,您还可以重写单元测试以删除对私有方法的引用,方法是重新架构以分离关注点或将属性更改为 internal 并使用friend"使用 InternalsVisibleToAttribute.


对于那些出于某种原因需要继续支持私有方法测试的人,同一篇文章为逻辑问题那么我可以使用什么?"提供以下建议::>

对于那些希望继续测试内部 API 的人,您有三个选择:

  1. 使用 Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject 类来帮助访问代码中的内部和私有 API.这可以在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 程序集中找到.
  2. 创建一个反射框架,该框架能够反映您的代码以访问内部或私有 API.
  3. 如果您尝试访问的代码是内部代码,您可以使用 InternalsVisibleToAttribute 访问您的 API,以便您的测试代码可以访问内部 API.

然而,对于语言团队添加的新功能,代码生成没有任何好的替代品.您可以创建 TestMethod 存根,然后删除内部代码.您只需要保留存根本身.


帮助我拼凑起来的进一步阅读/来源:

When trying to open an older solution in VS2017 there is an old Unit Test project that is giving me a problem when building.

I keep getting the following error when building this test project:

I checked the project's references and it appears to be referencing Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll. Additionally there are no code errors. How could I ever figure out if it is one of its dependencies that it can't find?

解决方案

I had a similar issue (with the additional message The "BuildShadowTask" task failed unexpectedly) with a project originally developed with VS2010, and got to spend the last few hours learning about yet another legacy facet of the build process.

There is a good chance that you are dealing with private accessor files (.accessor), which were deprecated in VS2012 (original source). This was foreshadowed in an announcement from the VS2010 team that they were no longer working on these features.

There is also a chance you're just dealing with erroneous refs to the wrong version of UnitTestFramework, but a NuGet restore should fix this. If not, see this GitHub thread for a possible fix (manually change the ref to the public folder), or move to the new MSTest.TestAdapter and MSTest.TestFramework packages (see MSDN support thread).

Solutions

A. Edit the unit test .csproj and change the item Include references from Shadow => None:<Shadow Include="Test References\namespace.accessor" /> to
<None Include="Test References\namespace.accessor" />

B. Better yet, simply delete all the .accessor files from the unit test project's Test References folder.

Ideally, you would also rewrite your unit tests to remove references to private methods, either by re-architecting to separate concerns or by changing properties to internal and using "friend" with the InternalsVisibleToAttribute.


For those who need to continue supporting testing of private methods for some reason, the same post provides the following suggestions to the logical question "What is available for me then?":


Further reading / sources that helped me piece this together:

这篇关于VS2017 无法加载文件或程序集 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 或其依赖项之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 11:03