System.BadImageFormatException caused by NUnit project
Asked Answered
D

11

21

Good day everyone. I have been having the same problem all day at work and am struggling to find any new paths to go down.

I am getting the following error when my solution builds on server. I have no problem running/debugging all tests in the solution and it builds fine. Both server and my PC are x64. I have followed a lot of advice which I have found to no avail.

I have set Platform Target to x86 for all projects in my solution under all configurations.

I am aware that there is an nunit-console-x86.exe which could make all the difference but I'm not sure where to specify this in the code.

Please realise I have trail-blazed the internet, so apologies if I have missed something.

System.BadImageFormatException: Could not load file or assembly
'Spin.TradingServices.DataAcquisition.Test.NUnit, Version=1.0.12103.2060, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'Spin.TradingServices.DataAcquisition.Test.NUnit, Version=1.0.12103.2060, Culture=neutral, PublicKeyToken=null'

Server stack trace: at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.Assembly.Load(AssemblyName assemblyRef) at NUnit.Core.Builders.TestAssemblyBuilder.Load(String path) at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, Boolean autoSuites) at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName, Boolean autoSuites) at NUnit.Core.TestSuiteBuilder.BuildSingleAssembly(TestPackage package) at NUnit.Core.TestSuiteBuilder.Build(TestPackage package) at NUnit.Core.SimpleTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.RemoteTestRunner.Load(TestPackage package) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at NUnit.Core.TestRunner.Load(TestPackage package) at NUnit.Util.TestDomain.Load(TestPackage package) at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options) at NUnit.ConsoleRunner.Runner.Main(String[] args)

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

http://app1017-build.oy.gb.sportingindex.com:8080/job/TradingServices.DataAcquisition-Dev/ws/DataAcquisition/build.proj(86,5): error MSB6006: "nunit-console.exe" exited with code -100. Done Building Project " (default targets) -- FAILED.

Build FAILED.

PLEASE NOTE: We have reverted our build on Hudson and now re-committing files more gradually. I will report back on how this goes. Tried get a few heads involved on this one to no avail unfortunately. Shame!

Update I haven't been back to this page for a while but it looks like there are lots of different solutions. If I could mark them all as the answer I would! Those of you finding your way here should probably give equal credit to each option.

Descender answered 26/3, 2012 at 15:36 Comment(1)
For people that found this question via search engine, but their problem occurs on a build server like Jenkins — have a look at this question: #58876301Role
F
7

Check the target framework version of your assembly are same as nUnit test runner supports. See runFile.exe.config for list of supported runtimes.

Also if you have megrated from FW 3 to FW 4, they has different runtime (CLR is different).

Florentinoflorenza answered 26/3, 2012 at 15:58 Comment(2)
All of our projects are using .Net Framework 4. And have been since I started here. These tests were running last week until a commit on Friday, and its just hard to tell what change has been made. Supported RunTimes are from v1.0.3705 to v 2.0.50727Descender
FW 4 runtime is 4.[yah i don't remember next :)] Runtime 2.xxx is FW 2, 3, 3.5. So try to look forward this way. Update nUnit, verify paths... etc.Florentinoflorenza
S
61

I had this problem with a console app on X64 pc, the build was set as x86 and it still crashed. I went into Properties on the Console App and under build I changed my Platform Target from x86 to Any CPU then suddenly all the tests worked and ran successfully.

of note, the Configuration Manager's platform field can "lie" to you and doesn't actually have to reflect what the project's properties are actually configured to do. My configuration manager said that "Common.dll" was being built as "Any CPU", but the project properties (the setting that really matters) was building it as "x86".

enter image description here

Susurration answered 11/4, 2012 at 15:3 Comment(2)
Yeah, I think we must have tried this. It turned out to be a completely unrelated part of our project which no-one in our team had worked on. So we commented it out (unused now) and it worked... Nothing learned!Descender
In a lot of cases, you can just change the platform target but in other, you simply can't. For example, if you have to connect to MSAccess database, the platform target must be x86. I know, it's bullsh... but hey guys, it's Microsoft.... a hate/love relationship. :DAtrium
P
10

Make sure that this setting is correct: Test menu -> Test Settings -> Default Processor Architecture -> x64 / x86. It was incorrect in my case and it failed with the same issue.

Postdoctoral answered 7/4, 2015 at 0:14 Comment(1)
In VS2019 this is now a Top Level Menu Setting. Test > Processor Architecture for Any CPU Projects > (choose setting)Groundling
Z
9

A minor addition to all answers. You might need to change the NUnit runner platform (Resharper for me) itself as it was in my case. See example enter image description here

Zigrang answered 29/7, 2014 at 11:10 Comment(0)
F
7

Check the target framework version of your assembly are same as nUnit test runner supports. See runFile.exe.config for list of supported runtimes.

Also if you have megrated from FW 3 to FW 4, they has different runtime (CLR is different).

Florentinoflorenza answered 26/3, 2012 at 15:58 Comment(2)
All of our projects are using .Net Framework 4. And have been since I started here. These tests were running last week until a commit on Friday, and its just hard to tell what change has been made. Supported RunTimes are from v1.0.3705 to v 2.0.50727Descender
FW 4 runtime is 4.[yah i don't remember next :)] Runtime 2.xxx is FW 2, 3, 3.5. So try to look forward this way. Update nUnit, verify paths... etc.Florentinoflorenza
K
5

I come across this often, when I test against Console or WPF application. A few causes are

  • First, make sure you application doesn't run on .Net Framework 3 or 4 Client profile
  • Then compare the target framework .NET version on both application and test projects. They should be the same
  • Check Platform target on build tab. They should be the same. For example, if test project has "Any CPU" target, the application must have "Any CPU".
Kaitlin answered 10/9, 2012 at 9:12 Comment(0)
F
4

For me, the exception was caused by invalid /process=single argument of nunit. If I change the value, then the exception is gone:

/process=separate

or

/process=multiple

p/s: late answer, but just for reference..

Forland answered 6/3, 2014 at 11:17 Comment(0)
M
3

This might sound stupid, but, check your project and processor architectures. In my case, I was running 64-bit tests on what I assumed was a 64-bit machine (since it was building 64-bit projects).

Guess what? It was actually a 32-bit machine. So NUnit.exe ran as 32-bit (obviously), and can't understand 64-bit tests.

The solution? Build a x86 and x64 version of your tests, and run the x86 ones on the x86 machines, and the x64 ones on the x64 machines.

Obvious. But worth checking out.

Marielamariele answered 3/5, 2012 at 18:16 Comment(0)
V
2

My situation was a bit different. The error appeared quite randomly after running a Unit Test that had been modified.

I removed the test and the error went away.

I recreated the test and the error came back.

I renamed the test and the error went away.

I renamed the test back and the problem did not happen again.

Hope this helps!

Ben

Verbalism answered 12/11, 2014 at 16:30 Comment(0)
D
1

Thanks Ashes999 for waking me up.

This problem did return again. And rolling back and uploading with didn't work. It turned out to be a monitor object which we no longer even use. Commented out and fixed.

The way to find this is by doing Debug All Unit Tests. Fix everywhere it pauses. If t doesn't pause then err.

Descender answered 3/5, 2012 at 17:50 Comment(1)
Thanks for the details. I don't even use monitors, so this didn't fix my problem. I'll post my own answer.Marielamariele
B
1

For anyone still having this issue, you also may need to set the framework field on the test runner to match the one in your included project (The field just to the right of the one referenced by AlexM).

In my case, I was writing a test suite for a Windows Phone 8 library, and NUnit was unable to correctly figure out the framework version that it should use.

Breland answered 13/2, 2015 at 19:43 Comment(0)
I
0

I was also getting same issue.

For me solution was to uncheck the "Prefer 32-bit" checkbox on project properties.

The project properties Build tab showing the Prefer 32-bit option

Izanagi answered 19/4 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.