On 2016-10-26 11:01 AM, holger krekel wrote:
James, as you have posted several questions already wrt to pytest may i suggest
you rather subscribe and start posting to:
https://mail.python.org/mailman/listinfo/pytest-dev
Moreover, your examples so far contained some glitches which indicate you are
not that experienced with Python yet. That's totally fine but i suggest you
try to keep things as simple as you can before using all kinds of Python
features.
As to the mail below you can instantiate a class from a fixture and provide
that to a test:
class DoStuff():
def __init__(self, a,b):
self.a=a
self.b=b
def log(self, msg):
# do logging stuff
pass
@pytest.fixture
def do_stuff(a, b):
return DoStuff(a, b)
best,
holger
I still can't make it work:
=================================== FAILURES
===================================
______________________________ TestAclass.test_1
_______________________________
self = <testscript.TestAclass instance at 0x7f87395bffc8>
request = <FixtureRequest for <Function 'test_1'>>
def test_1(self,request):
> ds=do_stuff(a,b)
E NameError: global name 'do_stuff' is not defined
testscript.py:9: NameError
Is it a python error?
# conftest.py
import pytest
@pytest.fixture(scope='module')
def need_a(request):
value = request.config.getoption("A")
if not value:
pytest.skip('test needs -A option to run')
return value
@pytest.fixture(scope='module')
def a(request):
a = pytest.config.getoption("A")
return a
@pytest.fixture(scope='module')
def b(request):
b = pytest.config.getoption("B")
return b
def pytest_addoption(parser):
parser.addoption("--A", action="store", default=None, help="a option")
parser.addoption("--B", action="store", default=None, help="b option")
class DoStuff():
def __init__(self,a,b):
self.a=a
self.b=b
def log(self,msg):
# do logging stuff
pass
@pytest.fixture(scope='module')
def do_stuff(a,b):
# I tried def do_stuff(a,b,request):
return DoStuff(a, b)
import pytest
# testscript.py
@pytest.mark.incremental
class TestAclass():
def test_1(self,request):
ds=do_stuff(a,b)
print 'inside {0}-{1}
step'.format(self.__class__.__name__,request.function.__name__)
print 'a={0}, b={1}'.format(ds.a,ds.b)
ds.log("we are here")
pass
_______________________________________________
pytest-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pytest-dev