tags:

views:

525

answers:

3

I need to run NUnit tests programmatically in a console app. Using NUnit's nunit-console.exe is not an option. My current code is:

var testRunner = new SimpleTestRunner();
var package = new TestPackage("MyTests.dll", new List<string> { ("c:\MyTests\MyTests.dll" });
testRunner.Load(package);

When I call Load, NUnit looks for the dll in the current process's directory. So I get a FileNotFoundException for something like "c:\MyTestRunner\bin\debug\MyTests.dll".

How can I force it to look for the dll in a different directory?

+1  A: 

You can change the current directory

Directory.SetCurrentDirectory(@"c:\MyTestRunner\bin\debug\");

Update:

It seems this is not so simple. I`v looked around, there is an article and stack question on this issue.

Hope it helps

Am
+0: Does that actually fix the problem? of how Assembly.Load works?
Ruben Bartelink
In windows, dll loading is done so: current dir, system32 dir, paths directories. so it should fix the problem.
Am
Thanks, but this did not work. NUnit's Load method is still looking in the same directory.
Josh Wright
A: 

It has been a while since I ran into this. I think I got this working using method #3 in this: link. Basically the trick is to call the nunit core code to actually run the tests and resolve the assembly references, but with a reference to your assembly.

GrayWizardx
A: 

My understanding is that the standard nunit-console.exe runs its tests in a second app domain. This way it can set the AppDomainSetup.ApplicationBase property to the directory in which all the DLLs are located. It also allows the test suite to optionally have its own copy of MyTests.dll.config.

I'm not familiar with the NUnit source code, and a quick browse doesn't reveal any obvious helper classes that can set up a second app domain. However there shouldn't be anything out of the ordinary: create an AppDomainSetup with an altered ApplicationBase property; create a new AppDomain; create your SimpleTestRunner inside that app domain; and call Load like you're doing already.

Tim Robinson