System.BadImageFormatException caused by NUnit project

浪子不回头ぞ 提交于 2019-11-29 03:18:37

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

Theresa Forster

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

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

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.

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

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.

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

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!