Search code examples
optparsepytest

py.test Within same directory structure cannot have pytest_addoption with same option name


I have the following test directory structure and each has their own conftest.py:

    tests/api
    tests/api/newapi
    tests/sanity

In conftest.py, pytest_addoption has identical option entry as test_option:

parser.addoption("--test_option", dest="test_option", type="string", help="test_option")

Execute py.test at tests/api/newapi has following error:

optparse.OptionConflictError: option --test_option: conflicting option string(s): --test_option

But execute py.test at tests/api or tests or tests/sanity works fine. Is this by design that I must come up with a new option name if the parent directory has same option name?

I was hoping that I can keep the naming convention the same since the option in question serve the same purpose for the test. The intent is to vary the level of detail in testing depending on which directory the test was kicked off from.


Solution

  • I think you should have one tests/conftest.py which defines the option via pytest_addoption and you will be able to access it from everywhere because all conftest.py files are considered (so you might still have at tests/newapi/conftest.py and use the option value).