默认情况下,pytest-cov 将报告所有库的覆盖率,包括外部库。
如果您pytest --cov
针对您的代码运行,它将产生许多行覆盖,包括py
, pytest
,importlib
等。
要限制覆盖范围,即您只想随机检查覆盖范围,只需将模块名称传递给 cov 选项,例如pytest --cov=random
. 覆盖率报告然后只考虑命名的模块。您还可以通过指定多个cov
值来传递多个模块,例如pytest --cov=random --cov=pytest
这是一个运行测试以仅针对random
$ pytest --cov=random
====== test session starts ======
platform linux -- Python 3.6.12, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
plugins: cov-2.12.1
collected 1 item
test_something.py F
[100%]
=========== FAILURES ============
___________ test_rand ___________
def test_rand():
import random
> assert random.randint(0,10) == 5
E AssertionError: assert 0 == 5
E + where 0 = <bound method Random.randint of <random.Random object at ...>>(0, 10)
test_something.py:6: AssertionError
---------- coverage: platform linux, python 3.6.12-final-0 -----------
Name Stmts Miss Cover
/.../random.py 350 334 --
TOTAL 350 334 5%