Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 871
Some Name Avatar asked Oct 30 '25 07:10

Some Name


1 Answers

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.

like image 120
hoefling Avatar answered Nov 01 '25 20:11

hoefling