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打开后会显示代码覆盖统计