如何使用遗留代码实现测试驱动开发?

发布于 2024-09-08 19:48:06 字数 190 浏览 4 评论 0原文

现状是:数百万行代码,一百多个开发人员,缺陷频发。我们希望避免重复缺陷,并且希望改进代码设计(谁不想呢?)。

测试驱动开发(首先是单元测试,然后是代码)听起来很理想:为每个功能编写一个测试用例。

但是,写了这么多代码,如何实现TDD呢?从哪里开始 - 从低级功能开始?

或者我们开始 TDD 太晚了?

The situation: millions of lines of code, more than one hundred developers and frequent defects. We want to avoid repeating defects and we want to improve code design (who doesn't?).

Test Driven Development (first unit test, then code) sounds ideal: write a test case for each function.

But, with so much code written, how can TDD be implemented? Where do you start - with low level functions?

Or are we too late to start TDD?

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

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

发布评论

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

评论(2

无妨# 2024-09-15 19:48:06

有效使用旧代码开始。

如果您从遗留代码开始,这并不是真正的 TDD - 但您的所有编码都可以是 TDD。当你解决一个新问题时,为其编写一个测试。如果不能,因为遗留类太难测试,那么开始为它们编写测试,切掉一些位,并用测试覆盖这些位。

重构唾手可得的成果

为了避免重复缺陷:给定一个示例缺陷,编写一个测试来演示它。这可能是一个相对广泛的测试,只是模拟用户活动;还没有单元测试。确保测试失败。做你的研究;找出测试失败的原因。现在 - 这很重要 - 在修复错误之前,编写一个单元测试来演示该错误。修复错误,现在您已经有了两个测试,至少其中一个测试速度很快,可以保护您免受回归影响。

Start with Working Effectively with Legacy Code.

It's not really TDD if you're starting with legacy code - but all your coding can be TDD. As you tackle a new problem, write a test for it. If you can't, because the legacy classes are too difficult to test, then start writing tests for them, slicing off bits, and covering the bits with tests.

Refactor the Low-Hanging Fruit.

To avoid repeating defects: given an example defect, write a test that demonstrates it. It could be a relatively broad test that just simulates user activity; not yet a unit test. Make sure the test fails. Do your research; figure out why the test is failing. Now - this is important - before fixing the bug, write a unit test that demonstrates the bug. Fix the bug, and now you've got two tests, at least one of them fast, that protect you from regressions.

向地狱狂奔 2024-09-15 19:48:06

既然 Carl 推荐了一本书,我会推荐另一本书:Roy Osherove 的单元测试的艺术有一整章关于“使用遗留代码”。我还没读过这一章,但前 5 章非常棒,我很期待。

Since Carl suggested one book I'll suggest another: Roy Osherove's Art of Unit Testing has a whole chapter on "Working with legacy code". I haven't read that chapter yet, but the first 5 chapters are excellent, and I'm looking forward to it.

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