Resolving- Error: System.BadImageFormatException: Could not load file or assembly ‘*’ -When running Unit Tests

When attempting to run for the first time on a new machine the unit tests of a project, I got the following error message:

Unable to get type (Test method name), (Test project), Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.

Error: System.BadImageFormatException: Could not load file or assembly (Test project assembly information) or one of its dependencies.
An attempt was made to load a program with an incorrect format.

When attempting to resolve this issue, I came across a few solutions and decided to list what I found in hopes of helping someone else down the road

Changing the Test Settings

Go to Test -> Edit Test Settings -> Local

2015-05-15 09_32_42-ServiceNet.WebServices.Payment - Microsoft Visual Studio (Administrator)

 

 

 

 

Then  go to Hosts. Make sure Run tests in 64 bit process on 64 bit machine is selected.

2015-05-15 09_30_42-ServiceNet.WebServices.Payment - Microsoft Visual Studio (Administrator)

 

 

 

 

 

 

Note : The menu options might be different in the different versions of Visual Studio, but should be very similar to the above process.

Standardize the Build Platform

Another solution to try is to set your build platform to the same across the board. Go to your solution, right click, and select properties

SolutionProps

 

 

 

 

 

Select platform -> Select Any CPU.

Your next best bet is to go into the test project’s properties causing the error and explicitly set this as Any CPU. Even when selecting from above, it doesn’t change the build still.

PropBuild

 

 

 

 

 

Don’t Reference a Higher .NET Version

In your solution navigator, click the References project folder, and select the dlls in the project that is throwing the exception. Ensure the .NET versions referenced is either equal to or less then your hosting project.

 

Between the solutions above, I’ve solved this issue. That’s not to say though there is another solution not listed. If you do happen to find another solution, please leave a comment and I will add as soon as possible. Thanks!

Sources:

http://www.dotnetask.com/1421_20691932/

Jacob Saylor

Software developer in Kentucky

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: