Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 750
Echilon Avatar asked Jun 29 '09 13:06

Echilon


People also ask

Should unit tests be in a separate project?

Put Unit tests in the same project as the code to achieve better encapsulation. You can easily test internal methods, which means you wont make methods public that should have been internal.

How do you run parallel test cases in NUnit?

NUnit version 3 will support running tests in parallel: Adding the attribute to a class: [Parallelizable(ParallelScope. Self)] will run your tests in parallel.

How does NUnit decide what order to run tests in?

Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate. Tests do not wait for prior tests to finish. If multiple threads are in use, a test may be started while some earlier tests are still being run.


4 Answers

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.

like image 186
Pedro Avatar answered Sep 29 '22 12:09

Pedro


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.

like image 43
Tony O'Hagan Avatar answered Oct 01 '22 12:10

Tony O'Hagan


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.

like image 33
Ian Davis Avatar answered Oct 02 '22 12:10

Ian Davis


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

like image 32
Alexandr Nikitin Avatar answered Oct 01 '22 12:10

Alexandr Nikitin