如何运行C#code NUnit的测试 [英] How to Run NUnit Tests from C# Code
问题描述
我想写一个接收一个文件,并使用NUnit运行它简单的方法。 在code我设法建立使用NUnit的源不工作:
I'm trying to write a simple method that receives a file and runs it using NUnit. The code I managed to build using NUnit's source does not work:
if(openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
var builder = new TestSuiteBuilder();
var testPackage = new TestPackage(openFileDialog1.FileName);
var directoryName = Path.GetDirectoryName(openFileDialog1.FileName);
testPackage.BasePath = directoryName;
var suite = builder.Build(testPackage);
TestResult result = suite.Run(new NullListener(), TestFilter.Empty);
现在的问题是,我不断收到抛出builder.Build的异常,说明未找到该程序集。
The problem is that I keep getting an exception thrown by builder.Build stating that the assembly was not found.
我在想什么? 是否有运行在code测试(不使用的Process.Start)一些其他的方式?
What am I missing? Is there some other way to run the test from the code (without using Process.Start)?
推荐答案
添加以下行之初,树立NUnit的框架,可以帮助你:
Adding the following line at the beginning, sets up the NUnit framework and might help you:
CoreExtensions.Host.InitializeService();
另一个更简单的方式来执行NUnit的测试编程方式将是:
Another "easier" way to execute NUnit tests programmatically would be:
TestPackage testPackage = new TestPackage(@"C:\YourProject.Tests.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestResult testResult = remoteTestRunner.Run(new NullListener());
您需要参考以下组件:
-
nunit.core.dll程序
-
nunit.core.interfaces.dll
nunit.core.dll
nunit.core.interfaces.dll
当然,在 nunit.framework.dll中
必须在文件夹中有你的测试组件;)
And of course, the nunit.framework.dll
must be in the folder with your test assembly ;)
这篇关于如何运行C#code NUnit的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!