【问题标题】:NUnit: Could not load file or assembly 'some.dll' or one of its dependencies. The specified module could not be foundNUnit:无法加载文件或程序集“some.dll”或其依赖项之一。指定的模块无法找到
【发布时间】:2026-02-12 18:15:02
【问题描述】:

我有一个 C++/CLI DLL,它使用了一些 C++ DLL(都是为 32 位构建的)。我想使用 NUUnit 2.5.8.10295 在 C# DLL 中对其进行测试(更改为 2.6.4 并没有改变结果)

我使用了一个批处理文件,最终扩展为:

nunit-console-x86.exe "someUnitTests.dll" /framework=4.0.30319 /xml=NUnitResult_someUnitTests.xml

使用此命令,测试在我的开发 PC(64 位 Win 10 Enterprise Visual Studio 2013)上运行良好。从 Visual Studio Professional 2013 启动 nunit-console-x86.exe 并将工作目录设置为两个 DLL 的输出文件夹时,它也可以正常工作。

在构建服务器(在 Windows Server 2012 R2 上运行的 Jenkins)上执行时,测试失败并显示以下消息:

SetUp : System.IO.FileLoadException : Could not load file or assembly 'some.DLL' or one of its dependencies. A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

除了使用 VS 2013 Express 的 buildserver 之外,我尝试比较两台机器上已安装的框架和运行时(看不到明显的大差异)。我还尝试了 SysInternals ProcessMonitor,但没有成功。我不明白依赖行者的输出。

对使用的技术或工具有任何提示吗?

更新: 如果我添加 /noshadow,它也不再在我的开发 PC 上运行。在 Visual Studio 中,然后 nunit-console-x86 崩溃,而在命令行上我得到与构建服务器上相同的消息。虽然在有或没有 /noshadow 的情况下运行都没有区别

在没有任何 Visual Studio 但使用 vc_redist 2013 的 Windows 7 上仔细查看测试 PC:启用卷影复制(= 没有 /noshadow)我明白了:

SetUp : System.IO.FileNotFoundException : Could not load file or assembly 'some.DLL' or one of its dependencies. The specified module could not be found.

使用 /noshadow 错误会变成 BadImageFormatException

【问题讨论】:

    标签: c# c++ nunit command-line-interface badimageformatexception


    【解决方案1】:

    默认情况下,NUnit 2.x 卷影复制文件,但卷影复制不复制本机程序集。我相信它在 Visual Studio 中为您工作,因为您正在设置工作目录。

    要在 NUnit 中禁用卷影复制,请传递 /noshadow 命令行选项。

    顺便说一句,NUnit 3 中的默认行为是不进行卷影复制。

    【讨论】:

    • 我还发现测试PC上的错误信息与buildserver上的不同......
    • 当您的程序集之一是 x64 并且您正在运行 x86 或反之亦然时,您通常会看到 BadImageFormatException。
    【解决方案2】:

    最后,我设法通过创建一个没有 NUIt 的控制台应用程序来降低复杂性,遇到了类似的问题并修复了它。详情请见x86 console application doesn't run on 64bit machines except in debugger

    【讨论】:

      最近更新 更多