介紹
coverage 在單元測(cè)試中可以顯示覆蓋率,顯示更清晰的數(shù)據(jù)
- 支持python 2.6-python3.X
- pip install coverage安裝
測(cè)試代碼
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測(cè)試
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()
- 命名運(yùn)行
coverage run test_mymath.py
coverage report -m
- 結(jié)果

Paste_Image.png
更多高級(jí)用法:http://www.blog.pythonlibrary.org/2016/07/20/an-intro-to-coverage-py/