热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

ThistestfailswhenitisrunwiththeNUnitconsolerunner.ItworksifIrunjustthattestwith

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:

使用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");
}

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?

使用BackgroundWorker有哪些要求?

Is there a workaround to make the test work?

是否有解决方法使测试工作?

3 个解决方案

#1


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.

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

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:

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

DateTime end = DateTime.Now.AddSeconds(10);
bool res = false;
while ((!res) && (DateTime.Now

#2


Are you missing a BGW DoWork event handler?

你错过了一个BGW DoWork事件处理程序吗?

#3


Just add a Sleep before your:

只需在您之前添加睡眠:

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

Assert.IsTrue(res,“RunWorkerCompleted未在10秒内执行”);

Something like this:

像这样的东西:

System.Threading.Thread.Sleep(10000);


推荐阅读
author-avatar
mobiledu2502862217
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有