views:

409

answers:

2

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?

+2  A: 

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..

Andre White
A: 

See this blog post. 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 });
    }

Also read this article, especially about issues with AppDomain in the third method. It also applies here.

Dmitry