Skip to content

Commit 1f8fd42

Browse files
committed
item.obj is again a bound method on TestCase function items
Fix #5390
1 parent 76d5080 commit 1f8fd42

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

changelog/5390.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression where the ``obj`` attribute of ``TestCase`` items was no longer bound to methods.

src/_pytest/unittest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ class TestCaseFunction(Function):
108108

109109
def setup(self):
110110
self._testcase = self.parent.obj(self.name)
111+
self._obj = getattr(self._testcase, self.name)
111112
if hasattr(self, "_request"):
112113
self._request._fillfixtures()
113114

114115
def teardown(self):
115116
self._testcase = None
117+
self._obj = None
116118

117119
def startTest(self, testcase):
118120
pass

testing/test_unittest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ def test_func2(self):
139139
reprec.assertoutcome(passed=2)
140140

141141

142+
def test_function_item_obj_is_instance(testdir):
143+
"""item.obj should be a bound method on unittest.TestCase function items (#5390)."""
144+
testdir.makeconftest(
145+
"""
146+
def pytest_runtest_makereport(item, call):
147+
if call.when == 'call':
148+
class_ = item.parent.obj
149+
assert isinstance(item.obj.__self__, class_)
150+
"""
151+
)
152+
testdir.makepyfile(
153+
"""
154+
import unittest
155+
156+
class Test(unittest.TestCase):
157+
def test_foo(self):
158+
pass
159+
"""
160+
)
161+
result = testdir.runpytest_inprocess()
162+
result.stdout.fnmatch_lines(["* 1 passed in*"])
163+
164+
142165
def test_teardown(testdir):
143166
testpath = testdir.makepyfile(
144167
"""

0 commit comments

Comments
 (0)