[TIP] get fixtures for each parameter or global variables?
James
bjlockie at lockie.ca
Fri Oct 21 17:02:26 PDT 2016
# conftest.py
import pytest
@pytest.fixture(scope='module')
def a(request):
value = request.config.getoption("A")
if not value:
pytest.skip('test needs -A option to run')
return value
class global_vars():
def __init__(self):
self.a=''
self.b=''
@pytest.fixture(scope='module')
def get_globals():
gv=global_vars()
gv.a = pytest.config.getoption("A")
gv.b = pytest.config.getoption("B")
return gv
def pytest_addoption(parser):
parser.addoption("--A", action="store", default=None, help="a option")
parser.addoption("--B", action="store", default=None, help="b option")
import inspect
import pytest
# testscript.py
@pytest.mark.usefixtures('get_globals')
class TestAclass():
def test_1(self):
print 'inside {0}-{1}
step'.format(self.__class__.__name__,inspect.currentframe().f_code.co_name)
gv=get_globals()
print 'a={0}, b={1}'.format(gv.a, gv.b)
pass
The output is:
______________________________ TestAclass.test_1
_______________________________
self = <testscript.TestAclass instance at 0x7f2e48c12f38>
def test_1(self):
print 'inside {0}-{1}
step'.format(self.__class__.__name__,inspect.currentframe().f_code.co_name)
> gv=get_globals()
E NameError: global name 'get_globals' is not defined
testscript.py:11: NameError
I'm trying to store the parameters in global variables so it doesn't
need to do pytest.config.getoption every time it wants a parameter.
Maybe I should just make get fixtures for each parameter?
More information about the testing-in-python
mailing list