NUnit tests in a separate project, same solution

半腔热情 提交于 2019-12-12 11:33:14

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/1058167/nunit-tests-in-a-separate-project-same-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!