pytest_addoption() if options < 2

Created on 22 Dec 2020  路  2Comments  路  Source: pytest-dev/pytest

Using pytest_addoption() via list of options. If list contains less then 2 items - we'll get an error option names {'--a'} already added.

conftest.py

option_list = (
    'test_attribute'
)

def pytest_addoption(parser):
    parser.addoption('--env', action='store', default=None)
    for option in option_list:
        parser.addoption(f'--{option}', action='store', default=None)

output

ValueError: option names {'--a'} already added

but if <...> it will be ok

option_list = (
    'test_attribute_1',
    'test_attribute_2'
)

My environment

$ pip list

Package               Version
--------------------- -------
allure-pytest         2.3.2b1
allure-python-commons 2.3.2b1
pip                   20.2.4
PyHamcrest            1.9.0
pytest                3.4.0
PyYAML                5.3.1

$ sw_vers

ProductName:    macOS
ProductVersion: 11.0.1
BuildVersion:   20B50

  • [x] a detailed description of the bug or problem you are having
  • [x] output of pip list from the virtual environment you are using
  • [x] pytest and operating system versions
  • [x] minimal example if possible
invalid question

Most helpful comment

@pashkatrick as Ronny mentions, this is a user error, as brackets do not necessarily denote a tuple:

In [21]: type('one')
Out[21]: str

in your case, option_list = ('test_attribute',) is correct.

All 2 comments

User error, the constant is a string not a list/tuple

@pashkatrick as Ronny mentions, this is a user error, as brackets do not necessarily denote a tuple:

In [21]: type('one')
Out[21]: str

in your case, option_list = ('test_attribute',) is correct.

Was this page helpful?
0 / 5 - 0 ratings