The System.BadImageFormatException error when unit-testing in Visual Studio and its solution

The BadImageFormatException was unhandled – An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) exception is being thrown when you try to load a .dll of your application which was compiled with 32 bits (X86) or 64 bits (X64) and the process that runs this file runs in a different bit-environment than the file.

If you see this exception when you run your C# unit tests in Visual Studio, then the reason for this error is that the test environment of Visual Studio has a different bit setting than the compiled code which is under test. To fix that you have to do the following change in your Visual Studio test settings:

In the main menu of Visual Studio click on TEST > Test Settings > Default Processor Architecture > and choose X86 or X64 based on which platform is your code compiled.

Set the correct platform for your unit tests in Visual Studio

comments powered by Disqus