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?
No. Referencing the pytest_addoption hook documentation:
Note:
This function should be implemented only in plugins or
conftest.pyfiles situated at the tests root directory due to how pytest discovers plugins during startup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With