Skip to content

Commit 8076f48

Browse files
authored
[4.6] Merge pull request #5393 from nicoddemus/unittest-self-5390 (#5399)
[4.6] Merge pull request #5393 from nicoddemus/unittest-self-5390
2 parents 937f945 + 92432ac commit 8076f48

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
@@ -114,6 +114,7 @@ class TestCaseFunction(Function):
114114
def setup(self):
115115
self._testcase = self.parent.obj(self.name)
116116
self._fix_unittest_skip_decorator()
117+
self._obj = getattr(self._testcase, self.name)
117118
if hasattr(self, "_request"):
118119
self._request._fillfixtures()
119120

@@ -132,6 +133,7 @@ def _fix_unittest_skip_decorator(self):
132133

133134
def teardown(self):
134135
self._testcase = None
136+
self._obj = None
135137

136138
def startTest(self, testcase):
137139
pass

testing/test_unittest.py

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

146146

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

0 commit comments

Comments
 (0)