gpt4 book ai didi

python - pytest-cov - 不要计算集成测试目录的覆盖率

转载 作者:行者123 更新时间:2023-12-05 02:56:23 25 4
gpt4 key购买 nike

我有以下目录结构:

./
src/
tests/
unit/
integration/

我想使用 pytest 在 unit/integration/ 中运行所有测试,但我只希望 coverage.py 计算覆盖率运行 unit/ 测试时的 src/ 目录(而不是运行 integration/ 测试时)。

我现在使用的命令(计算tests/下所有测试的覆盖率):

pytest --cov-config=setup.cfg --cov=src

使用 setup.cfg 文件:

[tool:pytest]
testpaths = tests

[coverage:run]
branch = True

我知道我可以将 @pytest.mark.no_cover 装饰器添加到集成测试中的每个测试函数,但我更愿意标记整个目录而不是装饰大量的功能。

最佳答案

您可以动态附加标记。下面的示例在 pytest_collection_modifyitems 的自定义实现中执行此操作钩。将代码放在项目根目录的 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)

关于python - pytest-cov - 不要计算集成测试目录的覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60608511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com