51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 9807|回复: 0

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

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

    连续签到: 1 天

    [LV.8]测试军长

    发表于 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
    回复

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-3-29 09:42 , Processed in 0.069099 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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