AnyCPU测试程序集从x64生产程序集实现接口时BadImageFormatException

Dav*_*vid 24 c# 64-bit mstest visual-studio-2010

我似乎遇到了一个场景,当我在引用x64程序集的AnyCPU程序集上运行mstest时,我得到一个BadImageFormatException.

当AnyCPUTestingx64Production.dll测试程序集实现x64Production.dll中的接口(即使未使用)时,会出现此问题:

Unable to load the test container 'D:\AnyCPUTestingx64Production.dll' 
or one of its dependencies. error details:
System.BadImageFormatException: 
    Could not load file or assembly 'x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Run Code Online (Sandbox Code Playgroud)
  • mstest在Windows 7 64位上运行
  • 测试组件建成AnyCPU得到它在64位主机上运行64位(如概述这里)
  • testsettings文件指定<Execution hostProcessPlatform ="MSIL"/>
  • peverify和corflags没有任何有趣的东西
  • 这在玩具解决方案中很容易重现,即在哪里
    • x64Production
      • 没有引用其他组件
      • 仅包含一个空的公共接口IExampleInterface
      • 将<PlatformTarget>设置为x64
    • AnyCPUTestingx64Production
      • 仅引用x64Production.dll(即使没有对Microsoft.VisualStudio.QualityTools.UnitTestFramework的引用,也会出现此问题)
      • 仅包含x64Production.IExampleInterface的空实现
      • 将<PlatformTarget>设置为x64
  • nunit可以加载并运行测试程序集(一旦我转换了所有的测试属性)
    • 但对于较大的问题(涉及大量项目文件)来说,这不是一个很好的短期解决方案
  • 无论项目是3.5还是4.0,都会出现同样的问题
  • 无论是使用VS2008还是VS2010 c#编译器,都会出现同样的问题
  • 无论是使用VS2010的mstest还是使用测试代理,都会出现同样的问题
  • 加载AnyCPUTestingx64Production时mstest失败 - 即尝试在错误的QTAgent中加载程序集时没有问题(进程监视器中没有显示任何内容,重命名QTAgent32.exe没有效果):
    *** Assembly Binder Log Entry  (09/02/2012 @ 09:44:26) ***

    The operation failed.
    Bind result: hr = 0x8007000b. An attempt was made to load a program with an incorrect format.

    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe
    --- A detailed error log follows. 

    === Pre-bind state information ===
    LOG: User = David
    LOG: DisplayName = x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///D:/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = MSTest.exe
    Calling assembly : AnyCPUTestingx64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe.Config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///D:/x64Production.DLL.
    LOG: Assembly download was successful. Attempting setup of file: D:\x64Production.dll
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
Run Code Online (Sandbox Code Playgroud)

有没有其他人确定这是否在VS2010 mstest中根本不受支持?

Anu*_*pam 32

我来到这里寻找类似问题的解决方案.发布这个答案,以防我找到的解决方案帮助其他人.这在Visual Studio(2012)中为我解决了这个问题:

添加新项目 - >测试设置 添加测试设置项 更改测试设置 在64位进程中运行测试 默认情况下,此项设置为"强制测试以32位进程运行"

从菜单中:测试 - >测试设置 - >选择测试设置文件 - >选择您创建的测试设置文件.

现在运行测试.


Jus*_*ing 18

现在使用Visual Studio 2013(至少在2012年没有尝试)我不需要做任何事情,只需选择Test-> Test Settings-> Default Processor Architecture-> x64.也可以使用测试设置文件来实现相同的结果.你在其他答案和网上发布的各种帖子中都看不到那些旧的克服.由于我的东西必须使用x64,我添加这些测试用例只是为了提醒我是否有一些设置错误.

    [TestMethod]
    public void Running_64Bit_OS()
    {
        // It was determined to run only in 64 bits.
        bool is64BitOS = System.Environment.Is64BitOperatingSystem;
        Assert.AreEqual(is64BitOS, true);
    }

    [TestMethod]
    public void Running_64Bit_Process()
    {
        // We have a requirement that one of the unmanaged DLL is built for 64 bits.
        // If you are running MS Test in Visual Studio 2013 and this
        // is failing, go to Test->Test Settings->Default Processor Architecture and
        // chose x64, then run the tests again.  This is not the only method.  You
        // could also use a test settings file.
        bool is64BitProcess = System.Environment.Is64BitProcess;
        Assert.AreEqual(is64BitProcess, true);
    }
Run Code Online (Sandbox Code Playgroud)

  • 只是为了挑剔,你的断言应该有相反的参数,甚至更好地使用Assert.IsTrue. (4认同)

use*_*071 15

此外,您可以转到菜单Test-> Test Settings-> Default Procesor Architecture-> X64.它可能会奏效.


Jos*_*hua 13

从阅读本文来看,MSTest.exe是32位.

  • 你是对的.找不到任何表明不可能的资源,我一定乐观地误解了我在读什么."[Visual Studio Team Test Load 64位!](http://blogs.msdn.com/b/lkruger/archive/2009/06/08/visual-studio-team-test-load-agent-goes- 64-bit.aspx)")声明'我们实际上只支持以"任何CPU"或"x86"平台为目标的测试程序集.捂脸. (6认同)
  • 好吧,对我来说,悲伤的解决方案就是通过控制台应用程序进行测试.创建测试方法并修改`Program()`方法以启动运行所需的方法.这就像2005年重新开发一样! (2认同)