C#/.NET:使用 NUnit 测试 BackgroundWorker

发布于 2024-07-17 06:51:32 字数 624 浏览 10 评论 0原文

当使用 NUnit 控制台运行程序运行时,此测试失败。 如果我只使用 TestDriven.NET 运行该测试,它会起作用,但如果我使用 TestDriven.NET 运行整个套件,则不会:

[Test]
public void BackgroundWorkerFiresRunWorkerCompleted()
{
  var runner = new BackgroundWorker();
  ManualResetEvent done = new ManualResetEvent(false);
  runner.RunWorkerCompleted += delegate { done.Set(); };

  runner.RunWorkerAsync();

  bool res = done.WaitOne(TimeSpan.FromSeconds(10));
  // This assert fails:
  Assert.IsTrue(res, "RunWorkerCompleted was not executed within 10 seconds");
}

我怀疑问题与没有消息循环有关,但我不确定。

使用BackgroundWorker有什么要求?

是否有解决方法可以使测试正常进行?

This test fails when it is run with the NUnit console runner. It works if I run just that test with TestDriven.NET, but not if I run the entire suite with TestDriven.NET:

[Test]
public void BackgroundWorkerFiresRunWorkerCompleted()
{
  var runner = new BackgroundWorker();
  ManualResetEvent done = new ManualResetEvent(false);
  runner.RunWorkerCompleted += delegate { done.Set(); };

  runner.RunWorkerAsync();

  bool res = done.WaitOne(TimeSpan.FromSeconds(10));
  // This assert fails:
  Assert.IsTrue(res, "RunWorkerCompleted was not executed within 10 seconds");
}

I suspect the problem have something to do with not having a message-loop, but I am not sure.

What are the requirements for using BackgroundWorker?

Is there a workaround to make the test work?

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

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

发布评论

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

评论(3

农村范ル 2024-07-24 06:51:32

我不认为这与消息泵有关,因为测试通常以阻塞方式运行。 但该事件可能是在“UI”线程上调用的,该线程使用未得到处理的 Windows 消息。

因此,如果问题是消息泵或 Windows 消息未得到处理,您可以尝试像这样替换当前的 bool res = done.WaitOne(TimeSpan.FromSeconds(10)); 行:

DateTime end = DateTime.Now.AddSeconds(10);
bool res = false;
while ((!res) && (DateTime.Now<end)) {
   Application.DoEvents();
   res = done.WaitOne(0):
}

I don't think that it has to do with the message pump, since tests usually run in a blocking fashion anyways. But it may be that the event is invoked on the "UI" thread, which uses a windows message which does not get handled.

So, if the problem was the message pump or windows messages not being handled, you could try like this to replace your current bool res = done.WaitOne(TimeSpan.FromSeconds(10)); line:

DateTime end = DateTime.Now.AddSeconds(10);
bool res = false;
while ((!res) && (DateTime.Now<end)) {
   Application.DoEvents();
   res = done.WaitOne(0):
}
旧街凉风 2024-07-24 06:51:32

您是否缺少 BGW DoWork 事件处理程序?

Are you missing a BGW DoWork event handler?

近箐 2024-07-24 06:51:32

只需在以下内容之前添加一个睡眠:

Assert.IsTrue(res, "RunWorkerCompleted was notexecute inside 10秒");

像这样的东西:

System.Threading.Thread.Sleep(10000);< /代码>

Just add a Sleep before your:

Assert.IsTrue(res, "RunWorkerCompleted was not executed within 10 seconds");

Something like this:

System.Threading.Thread.Sleep(10000);

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