Python coverage 将顶级包名变成句点

Python Coverage turns top-level package name into period

我有这样一个项目:

proj/ex_secure/__init__.py
proj/ex_secure/base.py
proj/ex_secure/metrics.py
proj/ex_secure/keys.py
proj/tests/test_base.py
proj/tests/test_metrics.py
proj/tests/test_keys.py
proj/.gitignore
proj/.pep8
proj/README.rst
proj/setup.cfg
proj/setup.py

如果我 运行 pytest 这样:

pytest -s --junitxml=pytests.xml --cov-report xml --cov-report term-missing --cov-branch --cov=ex_secure

那么得到的coverage.xml中的顶层包名为.:

<package branch-rate="0.4722" complexity="0" line-rate="0.6801" name=".">

但是如果我这样调用 pytest

pytest -s --junitxml=pytests.xml --cov-report xml --cov-report term-missing --cov-branch --cov=ex_secure.base --cov=ex_secure.metrics --cov=ex_secure.keys

那么顶层包就正确命名了ex_secure:

<package branch-rate="0.4722" complexity="0" line-rate="0.6801" name="ex_secure">

目前这是一个不错的解决方法,但并不理想。如果我添加更多包,我必须继续枚举它们(否则它们将在覆盖率报告中丢失)。此外,__init__.py 不包括使用此机制。

我做错了什么?

更新 1:

如果我 运行 Python 直接覆盖而不是使用 pytest-cov,它会按预期工作:

coverage run --branch --source=ex_secure -m pytest -s --junitxml=pytests.xml
coverage xml

然后:

<package branch-rate="0.4722" complexity="0" line-rate="0.6771" name="ex_secure">

更新 2:

如果我像最初那样 运行 PyTest,然后直接重新生成具有 Python 覆盖率的 XML 报告,重新生成的报告得到纠正,但略有不同的数字:

pytest -s --junitxml=pytests.xml --cov-report xml --cov-report term-missing --cov-branch --cov=ex_secure
coverage xml

然后:

<package branch-rate="0.3058" complexity="0" line-rate="0.4769" name="ex_secure">

不幸的是,这是一个相当讨厌的两部分错误,它跨越 Py-Coverage 和 PyTest-Cov。在 PyTest-Cov GitHub and the Py-Coverage BitBucket.

上有详细描述

总之命令行调用coverage xml是不允许你传入sources的,所以一般人看不出这个问题,但是你可以传入[=11] =] 将 Python API 用于 Py-Coverage 时,并且 API 无法正确处理 sources 属性。同时,PyTest-Cov 使用 Python API 作为 Py-Coverage,因此当您使用 --cov=xxxx--cov-report xml 调用 PyTest-Cov 时,您会遇到这个问题。