Skip to content

Commit 36d5c0e

Browse files
tests: Check urlpatterns after cleanups (#9400)
According to docs: https://docs.python.org/3/library/unittest.html#unittest.TestCase.addClassCleanup > Add a function to be called after tearDownClass() to cleanup resources used during the test class. Functions will be called in reverse order to the order they are added (LIFO). This was revealed with recent change in pytest (`8.2.0`): > pytest-dev/pytest#11728: For unittest-based tests, exceptions during class cleanup (as raised by functions registered with TestCase.addClassCleanup) are now reported instead of silently failing. `check_urlpatterns` is called before `cleanup_url_patterns` and fails (problem was hidden by `pytest < 8.2.0`). `doClassCleanups` can be used instead to check after-cleanup state: https://docs.python.org/3/library/unittest.html#unittest.TestCase.doClassCleanups > This method is called unconditionally after tearDownClass(), or after setUpClass() if setUpClass() raises an exception. It is responsible for calling all the cleanup functions added by addClassCleanup(). If you need cleanup functions to be called prior to tearDownClass() then you can call doClassCleanups() yourself. Fixes: https://github.com/encode/django-rest-framework/issues/9399 Signed-off-by: Stanislav Levin <[email protected]>
1 parent 9d4ed05 commit 36d5c0e

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Diff for: tests/test_testing.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,6 @@ def test_empty_request_content_type(self):
318318
assert request.META['CONTENT_TYPE'] == 'application/json'
319319

320320

321-
def check_urlpatterns(cls):
322-
assert urlpatterns is not cls.urlpatterns
323-
324-
325321
class TestUrlPatternTestCase(URLPatternsTestCase):
326322
urlpatterns = [
327323
path('', view),
@@ -333,10 +329,11 @@ def setUpClass(cls):
333329
super().setUpClass()
334330
assert urlpatterns is cls.urlpatterns
335331

336-
cls.addClassCleanup(
337-
check_urlpatterns,
338-
cls
339-
)
332+
@classmethod
333+
def doClassCleanups(cls):
334+
assert urlpatterns is cls.urlpatterns
335+
super().doClassCleanups()
336+
assert urlpatterns is not cls.urlpatterns
340337

341338
def test_urlpatterns(self):
342339
assert self.client.get('/').status_code == 200

0 commit comments

Comments
 (0)