NUnit tests in a separate project, the 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 an exception below after loading the tests, and the tests fail. I added the main project as a reference, and I have $ (ProjectDir) bin / Debug / $ (TargetName) $ (TargetExt) in the arguments for NUnit in the external tool settings, with an empty source 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 cleaning for hours, the only thing I found was an error in VS2005, which mentions the / bin and / obj directories, but the answer provided did not help.

Any solutions?

+4
source share
6 answers

Instead of setting up NUnit as an external tool, I set up the unit test project as a StartUp project. On the "Project Properties" screen, set the "Start" action to "Launch an external program" and point it to nunit.exe . In the "Launch Options" section, I specify the test build (no path) in the "Command Line Arguments" field. At this point, just press F5 to start NUnit.

+14
source

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

A better long-term solution would be to buy ReSharper, which includes a much more enjoyable test runner for NUnit, which integrates fully with Visual Studio. It automatically detects your type of .NET project (x68 or x64). ReShaper comes with tons of other features , of which unit testing is just one thing. Their test runner also integrates with the DotCover code coverage analyzer .

You may find that you will need a later version of Visual Studio to use ReSharper. The latest version works with Visual Studio 2013 Community Edition , which you can get for free, although I understand that you may have problems updating some project features from such an old VS2005 project.

I have no connection with ReSharper.

+10
source

Do you work on x64? You will get this error if you download x64 bits from x86 and vice versa. In addition, the path you are trying to create must be a $ (TargetPath) macro.

+7
source

Just set the Platform Target of the Test project to x86.

+5
source

Is your main project.exe or .dll? Older versions of .NET could not reference .exe, so this could be a problem.

In any case, I would expect problems if the main assembly did not get anywhere accessible by your test assembly (for example, in the same directory). You can check this, and if it is not, it is possible if Visual Studio copies the link (main) to the local directory.

"An attempt was made to download a program with the wrong format." makes me wonder if the theory of "missing collections" is correct, but without additional information, this is the best I can think of.

0
source

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

0
source

All Articles