Some Name
Some Name

Reputation: 9521

How to put parser.addoption in a test module, not in conftest.py?

I have the following

conftest.py:

def pytest_addoption(parser):
    parser.addoption('--sopt', action='store', default=None, help='Source Data Storage')

my_test.py:

@pytest.fixture(scope='module', autouse=True)
def sopt(pytestconfig):
    return pytestconfig.getoption('sopt')


def test_mtest(sopt):
    //pytest code

When running this test it works fine python3 -m pytest --sopt=aaaaa, but when moving

def pytest_addoption(parser):
    parser.addoption('--sopt', action='store', default=None, help='Source Data Storage')

from conftest.py to my_test.py it does not work and fails with the following error:

ERROR: usage: __main__.py [options] [file_or_dir] [file_or_dir] [...]
__main__.py: error: unrecognized arguments: --sopt=aaaaa

I don't like to have a separate file for just 2 lines of code.

Is there a way to put parser.addoption from conftest.py intomy_test.py and make it work?

Upvotes: 4

Views: 4401

Answers (1)

hoefling
hoefling

Reputation: 66231

No. Referencing the pytest_addoption hook documentation:

Note:

This function should be implemented only in plugins or conftest.py files situated at the tests root directory due to how pytest discovers plugins during startup.

Upvotes: 5

Related Questions