单元测试前端逻辑

发布于 2024-07-30 18:15:04 字数 1542 浏览 6 评论 0原文

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

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

发布评论

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

评论(4

神妖 2024-08-06 18:15:04

单元测试前端逻辑非常困难,因为有很多部分组成,例如前端的服务器端代码更改和客户端更改都会影响应用程序的显示方式。

在 Google 测试博客上,他们讨论了测试 MVP 所有部分的价值以及通过删除昂贵部分来测试 AJAX 的价值 此处。 Misko Hevery 此处谈论了不同的测试,我觉得前端测试属于他的大测试类别,因此总是有误报/误报的机会,但需要对它们进行排序,因为它们仍然提供很多价值

前端测试非常有价值,因为它们检查用户功能是否没有下降离开。 这就是为什么像 SeleniumWatir 非常受欢迎。


Unit testing front end logic is extremely hard since there are many parts that make it up like server side code changes to the front end and client side changes can all affect how the application appears.

On the Google testing blog they talked about the value of testing all the parts of a MVP and the value of testing AJAX by stubbing the expensive parts here. Misko Hevery talks about the different tests here and I feel that the Front End tests fall into his large test category so always have the chance of false negatives/positives but they need to be sorted because they still offer a lot of value

Front end tests are extremely valuable as they check the user functionality hasn't dropped off. This is why tools like Selenium and Watir are so popular.


木有鱼丸 2024-08-06 18:15:04

修复破损的窗户。
这是理论的链接 http://en.wikipedia.org/wiki/Fishing_Broken_Windows

防止代码质量不佳的一个成功策略是始终保持代码库干净并处于良好状态。 这意味着清理所有错误代码。 这可能会很痛苦,而且很耗时,但无论如何你都需要这样做。

当我们删除了项目中的所有编译警告后,我们发现我们的代码质量得到了提高。 人们开始关心他们签入的内容,因为我们发出了一个信号,即签入错误代码是不行的(即使是警告也是不行的),并且没有人想成为第一个打破窗口的人。

Fix the broken window.
here is a link to the theory http://en.wikipedia.org/wiki/Fixing_Broken_Windows

A successful strategy for preventing poor code quality is to always keep your code base clean and in prestige condition. This means clean up all the bad code. It might be painful, and time consuming, but you will need to do it anyway.

After we removed all the compile warnings on our project, we found that our code quality has been improving. People start to care what they check in, since we send out a signal that it is not ok to checkin bad code(even warning is not ok), and no one wants to be the first one to break the window.

倾城月光淡如水﹏ 2024-08-06 18:15:04

测试 UI 时的另一个陷阱是,最终很容易编写测试框架而不是应用程序的测试。 或者反过来说:编写实际测试 UI 而不是 UI 框架的测试可能具有挑战性。

例如,一些团队最终编写了大量无用的测试,例如:

Given a button, when the button is clicked, the event handler should fire.

Another pitfall when testing UI is that it is especially easy to end up writing tests that test the framework and not your application. Or said in reverse: it can be challenging to write test that actually test your UI and not the UI framework.

For instance, some teams end up writing lots of useless tests like:

Given a button, when the button is clicked, the event handler should fire.
〆凄凉。 2024-08-06 18:15:04

对我来说,问题是在维护测试的成本和发现否则无法发现的错误的可能性之间进行权衡。 想必您仍然会对实际 UI 进行一些视觉测试(否则您会很好地测试美观性)。

例如,您能否只关注 Presenter 的某些方面? 例如,一个不平凡的格式化程序,用许多有趣的值来练习它。

我怀疑说服的一种方法是慢慢进行。 首先尝试寻找省力的测试。

The question for me would be the trade-off between the costs of maintaining the tests and the probability of catching bugs that would not be caught otherwise. Presumably you will still have some visual testing of the actual UI (you'll do really well to test the aesthetics otherwise).

Can you for example focus only on some aspects of the Presenter. For example a non-trivial formatter, exercise that with lots of interesting values.

I suspect that one way to persuade is to proceed slowly. Try to find low-effort tests initially.

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