简体   繁体   English

棕色域ASP.NET应用程序的有用/现实代码覆盖目标

[英]Useful/Realistic code coverage goals for a brownfield ASP.NET application

Let me qualify this question. 让我限定这个问题。 I'm working on a "classic" ASP.NET application (Web Forms) that doesn't use Model-View-Presenter and was not written using TDD. 我正在开发一个“经典”的ASP.NET应用程序(Web窗体),它不使用Model-View-Presenter而且不是使用TDD编写的。 It's also using an antiquated data access strategy (hand written DAO layer that invokes stored procs to populate and persist objects) that is unlikely to be upgraded to an ORM despite my strong desire to do so. 它还使用过时的数据访问策略(手写DAO层调用存储过程来填充和持久化对象),尽管我强烈希望这样做,但它不太可能升级到ORM。

Since I took over development of the application, most new features have been implemented using TDD. 自从我接手开发应用程序以来,大多数新功能都是使用TDD实现的。 That still leaves the old code base, DAL layer and entire UI as untested. 这仍旧旧代码库,DAL层和整个UI未经测试。 Before I figure out how far away the application is from that mystical 70% code coverage goal, I'd like to get clarity around what kind of code is typically included when determining code coverage. 在我弄清楚应用程序距离神秘的70%代码覆盖率目标有多远之前,我想清楚一下在确定代码覆盖率时通常包含哪种代码。

Business logic code is clearly included, but how about WebForms code? 明确包含业务逻辑代码,但WebForms代码如何? Additionally, how about data access code? 另外,数据访问代码怎么样? As mentioned above, our data access layer uses stored procedures to populate object graphs and persist them back to a DB. 如上所述,我们的数据访问层使用存储过程来填充对象图并将它们保存回DB。 Is object persistence and re-hydration something that should be included? 物体持久性和再水化是否应包括在内?

I apologize if this question is too open ended, I just feel a little overwhelmed and confused about how to get this brownfield application in better shape. 我很抱歉,如果这个问题太开放,我只是觉得有点不知所措,并且对如何使这个棕色地块应用程序更好地形成一些困惑。

Thanks! 谢谢!

Don't set targets for code coverage or any other code metrics. 不要为代码覆盖率或任何其他代码度量标准设置目标 It usually turns out that hard targets do more damage than good . 通常证明, 硬目标造成的伤害大于好处

If you give other developers hard code metric targets they will just game the targets if they don't understand the underlying reasons for the target. 如果您为其他开发人员提供硬代码指标目标,那么如果他们不了解目标的潜在原因,他们就会对目标进行游戏

As a parallel example, you will not believe how many "Keep FxCopy happy" code comments I've seen in my career. 作为一个平行的例子,你不会相信我在职业生涯中看过多少“Keep FxCopy happy”代码评论。

If you set a hard target for test coverage, lazy developers may skip writing null checks etc. because it decreases their coverage if they don't write the corresponding tests. 如果为测试覆盖设置硬目标,懒惰的开发人员可能会跳过编写空检查等,因为如果他们不编写相应的测试,它会减少覆盖范围。 The end result is poorer code quality. 最终结果是代码质量较差。

Conversely, developers who understand the benefits of TDD don't need the target because they'll do the right thing regardless. 相反,了解TDD优势的开发人员不需要目标,因为无论如何他们都会做正确的事情。

That doesn't mean that the code coverage metric is irrelevant. 这并不意味着代码覆盖率指标无关紧要。 It is very relevant, but instead of setting a hard target, I think you should have a rule that says that it must never decrease . 这是非常相关的,但我认为你应该有一个规则, 它必须永远不会减少 ,而不是设定一个硬目标。

So measure it regularly and make sure it's only going up. 所以定期测量它并确保它只是上升。 That doesn't preclude you from having your own personal goal, but don't set a hard target. 这并不妨碍你有自己的个人目标,但不要设定一个硬目标。

Code coverage doesn't have a high correlation to application stability. 代码覆盖率与应用程序稳定性没有高度相关性。 Rate of influx of bug reports (and the bug's severity) does have a high correlation to application stability. 错误报告的流入率(以及错误的严重性)确实与应用程序稳定性具有高度相关性。

When I talk about code coverage, I include absolutely everything and just assume 100% is unrealistic. 当我谈论代码覆盖时,我绝对包含所有内容,并假设100%是不现实的。 There are probably many different opinions on the subject, so it seems pretty subjective to me. 关于这个问题可能有很多不同的意见,所以这对我来说似乎很主观。

If I was in your position, I would be more worried about getting manual and automated regression test bases built before worrying about code coverage. 如果我在你的位置,我会更担心在担心代码覆盖之前建立手动和自动化回归测试库。

In brownfield projects, it may not be worth the effort to to reach any absolute threshold, neither for source code metrics, nor for test coverage. 在棕地项目中,无论是源代码指标还是测试覆盖率,达到任何绝对阈值都可能是不值得的。 In code quality, the most suggested approach is known as the boy scout rule , ie leave the code (the camp) a little cleaner than you found it. 在代码质量方面,最常建议的方法被称为童子军规则 ,即让代码(营地)比您发现的更清洁。 This way, the code will gradually improve. 这样,代码就会逐渐改进。

In testing, the situation is similar. 在测试中,情况类似。 Core functionality has been used productively for a long time, so there is a low probability for bugs in this area. 核心功能已经有效地使用了很长时间,因此该区域中的错误概率很低。 Research has shown that recent changes have five times the bug probability compared to the system as a whole. 研究表明,与整个系统相比,最近的变化的错误概率是其五倍。 Therefore I suggest starting with recently changed code . 因此我建议从最近更改的代码开始

You can use information from the source code management system to identify changes, or eg compare the most recently built binaries with the last production release. 您可以使用源代码管理系统中的信息来识别更改,或者将最近构建的二进制文件与上一个生产版本进行比较。 In order to further narrow down the scope, you may ignore changes that solely originate from refactoring. 为了进一步缩小范围,您可以忽略仅源自重构的更改。 You can then focus your tests on these areas. 然后,您可以将测试重点放在这些区域上。 An example for this approach is sketched in this blog post . 此博客文章中概述了此方法的示例。 Since the idea is agnostic of the actual test methodology, it can even combine results from unit tests and manual regression tests. 由于该想法与实际测试方法无关,因此它甚至可以结合单元测试和手动回归测试的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM