Thursday, October 11, 2007

NUnit Testing With ReSharper in Visual Studio 2005

I recently got a new computer running Windows Vista Business 64-bit. After installing Visual Studio 2005 and ReSharper 3.0 I started developing in C# using NUnit.

I really like the "Edit and Continue" feature of Visual Studio, which enables me to write code while in debug mode without restarting my application. However, this feature is not available for 64 bit applications. The 
workaround is to set the "Platform Target" to x86 in the Project Properties ->Build tab.

This works like a charm, but now ReSharper's NUnit runner fails when running the tests. I got the following message 
from ReSharper:

System.BadImageFormatException: Could not load file or assembly 'SomeProject.UnitTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

I wasn't happy with having to choose between being able to run unit tests and using the "Edit and Continue" feature, so I searched the web for clues. I tried running the unit test manually using the nunit-console.exe tool but that didn't work either, but a fix to that issue can be found here. The fix is to force the nunit-console.exe to run in 32 bit mode by using corflags.exe.

Now the tests can be run using the nunit-console, but it still doesn't work from ReSharper/Visual Studio. To solve that issue, simply use corflags.exe on ReSharper's TaskRunner (which is the proggy that starts the 
NUnit tests) to force it to run as 32 bit:

  • Open the Visual Studio Command Prompt
  • Navigate to the C:\Program Files (x86)\JetBrains\ReSharper\v3.0\vs8.0\Bin folder (At least that's where I found the ReSharper bin folder).
  • Run corflags.exe JetBrains.ReSharper.TaskRunner.exe /32Bit+(make sure to close all instances of Visual Studio to avoid a "Could not open file for writing" error).

Hope that helps any others that encounter the same problem.