visual-studio-2010 - 单元测试适配器抛出异常 : Unable to load one or more of the requested types

标签 visual-studio-2010 mstest specflow

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

Unit Test Adapter threw exception: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information..



关于我的 VS2010 项目设置的一些信息:
  • Windows 7 企业版,64 位(版本 6.1.7601 Service Pack 1 Build 7601)
  • Visual Studio 2010 高级版 (v10.0.40219.1 SP1Rel)
  • 使用编码的 UI 测试
  • 使用 SpecFlow 1.9.0,它委托(delegate)给 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 事件日志查看器中 Root 之后,我终于找到了更多信息(我听说人们提示他们找不到带有此错误的堆栈跟踪——查看你的事件查看器)
    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 中运行良好,但我无法从命令行运行它们。

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

    作为旁注,这源于我之前的问题:How do you run SpecFlow scenarios from the command line using MSTest?

    更新 #1

    我又摸索了一下,发现了这篇博文:MSTest and 64bit .我检查了我的本地测试设置,它强制测试以 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

  • 打开.testsettings 文件
  • 在“测试设置”下,单击“数据和诊断”
  • 取消选中“代码覆盖率”
  • 点击“申请”
  • 关于visual-studio-2010 - 单元测试适配器抛出异常 : Unable to load one or more of the requested types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909195/

    相关文章:

    c# - 调试返回值

    c - 防止命令提示符在代码运行后关闭

    unit-testing - 如何为 MS Test 创建自定义属性?

    .net - .NET 断言类型

    c# - 使用步骤参数转换在 SpecFlow 中格式化字符串

    visual-studio-2010 - NUnit 未指定为 SpecFlow 的测试运行器

    vb.net - 我编写了一个反转字符串的代码(确实如此),但我认为这不是干净/正确的方法

    c# - 单元测试 : how to test dependent methods where mocking is unfeasible?

    nunit - 测试失败时 Hook NUnit 或 SpecFlow

    visual-studio-2010 - C++ tbb_debug.dll 缺失