简体   繁体   English

如何将命令行参数添加到 pytest 命令

[英]How to add command line parameter to pytest command

I cannot find how to add named command line parameter to pytest command, so I can execute tests with custom parameter available as a fixture.我找不到如何将命名命令行参数添加到 pytest 命令,因此我可以使用可用作夹具的自定义参数执行测试。

pytest --my-parameter

def test_something(my_parameter):
    ...

In order to accomplish such behaviour the pytest_addoption shall be defined and new session level fixture in combination with pytestconfig fixture shall be used为了完成这种行为,应定义pytest_addoption并使用新的 session 级别夹具与pytestconfig夹具

# conftest.py
import pytest


def pytest_addoption(parser):
    parser.addoption(
        "--my-parameter", action="store", default=None,
        help=("Parameter description")
    )


@pytest.fixture(scope='session')
def my_parameter(pytestconfig):
    """ Description of changes triggered by parameter. """
    param = pytestconfig.getoption("--my-parameter")
    if param is None:
        assert False, '--my-parameter parameter must be supplied in order to run test suite.'
    return param

# tests.py:
import pytest


def test_something(my_parameter):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM