单元测试适配器引发异常:无法加载一种或多种请求的类型

2024-01-04

我尝试从 Visual Studio 2010 命令提示符运行 SpecFlow 测试,但收到一条相当迟钝的错误消息:

单元测试适配器抛出异常: 无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。

有关我的 VS2010 项目设置的一些信息:

  • Windows 7 企业版,64 位(版本 6.1.7601 Service Pack 1 内部版本 7601)
  • Visual Studio 2010 高级版 (v10.0.40219.1 SP1Rel)
  • 使用编码的 UI 测试
  • 使用 SpecFlow 1.9.0,委托给 CodedUI 测试 API
  • MSTest
  • .NET v4.0.30319
  • 整个解决方案正在编译为 32 位代码(我有同事仍在使用 XP)

我有一个构建后事件,它将一些 DLL 文件从我的 NuGet 包目录复制到目标目录:

copy $(SolutionDir)packages\SpecBind.1.2.1.71\lib\net45\SpecBind.dll $(TargetDir)
copy $(SolutionDir)packages\SpecBind.CodedUI.1.2.1.71\lib\net45\SpecBind.CodedUI.dll $(TargetDir)

如果没有这个,mstest 就无法加载许多 SpecFlow 程序集。

测试项目App.config的相关部分

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
    <section name="specBind" type="SpecBind.Configuration.ConfigurationSectionHandler, SpecBind" />
  </configSections>

  <connectionStrings>
    ...
  </connectionStrings>

  <specFlow>
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
    <unitTestProvider name="MsTest" generatorProvider="Specflow.CodedUI.MsTestCodedUiGeneratorProvider, Specflow.CodedUI" runtimeProvider="TechTalk.SpecFlow.UnitTestProvider.MsTest2010RuntimeProvider, TechTalk.SpecFlow" />
    <stepAssemblies>
      <!-- This attribute is required in order to use StepArgument Transformation as described here; 
           https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Step-argument-transformations  -->
      <stepAssembly assembly="SpecFlow.Assist.Dynamic" />
      <stepAssembly assembly="SpecBind" />
    </stepAssemblies>
  </specFlow>
  <specBind>
    <browserFactory provider="SpecBind.CodedUI.CodedUIBrowserFactory, SpecBind.CodedUI" browserType="IE" />
    <!-- For additional details on SpecBind configuration options see the project site. -->
  </specBind>
</configuration>

我用来启动测试的命令:

C:\path\to\bin\Debug> mstest /testcontainer:MyTests.dll /test:SpecFlowFeatureName

Loading MyTests.dll
Starting Execution...

Results         Top Level Tests
--------        ------------------
Failed          ...
Failed          ...
Failed          ...

...

我一直在寻找解决方案,但我一直找到的只是对 VS2008 的引用和禁用代码覆盖率。我有 VS2010,并且在我的本地测试设置中未启用代码覆盖率。

Windows 事件日志查看器

在我的 Windows 事件日志查看器中查找之后,我终于找到了更多信息(我听到人们抱怨他们找不到带有此错误的堆栈跟踪 - 请查看您的事件查看器)

The description for Event ID 0 from source VSTTExecution cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

(QTAgent32.exe, PID 6920, Thread 213) Unit Test Adapter threw exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.GetAssemblyInfo(Assembly assembly)
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.GetTypeInfo(Type type, Boolean checkAlreadyExaminedType)
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.ResolveMethods()
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.Initialize(UnitTestResult result, UnitTestRunner runner, ConsoleOutputRedirector redirector)
   at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestRunner.Run(UnitTestElement test, ITestContext testContext, Boolean isLoadTest, Boolean useMultipleCpus)

the message resource is present but the message is not found in the string/message table

我不知道如何解决这个错误。当我右键单击编辑器中加载的 .feature 文件并选择“运行 SpecFlow 场景...”时,SpecFlow 测试可以在 Visual Studio 内部正常运行,但无法从命令行运行它们。

我可以发布任何想法或其他信息吗?

附带说明一下,这源于我之前的问题:如何使用 MSTest 从命令行运行 SpecFlow 场景? https://stackoverflow.com/questions/20613221/how-do-you-run-specflow-scenarios-from-the-command-line-using-mstest

更新#1

我又查了一下,发现了这篇博客文章:MSTest 和 64 位 http://rupertrawnsley.blogspot.com/2011/04/mstest-and-64bit.html。我检查了本地测试设置,它强制测试以 32 位运行。

现在使用这个命令:

mstest /testcontainer:MyTests.dll /testsettings:"..\..\..\Local.testsettings"

错误信息:

Test method Blah.Dee.Blah threw exception: 
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.Extension, Version=11.0.0.0, Culture=neutral, PublicKeyToken=...' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

TestCleanup method Blah.Dee.Blah.ScenarioTearDown threw exception. BoDi.ObjectContainerException: BoDi.ObjectContainerException: Interface cannot be resolved: SpecBind.BrowserSupport.IBrowser.

更新#2

我删除了对 Microsoft.VisualStudio.TestTools.UITest.Extension 的引用并将其添加回来。已清洁。重建。再次运行相同的命令。仍然遇到同样的错误。

由于某种原因,它尝试将 Microsoft.VisualStudio.TestTools.UITest.Extension 程序集加载为版本 11,而我的计算机上安装了版本 10。我正在尝试追踪指定此版本的位置,以便我可以更改它。


以防万一其他人遇到这个问题,对我来说这是因为启用了代码覆盖率。禁用它解决了这个问题:

http://blogs.msdn.com/b/danielvl/archive/2010/02/16/enable-code-coverage-in-visual-studio.aspx http://blogs.msdn.com/b/danielvl/archive/2010/02/16/enable-code-coverage-in-visual-studio.aspx

  • 打开 .testsettings 文件
  • 在“测试设置”下,单击“数据和诊断”
  • 取消选中“代码覆盖率”
  • 点击“应用”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单元测试适配器引发异常:无法加载一种或多种请求的类型 的相关文章

随机推荐