在中设置不同的测试路径pytest.ini文件

2024-09-28 20:47:38 发布

您现在位置:Python中文网/ 问答频道 /正文

有什么方法可以设置不同的测试路径吗pytest.init文件为了我的测试。所以我可以执行类似 pytest-xx那么所有的测试用例都可以从一组多个目录执行 pytestyy那么所有的测试用例都可以从另一组多个目录执行 pytest all然后可以执行所有testcase

到目前为止。在unitA,unitB,unitC方向下进行一组测试,在回归a、回归b、回归c下进行另一组测试。所以我不需要输入 pytest unitA,unitB,unitC pytest回归a,回归b,回归c

我的pytest.ini文件. 在

[pytest]
testpaths = unitA unitB

Tags: 文件方法路径目录initpytest测试用例all
1条回答
网友
1楼 · 发布于 2024-09-28 20:47:38

测试路径可以作为位置参数传递给pytest。如果您在*nix上,可以使用shell glob扩展来匹配多个目录:pytest unit*将被展开为pytest unitA unitB unitC。类似地,pytest unit{A,C}将扩展为pytest unitA unitC。在

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

# 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()]

相关问题 更多 >