Skip to content

Commit de47b73

Browse files
committed
unittest: fix assertion errors on unittest reruns
This fixes unittest test reruns when using plugins like pytest-rerunfailures. The `instance` property uses AttributeError to check if the instance needs to be initialized, so `del` is the correct way to clear it, not setting to `None`. Regressed in 8.2.2.
1 parent 8585c58 commit de47b73

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

changelog/12424.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash with `assert testcase is not None` assertion failure when re-running unittest tests using plugins like pytest-rerunfailures. Regressed in 8.2.2.

src/_pytest/unittest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def teardown(self) -> None:
222222
self._explicit_tearDown()
223223
self._explicit_tearDown = None
224224
self._obj = None
225-
self._instance = None
225+
del self._instance
226226
super().teardown()
227227

228228
def startTest(self, testcase: "unittest.TestCase") -> None:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
3+
4+
class MyTestCase(unittest.TestCase):
5+
first_time = True
6+
7+
def test_fail_the_first_time(self) -> None:
8+
"""Regression test for issue #12424."""
9+
if self.first_time:
10+
type(self).first_time = False
11+
self.fail()

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ commands =
141141
pytest --cov=. simple_integration.py
142142
pytest --ds=django_settings simple_integration.py
143143
pytest --html=simple.html simple_integration.py
144-
pytest --reruns 5 simple_integration.py
144+
pytest --reruns 5 simple_integration.py pytest_rerunfailures_integration.py
145145
pytest pytest_anyio_integration.py
146146
pytest pytest_asyncio_integration.py
147147
pytest pytest_mock_integration.py

0 commit comments

Comments
 (0)