23

I am getting the following error message when trying to run unit tests in 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
  • NUnit Adapter 3.4.0.0
  • NUnit 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. 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

Here is the full console output from Visual Studio when trying to run all test.

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. And again, my x64 build works just fine if I execute it using nunit3-console.exe.

What I see in the csproj file is this:

<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.

So the next question from this point is how can I make NUnit execute my x64 build? Any ideas?

Konstantin
  • 461
  • 1
  • 5
  • 13

8 Answers8

63

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. I am assuming from this that it then forces the use of the x86 NUnit runner. To change this (in VS2015 and VS2017 at least), go to Test > Test Settings > Default Processor Architecture > X64.

user276648
  • 6,018
  • 6
  • 60
  • 86
Fishus
  • 746
  • 6
  • 5
  • 1
    Thanks a lot, that worked! Do you know in which file this setting is saved? – Konstantin Aug 26 '16 at 03:57
  • Unfortunately, not for certain, however as it seems to change from solution to solution, and doesn't change when switching configurations I suspect it will be part of the suo file. – Fishus Aug 26 '16 at 09:29
  • My project are compiled as "AnyCPU" with "Prefer 32-bit", and when I changed from "Auto" to "x86" tests runs as expected – Frode Evensen Aug 18 '22 at 11:14
4

I couldn't execute my tests and found that to be one of the issues. It turns out that my TestFixture was internal. Just switching it to public solved my case.

gcbs_fln
  • 333
  • 2
  • 10
  • You could use [InternalsVisibleToAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute?view=netframework-4.7.2) to allow your test assembly to access internals. – user276648 Jan 31 '19 at 09:12
3

You can also set the execution target in the runsettings file. You then have to select that file. This should make the solution more stable. A runsettings file which only set this can look like:

enter image description here

To enable it, do as shown in the figure below:

enter image description here

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)

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. 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/

Terje Sandstrøm
  • 1,979
  • 15
  • 14
2

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. 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. After updating the nuget package for the dependent dll, nunit started picking the test project dll and test cases got executed.

2

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). The mount also contains the /bin/debug and /bin/release locations where NUnit was attempting to read the "test" DLL from.

The fix was to move the solution/project files to the C: drive of my Windows image. 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. I suspect file permissions problems or maybe somehow the entire mount is not accessible to the user/process running the NUnit discovery logic.

Mihai Cădariu
  • 2,407
  • 14
  • 17
1

Today I was running into this issue as well (on a .NET Framework 4.8 based NUnit project). The solution for me was to also install the Microsoft.NET.Test.Sdk package.

Kornelis
  • 101
  • 1
  • 5
0

To get to the root cause, it may help to attempt to run your tests using the NUnit CLI.

In my case, the CLI logged a bind failure I didn't see in Visual Studio. After I had fixed that, my tests ran correctly via CLI and VS.

tm1
  • 1,180
  • 12
  • 28
0

I got this error in a .Net 6.0 Asp.Net solution and here's how I solved it.

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":

enter image description here

In the Output is the error:

NUnit Adapter 4.3.1.0: Test discovery starting NUnit failed to load [dll path]
No test is available in [dll path]

What I did was comment out each class and bring them back one by one until the Tests would STOP to RUN. Then with the failing class put a break point on a [Test] method.

If you can't hit the break point then its failing in this classes [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..

AND THEN you get a prompt saying which DLL it can't load.

In my case it was “couldn’t load a DLL Microsoft.AspNetCore.Mvc.Core”

Referencing the DLL via Package Manager resolved the problem.

Edit: if this happens in a Unit Test you may want to reference the Microsoft.AspNetCore.App FrameworkReference rather than the Microsoft.AspNetCore.Mvc.Core package reference:

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321