端到端测试太多?

发布于 2025-01-03 13:24:25 字数 1459 浏览 2 评论 0原文

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

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

发布评论

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

评论(1

风苍溪 2025-01-10 13:24:25

您可以创建数据库代理类,确保它们是唯一与真实数据库通信的类。使用依赖注入对所有逻辑代码进行单元测试,而无需与实际数据库交互。创建尽可能少的端到端测试,以确保代理可以正确读取/写入数据库。

端到端测试本质上通常很脆弱。因此,尽可能少地创建它们,如果需要创建很多,则可以创建一个抽象层来设置装置和断言。 测试用例中的重复与代码中的重复一样难以维护。

有效地使用遗留代码是一个好的开始,我推荐xUnit测试模式,它基本上是单元测试的圣经,提供了很多好的建议,包括关于使用数据库。

编辑: TDD 就是隔离逻辑。我的意思是控制流语句,正则表达式,算法,数学,应该在你的代理之外,你可以轻松地对它们进行单元测试。你有 113 个测试,这让我怀疑有可以提取和单元测试的逻辑。

如果您正在创建 SQL 命令,则可以使用 Builder 模式来验证命令是否正确创建,如果您需要更改 SQL 方言,则只有一处可以进行更改。

让您的代码可测试可能意味着您需要积极地进行重构。困难的部分是根据项目的重要性和寿命来确定值得多少钱。

You can create database proxy classes, making sure they are the only classes that talk to the real database. Use dependency-injection to unit test all your logic code without talking to the actual database. Create as few end-to-end tests as possible to make sure the proxies can read/write to the database correctly.

End-to-end tests are usually fragile by nature. So create as few of them as possible, and if you need to create a lot you can create an abstraction layer for setting up fixtures and assertions. Duplication in test cases is just as difficult to maintain as duplication in code.

Working Effectively with Legacy Code is a good start, and I recommend xUnit Test Patterns, which is basically the bible of unit testing with lots of good advice including a section on testing with databases.

Edit: TDD is all about isolating logic. What I mean is control flow statements, regular expressions, algorithms, math, should be outside your proxies where you can unit test them easily. You had 113 tests which makes me suspect there is logic that can be extracted and unit tested.

If you are creating SQL commands, you can use the Builder pattern to verify the commands are created correctly, and if you need to change the SQL dialect, there is only one place to make the changes.

Making your code testable may mean you need to refactor somewhat aggressively. The hard part will be determining how much is worth it, based on the importance and longevity of the project.

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