简体   繁体   English

NUnit 加载失败 DLL

[英]NUnit failed to load DLL

I am getting the following error message when trying to run unit tests in Visual Studio:尝试在 Visual Studio 中运行单元测试时收到以下错误消息:

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

I am using我在用

  • Visual Studio Community 2013 Visual Studio 社区 2013
  • NUnit Adapter 3.4.0.0 NUnit 适配器 3.4.0.0
  • NUnit 3.4.1 N单元 3.4.1

The weird thing is, that I have another project which is set up the same way as this one and it works just fine.奇怪的是,我有另一个项目,它的设置方式与这个项目相同,而且工作正常。

I also downloaded NUnit 3.4.1 and installed it.我还下载并安装了 NUnit 3.4.1。 When I run当我跑步时

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

everything works just fine.一切正常。 Any ideas what I can do?任何想法我能做什么?

Many thanks Konstantin非常感谢康斯坦丁

Edit #1编辑 #1

Here is the full console output from Visual Studio when trying to run all test.这是尝试运行所有测试时来自 Visual Studio 的完整控制台 output。

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

As you can see it is very obvious that NUnit expects a x86 build, but I build for a x64 platform.如您所见,很明显 NUnit 需要 x86 构建,但我是为 x64 平台构建的。 And again, my x64 build works just fine if I execute it using nunit3-console.exe .再一次,如果我使用nunit3-console.exe执行它,我的 x64 构建工作得很好。

What I see in the csproj file is this:我在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>

The weird thing here is that it specifies using Version=2.6.4.14350 but referencing a 3.4.1 dll.这里奇怪的是它指定使用Version=2.6.4.14350但引用 3.4.1 dll。

So the next question from this point is how can I make NUnit execute my x64 build?所以从这一点开始的下一个问题是如何让 NUnit 执行我的 x64 构建? Any ideas?有任何想法吗?

I had a similar issue, the key is the fact that it is the Test Runner in Visual Studio that is stating that only x86 assemblies will be tested.我有一个类似的问题,关键是 Visual Studio 中的Test Runner声明只测试 x86 程序集。 I am assuming from this that it then forces the use of the x86 NUnit runner.我由此假设它会强制使用 x86 NUnit runner。 To change this (in VS2015 and VS2017 at least), go to Test > Test Settings > Default Processor Architecture > X64 .要更改此设置(至少在 VS2015 和 VS2017 中),请转到Test > Test Settings > Default Processor Architecture > X64

You can also set the execution target in the runsettings file.您还可以在 runsettings 文件中设置执行目标。 You then have to select that file.然后您必须选择该文件。 This should make the solution more stable.这应该使解决方案更稳定。 A runsettings file which only set this can look like:仅设置此项的运行设置文件可能如下所示:

在此处输入图片说明

To enable it, do as shown in the figure below:要启用它,如下图所示:

在此处输入图片说明

When you select it from the test menu (1), it will be added as the selected one in the menu (2), and a Rebuild will then make the test appear in the Test Explorer (3)当您从测试菜单 (1) 中选择它时,它将被添加为菜单 (2) 中的选定项,然后重建将使测试出现在测试资源管理器中 (3)

There is an extra bonus by using a runsettings file, and that is that it will then run properly on the TFS Build system, if you use that.使用 runsettings 文件还有一个额外的好处,那就是它可以在 TFS Build 系统上正常运行,如果你使用它。 I have written a blog post on that issue, see http://hermit.no/how-to-control-the-selection-of-test-runner-in-tfsvsts-making-it-work-with-x86x64-selected-targets/我写了一篇关于这个问题的博客文章,见http://hermit.no/how-to-control-the-selection-of-test-runner-in-tfsvsts-making-it-work-with-x86x64-selected -目标/

I couldn't execute my tests and found that to be one of the issues.我无法执行我的测试并发现这是问题之一。 It turns out that my TestFixture was internal .事实证明,我的TestFixtureinternal Just switching it to public solved my case.只是将其切换为公开解决了我的情况。

After unsuccessfully trying all other responses above, the following worked for me:在尝试上述所有其他响应失败后,以下对我有用:

In my case the .NET project and solution is on a mounted drive (I use a MacBook and Parallels for .NET development).就我而言,.NET 项目和解决方案位于已安装的驱动器上(我使用 MacBook 和 Parallels 进行 .NET 开发)。 The mount also contains the /bin/debug and /bin/release locations where NUnit was attempting to read the "test" DLL from.挂载还包含 NUnit 试图从中读取“测试”DLL 的 /bin/debug 和 /bin/release 位置。

The fix was to move the solution/project files to the C: drive of my Windows image.修复方法是将解决方案/项目文件移动到我的 Windows 映像的 C: 驱动器。 The tests were discovered immediately.测试很快就被发现了。

Apparently the shared/mounted location was not to its liking.显然,共享/安装位置不合其意。 I don't know why, since the mount is permanent and read/writable to all users on the Windows image.我不知道为什么,因为安装是永久性的,并且对于 Windows 映像上的所有用户来说都是可读/可写的。 I suspect file permissions problems or maybe somehow the entire mount is not accessible to the user/process running the NUnit discovery logic.我怀疑文件权限问题,或者运行 NUnit 发现逻辑的用户/进程无法访问整个挂载。

I happened to get this error when writing the unit test method.我在编写单元测试方法时碰巧遇到这个错误。 And noticed the root cause that one of the dependent dll was missing to load.并注意到缺少加载依赖的 dll 之一的根本原因。 This error("NUnit failed to load the .dll") was shown in the Output("Test") window after modifying the test method code and trying to run it.修改测试方法代码并尝试运行后,此错误(“NUnit 无法加载 .dll”)显示在输出(“测试”)窗口中。 After updating the nuget package for the dependent dll, nunit started picking the test project dll and test cases got executed.更新依赖 dll 的 nuget 包后,nunit 开始选择测试项目 dll 并执行测试用例。

Today I was running into this issue as well (on a .NET Framework 4.8 based NUnit project).今天我也遇到了这个问题(在基于 .NET Framework 4.8 的 NUnit 项目上)。 The solution for me was to also install the Microsoft.NET.Test.Sdk package.我的解决方案是还安装 Microsoft.NET.Test.Sdk 包。

To get to the root cause, it may help to attempt to run your tests using the NUnit CLI.要找到根本原因,尝试使用 NUnit CLI 运行测试可能会有所帮助。

In my case, the CLI logged a bind failure I didn't see in Visual Studio.在我的例子中,CLI 记录了我在 Visual Studio 中没有看到的绑定失败。 After I had fixed that, my tests ran correctly via CLI and VS.修复后,我的测试通过 CLI 和 VS 正确运行。

I got this error in a.Net 6.0 Asp.Net solution and here's how I solved it.我在 a.Net 6.0 Asp.Net 解决方案中遇到了这个错误,这就是我解决它的方法。

It was only occurring in one Test project whose tests wouldn't run, the other Test projects were running fine in Test Explorer and in Debug.它只发生在一个测试无法运行的测试项目中,其他测试项目在测试资源管理器和调试中运行良好。

The tests that were failing to be detected had "Test Not Run":未能检测到的测试有“测试未运行”:

在此处输入图像描述

In the Output is the error:在Output是报错:

NUnit Adapter 4.3.1.0: Test discovery starting NUnit failed to load [dll path] NUnit 适配器 4.3.1.0:启动 NUnit 的测试发现无法加载 [dll 路径]
No test is available in [dll path] [dll 路径] 中没有可用的测试

What I did was comment out each class and bring them back one by one until the Tests would STOP to RUN.我所做的是注释掉每个 class 并将它们一一带回,直到测试停止运行。 Then with the failing class put a break point on a [Test] method.然后随着失败的 class 在 [Test] 方法上放置一个断点。

If you can't hit the break point then its failing in this classes [SetUp].如果你不能达到断点,那么它在这个类 [SetUp] 中失败了。 Put a BreakPoint in the [SetUp] and use F11 to step off the edge of the method, ie F11 off the final curly brace..在 [SetUp] 中放置一个 BreakPoint 并使用 F11 离开方法的边缘,即 F11 离开最后的大括号..

AND THEN you get a prompt saying which DLL it can't load.然后你会得到一个提示,说它无法加载哪个 DLL。

In my case it was “couldn't load a DLL Microsoft.AspNetCore.Mvc.Core”在我的例子中是“无法加载 DLL Microsoft.AspNetCore.Mvc.Core”

Referencing the DLL via Package Manager resolved the problem.通过 Package Manager 引用 DLL 解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM