在C#控制台应用程序中运行NUnit测试

我需要在控制台应用程序中以编程方式运行NUnit测试。 使用NUnit的nunit-console.exe不是一个选项。 我目前的代码是:

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

当我调用Load时,NUnit会在当前进程的目录中查找dll。 所以我得到一个类似“c:\ MyTestRunner \ bin \ debug \ MyTests.dll”的FileNotFoundException。

如何强制它在不同的目录中查找dll?

您可以更改当前目录

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

更新:

看来这并不是那么简单。 我环顾四周,有关于这个问题的文章和堆栈问题。

希望能帮助到你

我不确定它如何与NUnit的SimpleTestRunner()使用,但使用RemoteTestRunner()对我来说效果很好:

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

确保nunit.framework.dll库位于测试程序集的文件夹中。

试试RemoteTestRunner() ;)

我的理解是标准的nunit-console.exe在第二个应用程序域中运行其测试。 这样它就可以将AppDomainSetup.ApplicationBase属性设置为所有DLL所在的目录。 它还允许测试套件可选地拥有自己的MyTests.dll.config副本。

我不熟悉NUnit源代码,快速浏览不会显示任何可以设置第二个应用程序域的明显帮助程序类。 但是不应该有任何exception:创建一个带有更改的ApplicationBase属性的AppDomainSetup ; 创建一个新的AppDomain ; 在该app域中创建SimpleTestRunner ; 并像你正在做的那样调用Load