Debugging Nunit Tests inside VS2010 Express

9

I have written a bunch of unit tests inside VS2010 Express and tests being tests they sometimes fail. Since the express editions of VS don't allow plugins to run I can't simply spin up TestDriven.Net or an equivalent and debug the tests. To try and work around this I've converted my test assembly into a console app and made the main method look like this:

class CrappyHackToDebugUnitTestInVSExpress
{
  public static void Main()
  {
     AppDomain.CurrentDomain.ExecuteAssemblyByName(
          @"C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit-console.exe",
          new [] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });
  }
}

In theory I should be able to run this up, set break points in my test. If it worked it would be an acceptable work around, but I keep getting the following:

FileLoadException
Could not load file or assembly 'C:\\Program Files\\NUnit 2.5.5\\bin\\net-2.0\\nunit-console.exe' 
or one of its dependencies. The given assembly name or codebase was invalid. 
(Exception from HRESULT: 0x80131047)

Now the file exists and when run manually nunit-console runs fine. What might be my problem?

visual-studio
unit-testing
nunit
asked on Stack Overflow Jun 8, 2010 by ilivewithian

3 Answers

7

Basically you need to convert your assembly to Windows Forms app, add reference to the nunit-gui-runner.dll assembly and change your Main method to look like this:

    [STAThread]
    static void Main()
    {
        NUnit.Gui.AppEntry.Main(new string[] { Assembly.GetExecutingAssembly().Location });
    }

here is another example:

...
using NUnit.Gui;

namespace __libs
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            NUnit.Gui.AppEntry.Main(new string[] { @"C:\test\bin\Debug\test.exe" });
        }
    }
}

This will allow you to step into certain tests but is not very good for a red green cycle, so you will want to use this only when debugging and not in other circumstances.

answered on Stack Overflow Sep 22, 2010 by Dmitry Osinovskiy • edited Mar 6, 2014 by Dmitry Osinovskiy
6

I played with your concept and it appears the issue isn't directly from loading the file, but from dependencies.

I used the following modified code:

And the error was actually a failure to locate nunit.core.dll, which is in the /lib directory.

 try
        {
            String NUnitPath = @"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe";

            AssemblyName asmName = System.Reflection.AssemblyName.GetAssemblyName(NUnitPath);

            AppDomain.CurrentDomain.ExecuteAssemblyByName(asmName, new[] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });

        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
            Trace.WriteLine(ex.StackTrace);
        }

(I like getting System.Reflection.AssemblyName because you can inspect and see that everything's in order verses the raw file path.)

A quick bulk copy (xcopy nunit.*.dll) into my test projects' debug directory and it ran just fine. (It should be trivial to discover the minimal dependencies required)

Tested in VC# 2010 Express with NUnit 2.5.7 (breakpoints work, but I didn't really play with any other options.) Although I'm sure you could make a passable build option from it.

Cheers!

PS - First post here so I'm a bit untested as to getting the 'code' blocks formatted. Sorry in advance..

answered on Stack Overflow Aug 3, 2010 by Andre White
2

I had a similar problem trying to debug unit tests in VS C# express. Had a hard time getting it to work properly but then I found out about this project template. Works perfectly in C# Express!

http://visualstudiogallery.msdn.microsoft.com/b8a7a8fa-9f5a-4b9b-8e8b-8839a4364f26?SRC=VSIDE

C# Project Template

Integrated tests with Visual Studio, including Visual C# Express version

Self contained NUnit console runner. Allow to write test fixtures and test, running from Visual Studio simply by pressing F5 (support test debugging), or Ctrl-F5 free run with results in console window. In case of test failure indicate by beep sound.

Contains essential NUnit modules to start test project. No external dependencies. Simply create new project, using NUnit Test Application template.

answered on Stack Overflow Jun 14, 2012 by Borundin

User contributions licensed under CC BY-SA 3.0