Python pytest是否限制自定义命令行参数中的所有小写选项?

Python pytest是否限制自定义命令行参数中的所有小写选项?,python,pytest,Python,Pytest,我试图使用pytest的pytest\u addoption将一些命令行参数从argparse.ArgumentParser实例移植到等效的pytestconftest.py文件中 查看pytest帮助函数,我发现有12个使用过的单字符短选项,即k、m、x、c、s、v、q、r、l、h、p、o MacBook-Pro:~ user$ pytest --help | grep " -[a-z]" -k EXPRESSION only run tests which match t

我试图使用pytest的
pytest\u addoption将一些命令行参数从
argparse.ArgumentParser
实例移植到等效的pytest
conftest.py
文件中

查看pytest帮助函数,我发现有12个使用过的单字符短选项,即
k、m、x、c、s、v、q、r、l、h、p、o

MacBook-Pro:~ user$ pytest --help | grep " -[a-z]"
  -k EXPRESSION         only run tests which match the given substring
                        -k 'test_method or test_other' matches all test
                        'test_method' or 'test_other', while -k 'not
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  -x, --exitfirst       exit instantly on first error or failed test.
  -c file               load configuration from `file` instead of trying to
  -s                    shortcut for --capture=no.
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  -r chars              show extra test summary info as specified by chars
  -l, --showlocals      show locals in tracebacks (disabled by default).
  -h, --help            show help message and configuration info
  -p name               early-load given plugin (multi-allowed). To avoid
  -o [OVERRIDE_INI [OVERRIDE_INI ...]], --override-ini=[OVERRIDE_INI [OVERRIDE_INI ...]]
然而,如果我试图在该集合之外定义一个选项,我会得到以下异常

def pytest_addoption(parser):
    parser.addoption('-b', '--build_special' )


  File "/Library/Python/2.7/site-packages/_pytest/config/argparsing.py", line 72, in addoption
    self._anonymous.addoption(*opts, **attrs)
  File "/Library/Python/2.7/site-packages/_pytest/config/argparsing.py", line 305, in addoption
    self._addoption_instance(option, shortupper=False)
  File "/Library/Python/2.7/site-packages/_pytest/config/argparsing.py", line 315, in _addoption_instance
    raise ValueError("lowercase shortoptions reserved")
ValueError: lowercase shortoptions reserved
无论我选择什么样的角色,这似乎都是事实,这让我觉得这只是一个受限制的行为期

问题:pytest是否限制任何和所有短选项的使用?


我有点困惑,因为当你看相关的pytest时

def addoption(self,*opts,**attrs):
“”“注册命令行选项。
:opts:选项名称,可以是短选项或长选项。
:attrs:与
`argparse库
`_
接受。
pytest配置中提供了命令行解析后选项
通过``config.option.NAME``设置的对象,其中通常设置``NAME``
例如,通过传递``dest``属性
``addoption(“--long”,dest=“NAME”,…)```。
"""
self.\u anonymous.addoption(*选项,**属性)
它似乎建议我可以使用与argparse的add_选项完全相同的语法,这在argparse中非常有趣地提到了这一点

将所有
optpass.OptionParser.add_option()
调用替换为
ArgumentParser.添加参数()
调用

并且add_参数明确列出了允许的单个标志选项:

add_argument()
方法必须知道可选参数, 例如
-f
-foo
,或位置参数,例如 应为文件名。传递给add_argument()的第一个参数 因此必须是一系列标志或简单的参数名。 例如,可以创建一个可选参数,如下所示:

>解析器。添加_参数('-f','-foo')


从pytest源代码

是的,它们限制所有小写的短选项

def addoption(self, *opts, **attrs):
    """ register a command line option.
    :opts: option names, can be short or long options.
    :attrs: same attributes which the ``add_option()`` function of the
       `argparse library
       <http://docs.python.org/2/library/argparse.html>`_
       accepts.
    After command line parsing options are available on the pytest config
    object via ``config.option.NAME`` where ``NAME`` is usually set
    by passing a ``dest`` attribute, for example
    ``addoption("--long", dest="NAME", ...)``.
    """
    self._anonymous.addoption(*opts, **attrs)
def _addoption_instance(self, option: "Argument", shortupper: bool = False) -> None:
    if not shortupper:
        for opt in option._short_opts:
            if opt[0] == "-" and opt[1].islower():
                raise ValueError("lowercase shortoptions reserved")
    if self.parser:
        self.parser.processoption(option)
    self.options.append(option)