如何从 C# 代码运行 NUnit 测试

发布于 2024-09-01 09:18:30 字数 577 浏览 1 评论 0原文

我正在尝试编写一个简单的方法来接收文件并使用 NUnit 运行它。 我设法使用 NUnit 的源代码构建的代码不起作用:

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 抛出的异常,指出未找到程序集。

我缺少什么? 是否有其他方法可以从代码运行测试(不使用 Process.Start)?

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);

The problem is that I keep getting an exception thrown by builder.Build stating that the assembly was not found.

What am I missing?
Is there some other way to run the test from the code (without using Process.Start)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2024-09-08 09:18:30

在开头添加以下行,设置 NUnit 框架并可能对您有所帮助:

CoreExtensions.Host.InitializeService();

以编程方式执行 NUnit 测试的另一种“更简单”方法是:

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.framework.dll 必须位于测试程序集所在的文件夹中;)

Adding the following line at the beginning, sets up the NUnit framework and might help you:

CoreExtensions.Host.InitializeService();

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());

You need to reference the following assemblies:

  • nunit.core.dll
  • nunit.core.interfaces.dll

And of course, the nunit.framework.dll must be in the folder with your test assembly ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文