about summary refs log tree commit diff
path: root/pkgs/development/python-modules/pytest/8.2.2-unittest-testcase-regression.patch
blob: 1d127b191f8184319730073949912c0ec038faa4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
From de47b73520fd9b7e41272701d7fd4663357af046 Mon Sep 17 00:00:00 2001
From: Ran Benita <ran@unusedvar.com>
Date: Fri, 7 Jun 2024 09:49:29 +0300
Subject: [PATCH] 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.
---
 changelog/12424.bugfix.rst                            |  1 +
 src/_pytest/unittest.py                               |  2 +-
 .../pytest_rerunfailures_integration.py               | 11 +++++++++++
 tox.ini                                               |  2 +-
 4 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 changelog/12424.bugfix.rst
 create mode 100644 testing/plugins_integration/pytest_rerunfailures_integration.py

diff --git a/changelog/12424.bugfix.rst b/changelog/12424.bugfix.rst
new file mode 100644
index 00000000000..7ad1126858b
--- /dev/null
+++ b/changelog/12424.bugfix.rst
@@ -0,0 +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.
diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py
index 643443f08c6..ca82ac5c14a 100644
--- a/src/_pytest/unittest.py
+++ b/src/_pytest/unittest.py
@@ -222,7 +222,7 @@ def teardown(self) -> None:
             self._explicit_tearDown()
             self._explicit_tearDown = None
         self._obj = None
-        self._instance = None
+        del self._instance
         super().teardown()
 
     def startTest(self, testcase: "unittest.TestCase") -> None:
diff --git a/testing/plugins_integration/pytest_rerunfailures_integration.py b/testing/plugins_integration/pytest_rerunfailures_integration.py
new file mode 100644
index 00000000000..9a13a3279a9
--- /dev/null
+++ b/testing/plugins_integration/pytest_rerunfailures_integration.py
@@ -0,0 +1,11 @@
+import unittest
+
+
+class MyTestCase(unittest.TestCase):
+    first_time = True
+
+    def test_fail_the_first_time(self) -> None:
+        """Regression test for issue #12424."""
+        if self.first_time:
+            type(self).first_time = False
+            self.fail()
diff --git a/tox.ini b/tox.ini
index 0a3f0acf5b8..35b335a015d 100644
--- a/tox.ini
+++ b/tox.ini
@@ -141,7 +141,7 @@ commands =
     pytest --cov=. simple_integration.py
     pytest --ds=django_settings simple_integration.py
     pytest --html=simple.html simple_integration.py
-    pytest --reruns 5 simple_integration.py
+    pytest --reruns 5 simple_integration.py pytest_rerunfailures_integration.py
     pytest pytest_anyio_integration.py
     pytest pytest_asyncio_integration.py
     pytest pytest_mock_integration.py