Coverage测试代码覆盖(Python)

Coverage 测试代码执行率
1、安装coverage 命令行:pip install coverage
2、将测试代码放到一个盘符下,如:D:\\test
测试代码:
Calc.py
# coding=utf-8
class Calc(object):

    def add(self, x, y, *d):
         # 加法计算
        result = x + y
        for i in d:
            result += i
        return result

    def sub(self, x, y, *d):
        # 减法计算
        result = x - y
        for i in d:
            result -= i
        return result

    @classmethod
    def mul(cls, x, y, *d):
        # 乘法计算
        result = x * y
        for i in d:
            result *= i
        return result

    @staticmethod
    def div(x, y, *d):
        # 除法计算
        if y != 0:
            result = x / y
        else:
            return -1
        for i in d:
            if i != 0:
                result /= i
            else:
                return -1
        return result

if __name__=="__main__":
    c=Calc()
    print c.add(1,2,3,4)
    #print c.add(1,2,[3,4],5,a=3)
    print c.sub(1,2,3,4)
    print c.mul(2,3,4)
    print c.div(10,5,1)
    print c.div(1,0,0)
    print c.div(1,1,0)
    print Calc.mul(1,2,3,4)
    print Calc.div(100,10,5,1)

3、命令行进入代码所在位置执行coverage run Calc.py 结果会显示出执行的结果
4、执行 coverage report -m 结果显示执行报告
5、执行 coverage html 会在本地生成htmlcov文件,里面的Clac_py.html打开后会显示代码覆盖统计

 

转载于:https://www.cnblogs.com/test-chen/p/10310180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值