TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Epic Fail of Enforcing Unit Tests

8 点作者 patz大约 8 年前

3 条评论

rstuart4133大约 8 年前
I don&#x27;t agree with him.<p>Everyone knows when you are forced to write tests you code differently because you have to make the lines tested visible to the testing code. In legacy code without tests no one bothered to do this, which is why adding tests to legacy code is near impossible.<p>If 100% test coverage isn&#x27;t forced you end up with lines that aren&#x27;t visible to unit tests without re-factoring. If a bug is found in those lines and you want to add a test to demonstrate the bug has been fixed, you can&#x27;t just make a minimal change - you have to re-factor the bloody thing, increasing the odds you will introduce another bug.<p>The bottom line is ensuring you have 100% test coverage (or even better 100% branch coverage) brings it&#x27;s own rewards because of the discipline it enforces in code structure, and that is true even if those test lines don&#x27;t do much in the way of useful testing.<p>So for me his argument only works you have 100% coverage. Thereafter, yeah, each test has to earn it keep.
评论 #13779277 未加载
taylodl大约 8 年前
I addressed a lot of these concerns in my blog post on Maintenance Driven Development. <a href="https:&#x2F;&#x2F;taylodl.wordpress.com&#x2F;2012&#x2F;07&#x2F;21&#x2F;maintenance-driven-development&#x2F;" rel="nofollow">https:&#x2F;&#x2F;taylodl.wordpress.com&#x2F;2012&#x2F;07&#x2F;21&#x2F;maintenance-driven-...</a><p>Yes, fine-grained tests are problematic and as such one tends to desire to minimize the creation of such tests as much as possible. But when bugs are discovered then it may be necessary to create fine-grained tests in that affected area. Essentially what you&#x27;re doing is the most extensive testing in the area of your code giving you the most problems. If you notice a particular area of your code requiring a lot of extensive unit tests then perhaps it&#x27;s time to consider refactoring that code.
评论 #13761727 未加载
Ace17大约 8 年前
Each unit test not ending with an assertion is essentially a hack to locally silence your coverage tool, without actually improving the quality of your test suite.<p>I call these tests &quot;crash-tests&quot;, as a crash is nearly the only thing that they can detect. Crash-tests are indeed what you get if your devs are aiming at a high-coverage number without understanding the rationale behind code coverage.