访问nunit中的UI线程
我正在尝试编写一个必须访问 UI 线程的测试用例。 Nunit 是否提供了访问 ui 线程的方法,或者我可以在测试用例中在 UI 线程上执行一些代码。 谢谢
更新:我正在使用winform。 通常的方法是拥有表示层并针对它编写测试用例,但在这种情况下,我需要使用一个网格并填充它,需要在 UI 线程上设置其 DataSource 属性。
I am trying to write a test case where I have to access UI Thread.
Does Nunit provide a way to access the ui thread or is there anyway I can execute some code on UI Thread in my test case.
Thanks
Update: I'm using winform. The usual way is to have the presentation layer and write the test cases against it but in this situation I have a grid that I need to work with and to populate it, its DataSource property needs to be set on UI Thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这在很大程度上取决于您的 UI 使用哪种技术。 根据这一点,可能可以将 NUnit 运行程序线程设置为您的 UI 线程。
但一般来说,建议使实际 UI 层尽可能薄,以减少要测试的 UI 代码量。
如果您确实需要在 NUnit 线程上拥有实时 WinForms 控件,请考虑使用 Application.DoEvents() 它将处理消息队列中所有当前待处理的事件。 请注意,这样做可能会给您带来其他问题。
That very much depends on which technology you are using for your UI. Depending on that it might be possible to setup the NUnit runner thread to act as your UI thread.
Genrally though, it is recommended to make the actual UI layer as thin as possible to reduce the amount of UI-code to test.
If you really need to have a live WinForms control on your NUnit thread, consider using Application.DoEvents() which will handle all currently pending events in the message queue. Be aware that doing so might bring you other problems though.
您可以尝试 NUnit Forms,但我赞同 David 的建议,即尽可能多地从用户界面层。
You could try NUnit Forms, but I'd second David's recommendation to take as much logic as possible out of the UI layer.
您可以在测试中指定一个“RequiresSTA”属性,该属性将在与 NUnit UI 相同的线程中运行测试。
我在尝试为 TIBCO Rendezvous 消息编写测试时发现了这一点。 侦听器必须在主 UI 线程中设置(责怪 TIBCO,而不是我!),否则对 getAutoDispatchQueueGroup 的调用将返回错误“对象无法完成请求的操作”。
我尝试使用 WindowsFormsSynchronizationContext 和 BeginInvoke,但都不起作用。
There is a 'RequiresSTA' attribute you can specify on the test that will run it in the same thread as the NUnit UI.
I discovered this when trying to write a test for a TIBCO Rendezvous message. The listener has to be set up in the main UI thread (blame TIBCO, not me!), otherwise the call to getAutoDispatchQueueGroup returns an error "Object cannot complete the requested operation".
I tried using WindowsFormsSynchronizationContext and BeginInvoke, and neither worked.
对于 NUnit 3+,我通过使用
[TestFixture, Apartment(ApartmentState.STA)]
声明测试类或使用[Test, Apartment(ApartmentState.STA) 声明测试方法来实现此目的)]
。另请参阅此相关问题。
For NUnit 3+, I got this working by declaring on a test class with
[TestFixture, Apartment(ApartmentState.STA)]
or on a test method with[Test, Apartment(ApartmentState.STA)]
.Also see this related question.
NUnit 没有内置支持 AFAIK。 当然,您可以在 UI 线程中执行代码,但“如何”取决于您使用的 UI 技术(WPF 或 Winforms)。
查找类似
BeginInvoke()
的内容并将匿名委托传递给它你可以在你的单元测试中定义NUnit has no builtin support AFAIK. You can execute code in your UI thread of course, but the 'how' depends on the UI technology you use (WPF or Winforms)
Look for something like
BeginInvoke()
and pass an anonymous delegate to it that you can define in your unit test