简体   繁体   中英

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 into my_test.py and make it work?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM