51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 10726|回复: 0
打印 上一主题 下一主题

【推荐原文】100 Percent Unit Test Coverage Is Not Enough(已认领)

[复制链接]
  • TA的每日心情
    慵懒
    2019-2-2 13:49
  • 签到天数: 242 天

    连续签到: 1 天

    [LV.8]测试军长

    跳转到指定楼层
    1#
    发表于 2017-8-14 10:20:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    原文作者:John Ruberto
    作者联系方式:无
    原文地址链接:https://www.stickyminds.com
    推荐理由:让我们更进一步了解单元测试覆盖率

    Summary:
    Many people equate 100 percent unit test coverage with high code quality, but that is not enough. Code coverage tools only measure whether the tests execute the code; they make no judgment on the effectiveness of the tests. Testers should review unit tests, even if they have high coverage levels, and either help improve the tests or supplement them with extra tests where necessary.
    Why do we need testers when our code is 100 percent covered by unit tests run by developers? Why should testers review unit tests when creating their tests? Because 100 percent unit test code coverage is not enough.
    Code coverage tools trace the execution of your code and provide metrics about that execution. One of the most common measures is statement coverage. Statement coverage gives a percentage of the statements executed over the total number of statements in the program. Many organizations set goals for unit test coverage, with a common target being 80 percent statement coverage.
    Developers pride themselves on getting to 100 percent unit test coverage, and people on the project teams associate this with having high-quality code. But even though the tests execute every line of code and we then call the code "fully tested," this can be misleading.
    One hundred percent unit test coverage does not mean we had good tests, or even that the tests are complete. The tests could be missing important data and only testing with data that succeeds, failing to test data that causes failures. One hundred percent unit test coverage doesn't say anything about missing code, missing error handling, or missing requirements.
    Tests also might not actually check the functionality of the code. Merely executing the code without checking its functionality still counts in the coverage metrics.
    The following examples illustrate what can go wrong with relying on unit test coverage alone. The code is Python, and tests are written with the unittest module. Hopefully these examples inspire testers to review unit tests, improve these tests, or supplement them with functional tests to fill in the gaps-even if test coverage is 100 percent.
    Missing Test Cases
    Consider the following function (only_correct_data), which takes several parameters, performs some math, and returns the result. It would only take one unit test to achieve 100 percent code coverage, because this function only has one line of code:
    def only_correct_data (a, b, c) :
        return (a / (b - c))
    def test_only_correct_data(self):
       #only tests with data that leads to correct results
        self.assertEqual( only_correct_data(1,2,3) , -1)
        self.assertEqual( only_correct_data(2,3,1) , 1)
        self.assertEqual( only_correct_data(0,2,3) , 0)
    The test coverage is 100 percent, with three test cases that cover a negative result, a positive result, and a zero result. What is missing is the test case that would create a divide by zero error. Using the call only_correct_data(2, 2, 2) would result in the divide by zero exception. Finding missing tests cases for the developer is a great way to improve the unit tests.
    Another benefit of reviewing these unit tests and making sure they are thorough is that the tester can concentrate on the overall system quality, the user experience, and acceptability to the customer. Knowing that the math is correct and verified by unit tests allows the tester to concentrate on these other factors related to quality.
    ......

    (下载附件即可查看原文)

    【申请翻译方式】
    ① 发送一段试译内容到我们的邮箱 editor@51testing.com,通过后即可翻译
    ② 加小编 QQ:1718403480,备注:翻译试译


    【翻译奖励】
    ① 翻译文章经过评审入选后,即可发布在《51测试天地》电子刊物中,并有相应稿费支付!还有机会上网站头条,得到大力宣传!
    ② 未入选文章,也可以获得博为峰网校10元代金券


    如果你想锻炼自己的翻译能力,或者想学习最新最热的技术内容,欢迎加入到我们的翻译队伍中来!!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-24 15:17 , Processed in 0.065671 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表