单元测试遗留代码:“提取和覆盖”的限制与 JustMock/TypeMock/moles 相比?

发布于 2024-11-30 05:42:42 字数 490 浏览 1 评论 0原文

考虑到以下条件:

  • 一个非常古老、庞大的 C# 遗留代码库,没有任何测试覆盖
  • (几乎)每个类都从某个接口派生,
  • 没有任何内容是密封的

使用分析器 API 驱动的解决方案(例如 JustMock 和 TypeMock)相比有什么实际好处使用 extract&override + 例如 RhinoMocks?除了规避私有/受保护之外,是否存在我不知道的情况,确实需要使用 TypeMock/JustMock 等?我特别欢迎那些改用其中一种产品的人提供一些经验。

使用 extract&override 似乎可以解决处理旧遗留代码时的所有问题,重构看起来非常简单,并且引入错误的可能性似乎很小。好处是编写更少的测试代码吗?更漂亮的类,更少的虚拟保护的东西?现在,我不“明白”,尽管我知道首先单独测试私有方法非常有帮助,因为在这种旧的遗留代码库中,公共方法可能太大。

如果您不知道 extract&override 是什么:请参阅

Given the following conditions:

  • a very old, big, C# legacy code base with no testcoverage whatsoever
  • (almost) every class derives from some interface
  • nothing is sealed

What are the practical benefits of using profiler-API-driven solutions like JustMock and TypeMock, compared to using extract&override + e.g. RhinoMocks? Are there cases I'm not aware of, besides circumventing private/protected, where using TypeMock/JustMock etc. is really needed? I'd especially welcome some experience from people having switched to one of the products.

Using extract&override seems to solve all problems when handling old legacy code, the refactoring seems dead simple, and the possibility for introducing bugs seems very minor. Is the benefit writing less test code? More beautifull classes with less virtual protected stuff? Right now, I don't 'get it', although I understand it's very helpfull to first test private methods in isolation, as public methods may be too large under the hood in such old legacy codebases.

If you don't know what extract&override is: see here.

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

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

发布评论

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

评论(1

天生の放荡 2024-12-07 05:42:42

框架之间存在许多差异,这些差异不考虑框架所基于的技术。

例如:

  • API - 每个框架都有不同的符号和默认值(例如
    严格的默认值与宽松的默认值)
  • 支持 - 专有框架通常提供许可证支持
  • 价格 - 这不是使用问题,而是需要预算

Extract&Override 的主要优点是,如果您的代码是这样,它需要一些重构工作被忽略了,它提供了一个很好的机会来检查它并重构它以获得更好的代码,而不仅仅是为了可测试性。

使用隔离框架的主要优点是您不需要更改测试中的代码(如果它是一个大型代码库,则可能需要很长时间才能重构它以实现可测试性)。此外,隔离框架不会强迫您进行特定的设计,如果遗留代码与其现有设计更好地匹配,这可能会很有帮助。在遗留代码中有用的另一个功能是交换在测试中的代码中创建的实例,通常重构实例需要更多的努力,这可以节省。最后一件事是伪造第 3 方代码 - 使用隔离框架,您可以隔离不属于您的代码,而无需使用包装类。

免责声明 - 我在 Typemock 工作

There are many differences between the frameworks which do not regard the technology on which the frameworks built on.

For example:

  • API - every framework has different notations and defaults (e.g.
    strict defaults vs. relaxed defaults)
  • Support - the propriety frameworks usually offer support with the licenses
  • Price - this is not a matter of usage but requires budget

The main advantage of Extract&Override is that it requires some refactoring, if the code you're working on is neglected, it's gives a good chance to go over it and refactor it toward better code and not just for testability.

The main advantage of using an Isolation framework is that you do not need to change the code under test (if it's a large codebase it could take long time just to refactor it for testability). In addition, the Isolation frameworks do not force you into specific design, this could be helpful if the legacy code matches better its existing design. Another feature which is useful in legacy code is swapping instances created in the code under test, usually refactoring instantiations takes more effort and this can be saved. Last thing is faking 3rd party code - using isolation frameworks you can isolate code which is not yours without using wrapper classes.

Disclaimer - I work at Typemock

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