测试驱动开发最能有效地捕获哪些类型的错误?

发布于 2024-09-07 04:26:28 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(3

野却迷人 2024-09-14 04:26:28

TDD 迫使您从“消费”您将要编写的代码的角度进行思考。这种观点有助于将您(开发人员)置于需要考虑如何构建 API 以及如何验证实现要求的位置。

TDD 有助于识别以下领域的缺陷:

  • 需求。代码需要做什么是否清楚。是否可以验证代码的不变量或最终效果。成功标准是否在需求中定义,或者是否含糊或缺失。
  • 易于使用。您能否有效地使用您计划编写的代码来实现最终用户或未来将与其集成的其他代码所需的各种功能。
  • 可测试性。代码是否可基于设计中可访问的数据或对象进行验证。是否有可能确认事情会按其应有的方式运作?
  • 边缘情况。通过预先确定边缘情况的存在,通常可以更轻松地识别和响应边缘情况。当游戏后期出现边缘情况时,人们倾向于尝试“强制”现有设计来适应它们,而不是重新考虑设计。
  • 异常处理。当您开始编写测试用例时,您开始意识到您可能希望能够响应错误或异常情况的领域。这可以帮助规划您的异常处理策略、要抛出的异常类型、要包含哪些信息等。TDD

还有助于提高测试的覆盖水平,因为它将测试带到前台,而不是使其成为“之后的测试”。事实”活动。当测试最后进行时,由于时间和预算的限制,或者由于开发人员的热情和动力自然下降,它最容易被省略或被忽视。

TDD forces you to think from the perspective of "consuming" the code you are going to write. This point of view helps to place you (the developer) into a position where you need to think about how your API will be structured as well as how you would verify the requirements of the implementation.

TDD helps identify defects in areas like:

  • Requirements. Is it clear what the code will need to do. Is it possible to verify the invariants or end-effects of the code. Are the success criteria defined in the requirements or are they vague or absent.
  • Ease of Use. Can you effectively use the code you plan on writing to achieve the kinds of things that are needed by end users, or by other code that it will integrate with in the future.
  • Testability. Is the code verifiable based on the accessible data or objects in the design. Is it possible to confirm that things will function as they should.
  • Edge Cases. It's often easier to identify and respond to edge cases by identifying their existance up-front. WHen edge cases crop up late in the game, there's an inclination to try to "force" the existing design to accomodate them, rather than rethinking the design.
  • Exception Handling. When you start writing tests cases, you begin to realize areas where you may want to be able respond to errors or exceptional conditions. This can help plan your exception handling strategy, the kinds of exceptions to throw, what information to include, etc.

TDD also help to improve the level of coverage in tests because it brings testing to the foreground, rather than making it an "after the fact" activity. When testing happens last, it is most prone to be omitted or short-shrifted due to time and budget constraints, or due to the natural drop in enthusiasm and motivation on the part of the developer.

如何视而不见 2024-09-14 04:26:28

设计“bug”:如果您通常进行 TDD,那么您自然会得到可测试的设计。反过来,这往往会减少耦合等 - 导致代码库更容易使用。

另外,我发现 TDD 可以更轻松地考虑某些情况下的极端情况 - 但在我看来,设计优势更重要。

Design "bugs": if you're generally doing TDD, you naturally end up with a testable design. In turn, that tends to reduce coupling etc - leading to a code base which is simply easier to work with.

Also, I've found TDD can make it easier to think about corner cases in certain situations - but the design benefit is more important, IMO.

躲猫猫 2024-09-14 04:26:28

对我来说,空值或零值参数情况是 TDD 捕获的最有差别的错误。我倾向于先用这种情况编写测试,就像刷新 API 的一种方式,而不考虑值:“哦,只需在其中扔一个 null 即可;我们将在下一个测试中放置一个真实值。”因此,我的方法最初是为了处理特定的边缘情况而编写的,并且在整个红绿重构过程中重复运行该测试(以及所有其他测试)可以使该边缘情况正常工作。在使用 TDD 之前,我经常会忘记 null 或零参数;现在,无需真正尝试,它们就被视为我应用 TDD 方式的自然结果。

The null- or zero-valued parameter case is for me the bug most differentially caught by TDD. I tend to write my tests first with this case, just as a way of flushing out the API, without regard for the value: "Oh, just toss a null in there; we'll put a real value in the next test." So my method is initially written to handle that particular edge case, and repeatedly running that test (along with all the others) throughout the red-green-refactor process keeps that edge case working right. Before using TDD, I would forget about null or zero parameters fairly frequently; now, without really trying, they're handled as a natural consequence of the way I apply TDD.

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