-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pytest does not show addClassCleanup errors #11728
Labels
plugin: unittest
related to the unittest integration builtin plugin
Comments
bluetech
added
the
plugin: unittest
related to the unittest integration builtin plugin
label
Jan 4, 2024
@bluetech hi, may I ask how is this going? I think it is rather inconvenient not being able to see certain errors. |
This workaround can be dropped in a plugin module or conftest.py import pytest
@pytest.hookimpl
def pytest_configure():
patch_unittest_TestCase_doClassCleanup()
def patch_unittest_TestCase_doClassCleanup():
"""Raise/print errors caught during class cleanup
pytest ignores `TestCase.tearDown_exceptions`, which causes them to
pass silently.
"""
@classmethod
def doClassCleanupAndRaiseLastError(cls):
doClassCleanups()
errors = cls.tearDown_exceptions
if errors:
if len(errors) > 1:
num = len(errors)
for n, (exc_type, exc, tb) in enumerate(errors[:-1], start=1):
print(f"\nclass cleanup error ({n} of {num}):", file=sys.stderr)
print_exception(exc_type, exc, tb)
raise errors[-1][1]
import sys
from traceback import print_exception
from unittest.case import TestCase
doClassCleanups = TestCase.doClassCleanups
TestCase.doClassCleanups = doClassCleanupAndRaiseLastError |
nicoddemus
added a commit
that referenced
this issue
Apr 27, 2024
Fixes #11728 --------- Co-authored-by: Bruno Oliveira <[email protected]>
stanislavlevin
added a commit
to stanislavlevin/django-rest-framework
that referenced
this issue
May 6, 2024
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: encode#9399 Signed-off-by: Stanislav Levin <[email protected]>
auvipy
pushed a commit
to encode/django-rest-framework
that referenced
this issue
May 7, 2024
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: #9399 Signed-off-by: Stanislav Levin <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Below in minimal example is a code that if I run with python, I get
which is fine, but if I run it with pytest I get:
Without any sign of that an error has happend. Therefore it fail silently without anyone knowing something wrong happend.
Expected: Pytest shows error as well.
pip list
from the virtual environment you are usingpytest and operating system versions
pytest 7.4.3
Centos7
minimal example if possible
The text was updated successfully, but these errors were encountered: