使用 NUnit 测试弹出窗口
我正在使用 VS2005 和 NUnit。
我想知道是否有一种方法可以仅使用 NUnit 来测试弹出窗口。
button1.click();<-Find/Assert if the popup window has been opened->
I am using VS2005 and NUnit.
I would like to know if there is a way to test popups using just NUnit.
button1.click();<-Find/Assert if the popup window has been opened->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恕我直言,对 UI 进行单元测试通常是一个坏主意。 UI 的本质使得它们对于自动化测试来说是有问题的。 它们是流动的,并且变化频繁,比其他代码更频繁,其方式会破坏任何测试。 许多自动化测试只能通过拍摄图像快照来解决,这显然非常脆弱。 时间问题变得极其成问题。 最后,这种自动化依赖于对整个 UI 的控制,包括键盘和鼠标等 I/O 设备。 这意味着在运行测试时不能在系统上执行其他活动,以免破坏正在运行的测试。 最好将尽可能多的功能从 UI 中移出(例如 演示模型< /a> 帮助解决这个问题)并测试该功能。
也就是说,如果您必须和/或坚持为 UI 创建自动化测试,那么最好使用专门为此设计的自动化库。 对于 WinForms 和 WPF,White 是此类库的一个很好的示例。
IMHO, it's generally a bad idea to unit test the UI. The very nature of UIs make them problematic for automated testing. They're fluid, and are changed frequently, much more frequently than other code, in ways that will break any tests. Many automated tests are solvable only by taking image snapshots, which is obviously very brittle. Timing concerns become extremely problematic. And finally, such automation relies on taking control of the entire UI, including I/O devices such as the keyboard and mouse. This means that no other activity can be done on the system while the tests are run for fear of breaking the running tests. It's much better to move as much of the functionality out of the UI as humanly possible (patterns such as Presentation Model help with this) and test that functionality instead.
That said, if you must and/or insist on creating automated tests for the UI, you're best served to use an automation library designed just for this. For WinForms and WPF, White is a good example of such a library.
如果您正在测试弹出窗口是否已打开,那么我想您正在为浏览器工作,而不是为您的项目工作:)
这种功能是使用 NMock 或 Rhino.Mocks,在它创建了一个模拟对象,它实际上相当于您的 UI 对象,并且它被“模拟”为具有您的 UI 组件的功能,该功能用于测试您期望 UI 组件/对象传递的功能。
If you are testing whether the popup window has opened or not, then I suppose you are working for the Browser and not your project :)
This kind of functionality is tested using NMock or Rhino.Mocks, In which a mock object is created which is in tern equivalent of your UI object, and its "mocked" to have functionality of your UI component, which is used to test the functionality you would expect the UI component/object to pass.
我已经使用 FindWindowEx 和 CloseWindow 非托管函数来自动执行您尝试执行的操作,如果您确信知道弹出窗口的标题,这可能是合适的。 (我假设这是在 Win32 平台上)。 我用它来测试一个创建模式对话框的函数,并且在对话框关闭之前不会将控制权返回到原始线程(它断言已成功找到并关闭窗口)。 这不太符合 NUnit 的规定,但它符合我的目的。
如果这听起来对您有用,我可以生成一个片段,但这是一个相当具体的用例。
I have used the
FindWindowEx
andCloseWindow
unmanaged functions to automate what you are trying to do which may be suitable if you know, with confidence, the title of the popup window. (I am assuming that this is on a Win32 platform). I used it to test a function that creates a modal dialog and does not return control to the original thread until the dialog has been closed (it asserts that the window is found and closed successfully). This isn't quite NUnit kosher, but it worked for my purposes.If this sounds useful to you, I can produce a snippet, but it's a fairly specific use case.