views:

887

answers:

3

I have a solution containing my main project and a test project using NUnit. Everything compiles but when I run NUnit I get the exception below after the tests load, and the tests fail. I've added the main project as a reference, and I have $(ProjectDir)bin/Debug/$(TargetName)$(TargetExt) in the arguments for NUnit in the external tools setup, with a blank initial directory.

MyMainProjectTests.Database.TestAddDelete:   
System.BadImageFormatException : Could not load file or assembly 'MyMainProject, 
    Version=1.1.1.0, Culture=neutral, PublicKeyToken=null' or one of its 
    dependencies. An attempt was made to load a program with an incorrect format.
TearDown : System.Reflection.TargetInvocationException : Exception has been 
    thrown by the target of an invocation.
  ----> System.BadImageFormatException : Could not load file or assembly 
    'ChickenPing, Version=1.1.1.0, Culture=neutral, PublicKeyToken=null' or one 
    of its dependencies. An attempt was made to load a program with an incorrect 
    format.

After scouring for hours the only thing I've found is a bug in VS2005 which mentions the /bin and /obj directories, but the answer provided didn't help.

Any solutions?

A: 

Is your main project a .exe or a .dll? Older versions of .NET couldn't reference an .exe, so that might be the problem.

In either case, I'd expect problems if the main assembly didn't end up somewhere accessible by your test assembly (for example, in the same directory). You could check that, and if not make it so, perhaps by having Visual Studio copy the referenced (main) assembly to the local directory.

The "An attempt was made to load a program with an incorrect format." makes me wonder if the "missing assembly" theory is right, but without more info, it's the best guess I can think of.

Blair Conrad
A: 

Are you running on x64? You will get that error if loading a x64 bit from x86 and vise versa. Also, the path you are trying to create should be the $(TargetPath) macro.

Ian Davis
I am, yes. What do you mean by the path I'm trying to create. I'm pretty new to this, could you post how you have NUnit setu[ in external tools please?
Echilon
+4  A: 

Instead of setting up NUnit as an External Tool, I set the unit test project as the StartUp project. In the project's Properties screen, set the Start Action to "Start external program" and point it to nunit.exe. In the Start Options section, I specify the test assembly (no path necessary) in the "Command line arguments" box. At this point, simply press F5 to start up NUnit.

Pedro
Give this man an upvote! This is really nice because you can step through your unit tests while the NUnit GUI is running.
emddudley