gpt4 book ai didi

python - 在 pytest.ini 中设置不同的测试路径

转载 作者:行者123 更新时间:2023-12-01 02:14:26 25 4
gpt4 key购买 nike

有什么方法可以在 pytest.init 下为我的测试设置不同的测试路径。所以我可以执行类似的事情pytest xx 那么所有测试用例都可以从一组多个方向执行pytest yy 那么所有测试用例都可以从另一组多个方向执行pytest all 那么所有测试用例都可以执行

到目前为止我已经做到了。一组测试在unitA、unitB、unitC 方向下进行,另一组测试在regressionA、regressionB、regressionC 下进行。所以我不需要输入pytest 单元A、单元B、单元Cpytest回归A,回归B,回归C

我的 pytest.ini。

[pytest]
testpaths = unitA unitB

最佳答案

测试路径可以作为位置参数传递给pytest。如果您使用的是 *nix,则可以使用 shell glob 扩展来匹配多个目录:pytest unit* 将扩展为 pytest unitA unitB unitC。同样,pytest unit{A,C} 将扩展为 pytest unitA unitC

但是,如果您需要自定义测试过滤或分组逻辑,您也可以定义自己的参数。开关 --unit-only 的示例,该开关仅在以 unit 开头的目录中运行测试,忽略 pytest 中的 testpaths 设置.ini:

# conftest.py
import pathlib
import pytest

def pytest_addoption(parser):
parser.addoption('--unit-only', action='store_true', default=False, help='only run tests in dirs starting with "unit".')


def pytest_configure(config):
unit_only = config.getoption('--unit-only')
if unit_only:
config.args = [p for p in pathlib.Path().rglob('unit*') if p.is_dir()]

关于python - 在 pytest.ini 中设置不同的测试路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48464052/

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