我有以下目录结构:
./
src/
tests/
unit/
integration/我想使用pytest来运行unit/和integration/中的所有测试,但我只希望coverage.py在运行unit/测试时(而不是在运行integration/测试时)计算src/目录的覆盖率。
我现在使用的命令(计算tests/下所有测试的覆盖率):
pytest --cov-config=setup.cfg --cov=src使用setup.cfg文件:
[tool:pytest]
testpaths = tests
[coverage:run]
branch = True我知道我可以在集成测试中将@pytest.mark.no_cover装饰器添加到每个测试函数中,但是我更愿意标记整个目录,而不是装饰大量的函数。
发布于 2020-03-09 22:53:49
您可以动态地附加标记。下面的示例在pytest_collection_modifyitems钩子的自定义驱动中这样做。将代码放入项目根dir中的conftest.py中:
from pathlib import Path
import pytest
def pytest_collection_modifyitems(items):
no_cov = pytest.mark.no_cover
for item in items:
if "integration" in Path(item.fspath).parts:
item.add_marker(no_cov)https://stackoverflow.com/questions/60608511
复制相似问题