NUnit: Could not load file or assembly 'some.d

2019-07-29 13:03发布

问题:

I have a C++/CLI DLL that uses some C++ DLLs (both built for 32 bit). I want to test it within a C# DLL by using NUNit 2.5.8.10295 (changing to 2.6.4 did not change the outcome)

I use a batch file which in the end gets expanded to this:

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

The tests run fine on my development PC (64-bit Win 10 Enterprise Visual Studio 2013) with this command. When starting nunit-console-x86.exe from Visual Studio Professional 2013 and setting working directory to the output folder of the two DLLs it works fine as well.

When executed on the build server (Jenkins running on Windows Server 2012 R2) the test fail with this message:

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)

I tried comparing the installed frameworks and runtimes on both machines (don't see obvious big differences) except buildserver using VS 2013 Express. I also experimented with SysInternals ProcessMonitor, but without success. The output of depency walker I do not understand.

Any hints on techniques or tools to use?

Update: If I add /noshadow it doesn't run on my development PC anymore as well. In Visual Studio then nunit-console-x86 crashes, while on command line I get the same message as on the buildserver. While there it doesn't make a difference if is run with or without /noshadow

Did a closer look on test PC with Windows 7 without any Visual Studio but with vc_redist 2013: With shadow copying enabled (= without /noshadow) I get this:

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

With /noshadow the error turns into a BadImageFormatException

回答1:

NUnit 2.x shadow copies files by default, but shadow copying does not copy native assemblies. I believe it is working in Visual Studio for you because you are setting the working directory.

To disable shadow copying in NUnit, pass the /noshadow command line option.

As an aside, the default behavior in NUnit 3 is to not shadow copy.



回答2:

In the end I managed to reduce complexity by creating a console application without NUNit, ran into similiar problems and fixed it. See x86 console application doesn't run on 64bit machines except in debugger for details.