python 代码覆盖率 coverage用法

时间:2021-11-20 22:00:46

先装coverage:

D:\test_python\e8_test>pip install coverage
Collecting coverage
  Downloading https://files.pythonhosted.org/packages/42/60/e6958685ab99e7b54dc5f2dddf85f648a4577bc1ca85b8e80ce8474f6e23/coverage-4.5.1-cp27-cp27m-win32.whl (178kB)
    100% |################################| 184kB 233kB/s
Installing collected packages: coverage
Successfully installed coverage-4.5.1

代码:

#coding=utf-8
import unittest

if __name__ == '__main__' :
  
    testSuite = unittest.TestLoader().discover('.')
    unittest.TextTestRunner(verbosity = 2).run(testSuite)

运行coverage run task_test.py

D:\test_python\e8_test>coverage run task_test.py
10
-8
24
2
-1
-1
24
2

输出报告:coverage report -m

D:\test_python\e8_test>coverage report -m
Name           Stmts   Miss  Cover   Missing
--------------------------------------------
task_test.py      35      0   100%

输出html报告coverage html

D:\test_python\e8_test>coverage html

文件可以在当前目录下的htmlcov下找到

python 代码覆盖率 coverage用法