Why is pytest giving me an "unrecognized option" error when I try and create a custom command line option?

Viewed 8673

I'm using Python 3.8 and pytest 6.0.1. How do I create a custom command line option for pytest? I thought it was as simple as adding this to conftest.py ...

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

but when I pytest, I get an unrecognized option error

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

What's the right way to add a custom option?

Edit: I tried the answer given. I included some other things in my tests/conftest.py file in case those are the reason the answer isn't working. File contains

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))

But output when running is

$ 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
2 Answers

As already mentioned in a comment action='store_const' makes your option a flag. The value you receive when you read option value provided it is specified on cli is the one specified by const i.e. True in your case.

Try this: add below function to conftest.py

def pytest_generate_tests(metafunc):
    option1value = metafunc.config.getoption("--option1")
    print(f'Option1 Value = {option1value}')
  1. pytest invoked with option pytest -s --option1

    Output will have : Option1 Value = True

  2. pytest invoked without option pytest -s

    Output will have : Option1 Value = None

action=store might give you the desired behavior.

Solution:

# 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

Output:

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

You can find details on available action(s) and more here: https://docs.python.org/3/library/argparse.html#action

I think order matters in this case.

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

Related