Skip to content

Commit b527dfc

Browse files
committed
patch instance
1 parent a8d8de2 commit b527dfc

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

pytest_lazyfixture.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def pytest_runtest_setup(item):
2323
if isinstance(item, TestCaseFunction):
2424
return
2525

26+
if isinstance(item.parent, Instance):
27+
_patch_instance(item)
28+
2629
fixturenames = item.fixturenames
2730
argnames = item._fixtureinfo.argnames
2831

@@ -35,14 +38,6 @@ def pytest_runtest_setup(item):
3538
if is_lazy_fixture(val):
3639
item.callspec.params[param] = item._request.getfixturevalue(val.name)
3740

38-
if isinstance(item.parent, Instance):
39-
def newinstance(self):
40-
return self.obj
41-
42-
item.parent.newinstance = types.MethodType(
43-
newinstance, item.parent
44-
)
45-
4641

4742
def pytest_runtest_call(item):
4843
if hasattr(item, 'funcargs'):
@@ -135,6 +130,18 @@ def get_nodeid(module, rootdir):
135130
return relpath
136131

137132

133+
def _patch_instance(item):
134+
obj = Instance.newinstance(item.parent)
135+
item.obj = item._getobj()
136+
137+
def newinstance(self):
138+
return obj
139+
140+
item.parent.newinstance = types.MethodType(
141+
newinstance, item.parent
142+
)
143+
144+
138145
def lazy_fixture(names):
139146
if isinstance(names, string_type):
140147
return LazyFixture(names)

tests/test_lazyfixture.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,15 @@ class TestModels(object):
391391
def setup(self):
392392
self.var = 15
393393
394-
def test_model(self):
394+
def test_model_a(self):
395+
assert self.var == 15
396+
397+
def test_model_b(self):
395398
assert self.var == 15
396399
397400
""")
398401
reprec = testdir.inline_run('-s', '-v')
399-
reprec.assertoutcome(passed=1)
402+
reprec.assertoutcome(passed=2)
400403

401404

402405
def test_issues12_skip_test_function(testdir):

0 commit comments

Comments
 (0)