当项目编码标准与单元测试代码覆盖率冲突时该怎么办? [英] What to do when project coding standards conflicts with unit test code coverage?

查看:159
本文介绍了当项目编码标准与单元测试代码覆盖率冲突时该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于学习体验的个人项目,同时还要实现一个体面的代码体。这种教育的一部分,并使它成为一个体面的代码,是单元测试。我最近陷入了 PHPUnit 及其代码覆盖工具。



我遇到了一个特定实现的情况,其中使用的编码标准导致代码覆盖丢失。在这个特定的实例中,破坏所使用的编码标准会导致代码覆盖率从88%跳转到94%。



在一个方法中,我有两行,

  // ..一些数据验证东西
trigger_error('Error validating the stuff',E_USER_WARNING);
}

数据验证和东西在这里不重要,} 是。现在当单元测试通过这行代码时, PHPUnit_Framework_Error 被放在} 之前的行



如果我这样做

  // ..一些数据验证东西
trigger_error('Error validating the stuff',E_USER_WARNING);}
/ pre>

我的代码覆盖率提高了6%。我尝试将 PHPUnit_Framework_Error_Warning :: $ enabled 设置为 false 但是我得到一个丑陋的预期的错误消息我的终端,因为我想要这个项目最终被人以外的人使用单元测试的错误消息是不能接受的。此外,我真的希望我的编码风格得以贯彻始终。代码风格违反可能会跳出代码的进一步的习惯,意味着我还必须添加一个可怕的评论解释为什么大括号被移动...可能在多个地方。



我想我的问题是:


  1. PHPUnit有一个设置,允许1TBS在} ?之前直接使用并仍然被测试抛出异常或触发错误覆盖?

  2. 遵循编码标准或提高代码覆盖率? (虽然boost只是解释器超过额外的}


解决方案

不要痴迷数字。你知道,报告的数字是假的,你有更多的报道比报告,所以为什么担心吗?更重要的是,您的测试覆盖所有有意义的代码,而不是实现100%的代码覆盖率。



如果你觉得编码标准很重要,而且这个看起来是,那么不要牺牲数字的可读性。


I'm working on a personal project for the learning experience, and also at the same time to implement a decent body of code. Part of this education, and making it a decent body of code, is unit testing. I have recently dived into PHPUnit and its code-coverage tools.

I have encountered a situation with a particular implementation where the coding standard used causes code-coverage to be lost. In this particular instance breaking the coding standard used causes a jump from 88% to 94% in code-coverage.

In a method I have 2 lines that look like the following

    // .. some data validation stuff
    trigger_error('Error validating the stuff', E_USER_WARNING);
}

The data validation and the stuff isn't important here, the } is. Right now when the unit test goes over this line of code a PHPUnit_Framework_Error is thrown on the line before the }, since the code never actually continues on to the end of the brace this line is never captured by code coverage.

If I do

    // .. some data validation stuff
    trigger_error('Error validating the stuff', E_USER_WARNING);}

I get a 6% jump in code coverage. I've tried setting PHPUnit_Framework_Error_Warning::$enabled to false but then I get an ugly, expected, error message in my terminal, since I want this project to eventually be used by people other than myself error messages on unit tests are unacceptable. In addition, I really would like for my coding styles to be implemented consistently. The code-style violation would likely jump out upon further perusals of the code, meaning I'd also have to add a dreaded comment explaining why the braces were moved...likely in multiple places.

I guess my question(s) are:

  1. Is there a setting for PHPUnit that would allow the 1TBS to be used and still get covered by a test throwing an exception, or triggering an error, directly before a }?
  2. Is it more important to follow the coding standard or get a boost in code-coverage? (Although the boost is really just the interpreter going over an extra })

解决方案

Don't be obsessed with a number. You know that the numbers being reported are false and that you have more coverage than is being reported so why worry about it? It is more important that your tests cover all meaningful code than you achieve 100% code coverage.

If you feel the coding standard is important, and this one looks to be, then don't sacrifice readability for a number.

这篇关于当项目编码标准与单元测试代码覆盖率冲突时该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆