depot/third_party/nixpkgs/pkgs/development/python-modules/pytest/8.2.2-unittest-testcase-regression.patch
Default email f34ce41345 Project import generated by Copybara.
GitOrigin-RevId: b73c2221a46c13557b1b3be9c2070cc42cf01eb3
2024-07-27 08:49:29 +02:00

72 lines
2.8 KiB
Diff

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