Echilon
Echilon

Reputation: 10244

NUnit tests in a separate project, same solution

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?

Upvotes: 5

Views: 7978

Answers (6)

Pedro
Pedro

Reputation: 12328

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.

Upvotes: 15

Tony O'Hagan
Tony O'Hagan

Reputation: 22682

Use the nunit-x86.exe instead of nunit.exe as your runner.

A better longer term solution may be to buy ReSharper that includes a much nicer test runner for NUnit that fully integrates into Visual Studio. It auto detects your .NET project type (x68 or x64). ReShaper comes with tons of other features of which unit testing is just one. Their test runner also integrates with their DotCover code coverage analyser.

You may find that you'll need a later version of Visual Studio to use ReSharper. The latest version works with Visual Studio 2013 Community Edition that you can get for free though I understand you may have issues upgrading some project features from such a rather old VS2005 project.

I don't have any affiliation with ReSharper.

Upvotes: 11

B... James B.
B... James B.

Reputation: 136

Go the the NUnit install (example: C:\Program Files (x86)\NUnit 2.6.3\bin) location and open nunit-86.exe.

Upvotes: 0

Alexandr Nikitin
Alexandr Nikitin

Reputation: 7436

Just set "Platform target" of Tests project to "x86".

Upvotes: 5

Ian Davis
Ian Davis

Reputation: 3858

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.

Upvotes: 7

Blair Conrad
Blair Conrad

Reputation: 241714

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.

Upvotes: 0

Related Questions