本文介绍了可以选择在现有系统中添加单元测试或集成测试,哪个更好,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在咨询现有系统,我怀疑正确的下一步是添加单元测试,因为正在发生的异常类型(空指针,空列表,无效的返回数据)。但是,在应用程序中进行个人投资的员工坚持进行集成测试,即使报告的问题与特定用例失败无关。在这种情况下,最好是从单元测试还是集成测试开始?

I'm currently consulting on an existing system, and I suspect the right next step is to add unit tests because of the types of exceptions that are occurring (null pointers, null lists, invalid return data). However, an employee who has a "personal investment" in the application insists on integration tests, even though the problems being reported are not related to specific use cases failing. In this case is it better to start with unit or integration tests?

推荐答案

通常情况下,改造未经测试的代码库非常困难有单元测试。将会有高度的耦合,并且运行单元测试将比您获得的回报更大的时间下降。我建议如下:

Typically, it is very difficult to retrofit an untested codebase to have unit tests. There will be a high degree of coupling and getting unit tests to run will be a bigger time sink than the returns you'll get. I recommend the following:


  • 至少获取一份,由Michael Feathers与团队成员一起完成。它处理这个问题。

  • 对所有编写的新代码实施严格的单元测试(最好是TDD)策略。这将确保新代码不会成为遗留代码,并且获得要测试的新代码将推动旧代码的重构以实现可测试性。

  • 如果你有时间(你可能赢了) t),在系统的关键路径上编写一些关键的集中测试。这是一个很好的健全性检查,你在步骤#2中进行的重构不会破坏核心功能。

  • Get at least one copy of Working Effectively With Legacy Code by Michael Feathers and go through it together with people on the team. It deals with this exact issue.
  • Enforce a rigorous unit testing (preferably TDD) policy on all new code that gets written. This will ensure new code doesn't become legacy code and getting new code to be tested will drive refactoring of the old code for testability.
  • If you have the time (which you probably won't), write a few key focused integration tests over critical paths of your system. This is a good sanity check that the refactoring you're doing in step #2 isn't breaking core functionality.

这篇关于可以选择在现有系统中添加单元测试或集成测试,哪个更好,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:56