Skip to content

Commit f08a77d

Browse files
committed
python: add back instance accessor to all python nodes, not just Function
Regressed in 062d91a (pytest 7.0.0rc1 only). Fix pytest-dev#9486.
1 parent abe2a8f commit f08a77d

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/_pytest/python.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ def cls(self):
283283
node = self.getparent(Class)
284284
return node.obj if node is not None else None
285285

286+
@property
287+
def instance(self):
288+
"""Python instance object the function is bound to.
289+
290+
Returns None if not a test method, e.g. for a standalone test function,
291+
a staticmethod, a class or a module.
292+
"""
293+
node = self.getparent(Function)
294+
return getattr(node.obj, "__self__", None) if node is not None else None
295+
286296
@property
287297
def obj(self):
288298
"""Underlying Python object."""
@@ -1693,15 +1703,6 @@ def function(self):
16931703
"""Underlying python 'function' object."""
16941704
return getimfunc(self.obj)
16951705

1696-
@property
1697-
def instance(self):
1698-
"""Python instance object the function is bound to.
1699-
1700-
Returns None if not a test method, e.g. for a standalone test function
1701-
or a staticmethod.
1702-
"""
1703-
return getattr(self.obj, "__self__", None)
1704-
17051706
def _getobj(self):
17061707
assert self.parent is not None
17071708
if isinstance(self.parent, Class):

testing/test_collection.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_fail(): assert 0
6464

6565
assert pytester.collect_by_name(modcol, "doesnotexist") is None
6666

67-
def test_getparent(self, pytester: Pytester) -> None:
67+
def test_getparent_and_accessors(self, pytester: Pytester) -> None:
6868
modcol = pytester.getmodulecol(
6969
"""
7070
class TestClass:
@@ -77,14 +77,21 @@ def test_foo(self):
7777
fn = pytester.collect_by_name(cls, "test_foo")
7878
assert isinstance(fn, pytest.Function)
7979

80-
module_parent = fn.getparent(pytest.Module)
81-
assert module_parent is modcol
82-
83-
function_parent = fn.getparent(pytest.Function)
84-
assert function_parent is fn
85-
86-
class_parent = fn.getparent(pytest.Class)
87-
assert class_parent is cls
80+
assert fn.getparent(pytest.Module) is modcol
81+
assert modcol.module is not None
82+
assert modcol.cls is None
83+
assert modcol.instance is None
84+
85+
assert fn.getparent(pytest.Class) is cls
86+
assert cls.module is not None
87+
assert cls.cls is not None
88+
assert cls.instance is None
89+
90+
assert fn.getparent(pytest.Function) is fn
91+
assert fn.module is not None
92+
assert fn.cls is not None
93+
assert fn.instance is not None
94+
assert fn.function is not None
8895

8996
def test_getcustomfile_roundtrip(self, pytester: Pytester) -> None:
9097
hello = pytester.makefile(".xxx", hello="world")

0 commit comments

Comments
 (0)