python coverage--单元测试覆盖图

介绍

coverage 在单元测试中可以显示覆盖率,显示更清晰的数据

  • 支持python 2.6-python3.X
  • pip install coverage安装

测试代码

def add(a, b):
    return a + b
 
 
def subtract(a, b):
    return a - b
 
 
def multiply(a, b):
    return a * b
 
 
def divide(numerator, denominator):
    return float(numerator) / denominator
  • unittest测试
import mymath
import unittest
 
class TestAdd(unittest.TestCase):
    """
    Test the add function from the mymath library
    """
 
    def test_add_integers(self):
        """
        Test that the addition of two integers returns the correct total
        """
        result = mymath.add(1, 2)
        self.assertEqual(result, 3)
 
    def test_add_floats(self):
        """
        Test that the addition of two floats returns the correct result
        """
        result = mymath.add(10.5, 2)
        self.assertEqual(result, 12.5)
 
    def test_add_strings(self):
        """
        Test the addition of two strings returns the two string as one
        concatenated string
        """
        result = mymath.add('abc', 'def')
        self.assertEqual(result, 'abcdef')
 
 
if __name__ == '__main__':
    unittest.main()
  • 命名运行
coverage run test_mymath.py
coverage report -m
  • 结果
Paste_Image.png

更多高级用法:http://www.blog.pythonlibrary.org/2016/07/20/an-intro-to-coverage-py/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python 四五事 介绍 Python 相关工具,工作流程和测试框架。 发布于 2014.1.19最后更新 20...
    hzyido阅读 65,080评论 0 4
  • Python的开发跟其他的一些语言是有很大不同的. 她和Ruby, Perl一样都是解释型语言,所以开发者能够交互...
    周筱鲁阅读 64,935评论 0 10
  • 作者:廖飞全文约 7778 字,读完可能需要 15 分钟。 原文链接:http://www.cnblogs.com...
    罗义的夏天阅读 10,037评论 1 2
  • 因为unittest支持的html报告在作为邮件附加时耗时较长,故将报告扩展支持为unishark框架。 基于un...
    五娃儿阅读 3,567评论 0 0
  • Startup 单元测试的核心价值在于两点: 更加精确地定义某段代码的作用,从而使代码的耦合性更低 避免程序员写出...
    wuwenxiang阅读 13,411评论 1 27