测试复杂的自定义 WinForms 控件

发布于 2024-11-02 09:17:59 字数 369 浏览 1 评论 0原文

我正在开发相当复杂的 .NET 自定义控件(40K 行代码),但在测试它时遇到了一些麻烦。

我已经完成了几个示例项目,展示了控件的主要功能,但只能测试控件状态和操作的一小部分。

由于以下问题,单元测试也是无用的:

  • 大量的用例(例如,描述“项目选择”可能需要大约 4 页的规范)
  • 做同样事情的多种方法(也来自用户代码或 GUI)
  • 控制有许多状态和子状态,并且任何东西都可能无法在每个状态下工作
  • 如何测试设计时支持?

我知道这是GUI测试的一个常见问题,所以我想问一下您是否有任何成熟的测试自定义可视化组件的实践

感谢您的任何建议。

I am developing quite complex .NET custom control (40K lines of code) but have some trouble testing it.

I have done several sample projects demonstrating main features of the control, but one can test only small subset of control states and operations.

Unit tests are also useless, because of these problems:

  • tremendous number of use cases (e.g. describing "item selections" can take some 4 pages of specs)
  • many ways of doing the same thing (and also from user-code or GUI)
  • the control have many states a sub-states, and anything may not possibly work in every state
  • how to test design-time support?

I know this is a common problem of GUI testing, so I would like to ask you if there are any well-estabilished practices of testing custom visual components?

Thanks for any advice.

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

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

发布评论

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

评论(1

暮光沉寂 2024-11-09 09:17:59

最主要的是将 GUI 测试限制在最低限度,因为这是最昂贵的测试方式。在 40K 行代码中,我敢打赌 90% 的代码根本不能真正与 WinForm GUI 元素一起工作。因此,大部分内容可以通过单元测试来覆盖,但这取决于您构建代码的方式。

需要考虑的事情:

  • SOLID原则,尤其是单一责任原则(SRP)——你应该依赖大量的小型“服务”接口和接口,而不是几个巨大的“上帝”类。只做一件事并且做得很好的类。您可以为这些编写单独的测试,然后在知道它们工作正常后将它们组装成一个更大的图片。
  • MVP 模式(实际上是被动视图):WinForm GUI 代码应该只是一个薄层(视图),其他所有内容都应该在 Presenters 和 Model 中。
  • 演示者优先方法

我从事相对复杂的 WinForms 应用程序,这些模式从未让我失望过。

至于 GUI 测试,我使用 UI Automation,但仅适用于某些标准案例。其他一切都由非 GUI 单元测试涵盖。

The main thing is to limit the GUI testing to a minimum, because it's the most expensive way to test. In 40K lines of code I bet 90% of code doesn't really work with the WinForm GUI elements at all. So most of it could be covered by unit tests, but this depends on the way you structured your code.

Things to consider:

  • SOLID principles, especially the Single responsibility principle (SRP) - instead of a couple of huge "God" classes, you should rely on a large number of small "service" interfaces & classes which only do one thing and do it well. You can write isolated tests for these and then assemble them into a larger picture once you know they work OK.
  • MVP pattern (Passive View actually): the WinForm GUI code should be only a thin layer (View), everything else should be in Presenters and the Model.
  • Presenter First approach.

I work on a relatively complex WinForms application and these patterns have never failed me.

As for GUI testing, I use UI Automation, but only for some standard cases. Everything else is covered by non-GUI unit testing.

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