【问题标题】:Why is pytest giving me an "unrecognized option" error when I try and create a custom command line option?当我尝试创建自定义命令行选项时,为什么 pytest 会出现“无法识别的选项”错误?
【发布时间】:2020-11-26 22:29:30
【问题描述】:

我正在使用 Python 3.8 和 pytest 6.0.1。如何为 pytest 创建自定义命令行选项?我认为这就像将它添加到 conftest.py 一样简单......

def pytest_addoption(parser):
    parser.addoption('--option1', action='store_const', const=True)

但是当我 pytest 时,我得到一个无法识别的选项错误

# pytest --option1=Y -c tests/my_test.py 
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --option1=Y

添加自定义选项的正确方法是什么?

编辑:我尝试了给出的答案。我在我的 tests/conftest.py 文件中包含了一些其他的东西,以防这些是答案不起作用的原因。文件包含

def pytest_generate_tests(metafunc):
    option1value = metafunc.config.getoption("--option1")
    print(f'Option1 Value = {option1value}')

def pytest_configure(config):
    use_docker = False
    try:
        use_docker = config.getoption("--docker-compose-remove-volumes")
    except:
        pass
    plugin_name = 'wait_for_docker' if use_docker else 'wait_for_server'
    if not config.pluginmanager.has_plugin(plugin_name):
        config.pluginmanager.import_plugin("tests.plugins.{}".format(plugin_name))

但是运行时的输出是

$ pytest -s --option1 tests/shared/model/test_crud_functions.py
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --option1
  inifile: /Users/davea/Documents/workspace/my_project/pytest.ini
  rootdir: /Users/davea/Documents/workspace/my_project

【问题讨论】:

  • 如果您使用store_const,则该选项将作为标志处理并且不能有值,例如你必须使用pytest --option1。如果要设置不同的值,可以改用默认的store 操作。
  • 我投票关闭这个作为一个骗子,在pytest 已经有很多关于自定义 cli args 的答案,参见例如stackoverflow.com/q/40880259/2650249 和链接的问题。
  • 问这个问题可能有点傻:你确定你把这个钩子添加到你的根 conftest.py 中了吗?
  • 你的意思是“tests/conftest.py”?是的,我知道它在那里,因为如果我只运行“pytest tests/shared/model/test_crud_functions.py”,我会收到错误,“ValueError:没有名为'--option1'的选项”。令人沮丧的是,当我运行“pytest --option1=abc tests/shared/model/test_crud_functions.py”时,我得到了“pytest: error: unrecognized arguments: --option1=abc”错误。

标签: python-3.x command-line pytest conftest


【解决方案1】:

正如评论action='store_const' 中已经提到的那样,您的选择是一个标志。如果您在 cli 上指定选项值,则在读取选项值时收到的值是 const 指定的值,即在您的情况下为 True

试试这个: 将以下函数添加到 conftest.py

def pytest_generate_tests(metafunc):
    option1value = metafunc.config.getoption("--option1")
    print(f'Option1 Value = {option1value}')
  1. 使用选项pytest -s --option1调用pytest

    输出将有: Option1 Value = True

  2. 在没有选项pytest -s的情况下调用pytest

    输出将有: Option1 Value = None

action=store 可能会给你想要的行为。

解决方案

# Change the action associated with your option to action='store'
def pytest_addoption(parser):
    parser.addoption('--option1', action='store')


def pytest_configure(config):
    x = config.getoption('option1')
    print(x)  # Any logic that uses option value

输出:

pytest -s --option1=Y -c=test.py
Y
============================================================================= test session starts ==============================================================================
platform darwin -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1

您可以在此处找到有关可用操作的详细信息及更多信息:https://docs.python.org/3/library/argparse.html#action

【讨论】:

  • 谢谢。试一试,但出现错误,“错误:用法:pytest [options] [file_or_dir] [file_or_dir] [...] pytest:错误:无法识别的参数:--option1”。完整的跟踪和代码是对我的问题的修改。
  • 嗨,戴夫,您需要使用这个,parser.addoption('--option1', action='store') 已编辑答案,请查看答案的解决方案部分。
【解决方案2】:

我认为在这种情况下顺序很重要。

试试pytest -c tests/my_test.py --option1=Y

【讨论】:

  • 我认为“-c”是指一个“.ini”文件,不是吗?按您的方式运行命令会给出错误,“错误:用法:pytest [options] [file_or_dir] [file_or_dir] [...],pytest:错误:无法识别的参数:--option1 = Y,inifile:/Users/ davea/Documents/workspace/my_project/tests/my_test.py”。删除“-c”仍然给出错误“错误:用法:pytest [options] [file_or_dir] [file_or_dir] [...],pytest:错误:无法识别的参数:--option1 = Y,inifile:/Users/davea/文件/工作区/my_project/pytest.ini”。
猜你喜欢
  • 1970-01-01
  • 2011-05-22
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多