XUnit Error: System.BadImageFormatException

28. December 2009 Uncategorized 0

As I said in my last post, I’ve been trying out XUnit.Net for a project I’m working on. When I loaded the assembly in Visual Studio, wrote my test and tried to execute it, I kept getting this error:

System.BadImageFormatException: Could not load file or assembly ‘ItsGreekTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

I had my DLL that I was testing, as well as a separate project with my tests. So my first thought was that I added my test DLL as a specific version, and did not use the “Project” reference. This could cause versioning conflicts between the two, and might possibly cause the project to not find my DLL.

I quickly realized that wasn’t the case, by simply checking my references.

I did some Googling on “BadImageFormatException” and realized what had happened. My DLL under test was using SQL Server Compact for the database. Even though I’m compiling on a processor with 64 bit support, SQL Server Compact doesn’t support 64 bit (although it appears SP1 supports it). So I had to change my DLL to compile only on x86.

That, in itself, wasn’t the issue. What caused the issue was the command I used in my Post-Build Events:
C:DevToolsTestingxunit-1.5xunit.console.exe $(TargetFileName)
This is the 64 bit version of xunit. The fix was simple, I changed the Post-Build Event to:
C:DevToolsTestingxunit-1.5xunit.console.x86.exe $(TargetFileName)

From there on, whenever I ran a build, my tests would run without incident (except, of course, when I did something stupid and broke a test.)