开发者

Running NUnit Tests In a C# Console App

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开发者_如何学C I force it to look for the dll in a different directory?


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


I'm not sure how it would work with NUnit's SimpleTestRunner() but using the RemoteTestRunner() instead works fine for me:

TestPackage testPackage = new TestPackage(@"C:\YourProject.Tests.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestResult result = remoteTestRunner.Run(new NullListener());

Make sure, the nunit.framework.dll library is in the the folder with your test assembly.

Give the RemoteTestRunner() a try ;)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜