views:

1453

answers:

2

I have some assembly that references NUnit and creates a single test class with a single test method. I am able to get the file system path to this assembly (e.g. "C:...\test.dll"). I would like to programmatically use NUnit to run against this assembly.

So far I have:

var runner = new SimpleTestRunner();
runner.Load(path);
var result = runner.Run(NullListener.NULL);

However, calling runner.Load(path) throws a FileNotFound exception. I can see through the stack trace that the problem is with NUnit calling Assembly.Load(path) down the stack. If I change path to be something like "Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" then I still get the same error.

I have added an event handler to AppDomain.Current.AssemblyResolve to see if I could manually resolve this type but my handler never gets called.

What is the secret to getting Assembly.Load(...) to work??

+3  A: 

"What is the secret to getting Assembly.Load to work?"

System.Reflection.Assembly.Load takes an string containing an assembly name, not a path to a file.

If you want to load an assembly from a file use:

Assembly a = System.Reflection.Assembly.LoadFrom(pathToFileOnDisk);

(LoadFrom actually uses Assembly.Load internally)

By the way, is there any reason why you can;t use the NUnit-Console command line tool and just pass it the path to your test assembly? You could then just use the System.Diagnostics.Process to run this from within your client application, might be simpler?

Ash
Well I wish I could use LoadFrom but unfortunately the Assembly.Load is being called from within NUnit not my code so I can't control it.
justin.m.chase
I probably will need to just call it through a process like you suggest... I would rather have objects for my own display purposes but it should be good enough to just use a process for now.
justin.m.chase
+5  A: 

If you want to open in a console mode, add nunit-console-runner.dll reference and use:

NUnit.ConsoleRunner.Runner.Main(new string[]
   {
      Assembly.GetExecutingAssembly().Location, 
   });

If you want to open in a gui mode, add nunit-gui-runner.dll reference and use:

NUnit.Gui.AppEntry.Main(new string[]
   {
      Assembly.GetExecutingAssembly().Location, 
      "/run"
   });

This is the best approach because you don't have to specify any path.

Ricibald