NUnit 加载 DLL 失败

NUnit failed to load DLL

我在 Visual Studio 中尝试 运行 单元测试时收到以下错误消息:

NUnit failed to load w:\Repos\trading.tools\Trading.Tools.Test\bin\x64\Debug\Trading.Tools.Test.dll

我正在使用

奇怪的是,我有另一个项目,它的设置方式与这个项目相同,而且工作正常。

我还下载并安装了NUnit 3.4.1。当我 运行

nunit3-console.exe Trading.Tools.Test.dll

一切正常。 有什么想法吗?

非常感谢 康斯坦丁

编辑#1

这是 Visual Studio 尝试 运行 所有测试时的完整控制台输出。

Test run will use DLL(s) built for framework Framework45 and platform X86. Following DLL(s) will not be part of run: 
Trading.Tools.Test.dll, Trading.Tools.dll are built for Framework Framework45 and Platform X64.
 Go to http://go.microsoft.com/fwlink/?LinkID=236877&clcid=0x409 for more details on managing these settings.
NUnit Adapter 3.4.0.0: Test discovery starting
NUnit failed to load w:\Repos\trading.tools\Trading.Tools.Test\bin\x64\Debug\Trading.Tools.Test.dll
Assembly contains no NUnit 3.0 tests: w:\Repos\trading.tools\Trading.Tools\bin\x64\Debug\Trading.Tools.dll
NUnit Adapter 3.4.0.0: Test discovery complete

如您所见,很明显 NUnit 需要 x86 构建,但我为 x64 平台构建。再一次,如果我使用 nunit3-console.exe.

执行它,我的 x64 构建工作得很好

我在 csproj 文件中看到的是这样的:

<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\NUnit.3.4.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>

这里的奇怪之处在于它指定使用 Version=2.6.4.14350 但引用 3.4.1 dll。

所以下一个问题是如何让 NUnit 执行我的 x64 构建?有什么想法吗?

我遇到了类似的问题,关键是 Visual Studio 中的 Test Runner 声明仅测试 x86 程序集。我由此假设它会强制使用 x86 NUnit 运行器。要更改此设置(至少在 VS2015 和 VS2017 中),请转到 Test > Test Settings > Default Processor Architecture > X64.

您还可以在运行设置文件中设置执行目标。然后,您必须 select 该文件。这应该使解决方案更稳定。 只设置此项的 运行 设置文件可能如下所示:

开启方法如下图所示:

当您从测试菜单 (1) 中 select 时,它将作为 selected 被添加到菜单 (2) 中,然后重建将使测试出现在测试资源管理器中 (3)

使用 运行 设置文件还有一个额外的好处,那就是如果您使用它,它将在 TFS 构建系统上正确 运行。我已经就那个问题写了一篇博客 post,请参阅 http://hermit.no/how-to-control-the-selection-of-test-runner-in-tfsvsts-making-it-work-with-x86x64-selected-targets/

我在写单元测试方法的时候碰巧遇到了这个错误。并注意到根本原因是缺少加载的依赖 dll 之一。在修改测试方法代码并尝试 运行 后,此错误 ("NUnit failed to load the .dll") 显示在输出 ("Test") window 中。更新依赖 dll 的 nuget 包后,nunit 开始选择测试项目 dll 并执行测试用例。

我无法执行测试,发现这是问题之一。原来我的 TestFixtureinternal。只需将其切换为 public 即可解决我的问题。

在尝试上述所有其他响应均未成功后,以下对我有用:

在我的例子中,.NET 项目和解决方案位于已安装的驱动器上(我使用 MacBook 和 Parallels 进行 .NET 开发)。挂载还包含 NUnit 试图从中读取 "test" DLL 的 /bin/debug 和 /bin/release 位置。

修复方法是将 solution/project 文件移动到我的 Windows 图像的 C: 驱动器。测试立即被发现。

显然 shared/mounted 位置不符合它的喜好。我不知道为什么,因为挂载是永久的,并且 read/writable 对 Windows 图像上的所有用户。我怀疑文件权限问题,或者 user/process 运行 NUnit 发现逻辑无法访问整个装载。

今天我也 运行 遇到了这个问题(在基于 .NET Framework 4.8 的 NUnit 项目上)。我的解决方案是同时安装 Microsoft.NET.Test.Sdk 包。