I have conftest.py (only for fixtures), support.py (where I store my functions used by multiple test modules) and test_*.py modules.
And both conftest.py and support.py import settings.py.
settings.py contains just a single list with settings.
I'd like to make those settings be dependent on the command args passed to pytest.
conftest.py:
...
def pytest_addoption(parser):
parser.addoption('--buildid', metavar='buildid', default=1, type=int, nargs='*', help='a number to be used as a prefix to separate screenshots for test runs')
@pytest.fixture(scope='module')
def buildid(request):
return request.config.getoption('--buildid')
...
how to access buildid command line argument from settings.py?
This fails:
print(settings['buildid'])
settings = { "buildid": buildid }
because buildid is a variable pointing to the function, not a variable with text.
command line options are intentionally not availiable at import time,
the usual solution to update globals from commandline options is to write a pytest_configure hook that takes the commandline option and puts it into the values
for example
```
def pytest_addoption(parser):
parser.addoption(
'--buildid', metavar='buildid', default=1, type=int, nargs='*',
help='a number to be used as a prefix to separate screenshots for test runs')
def pytest_configure(config):
import settings
settings.buildid = config.getoption('--buildid')
`````
@RonnyPfannschmidt
Thank you!
I had read about pytest_configure(config) in the docs, but that told me almost nothing about what's the use for it.
Using pytest_configure(config) I figured out I didn't need all the pytest fixtures like
@pytest.fixture(scope='module')
def buildid(request):
return request.config.getoption('--buildid')
which caused way too many complications for me (I had to add each of them as an argument to the test functions in the test modules).
Thanks a lot!
Most helpful comment
command line options are intentionally not availiable at import time,
the usual solution to update globals from commandline options is to write a pytest_configure hook that takes the commandline option and puts it into the values
for example
```
def pytest_addoption(parser):
parser.addoption(
'--buildid', metavar='buildid', default=1, type=int, nargs='*',
help='a number to be used as a prefix to separate screenshots for test runs')
def pytest_configure(config):
import settings
settings.buildid = config.getoption('--buildid')
`````