Skip to content

Pr 3788 fix teardown exception #3792

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3788.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``AttributeError`` during teardown of ``TestCase`` subclasses which raise an exception during ``__init__``.
1 change: 1 addition & 0 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def collect(self):
class TestCaseFunction(Function):
nofuncargs = True
_excinfo = None
_testcase = None

def setup(self):
self._testcase = self.parent.obj(self.name)
Expand Down
21 changes: 21 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,24 @@ def test_two(self):

result = testdir.runpytest("-s")
result.assert_outcomes(passed=2)


def test_testcase_handles_init_exceptions(testdir):
"""
Regression test to make sure exceptions in the __init__ method are bubbled up correctly.
See https://github.com/pytest-dev/pytest/issues/3788
"""
testdir.makepyfile(
"""
from unittest import TestCase
import pytest
class MyTestCase(TestCase):
def __init__(self, *args, **kwargs):
raise Exception("should raise this exception")
def test_hello(self):
pass
"""
)
result = testdir.runpytest()
assert "should raise this exception" in result.stdout.str()
assert "ERROR at teardown of MyTestCase.test_hello" not in result.stdout.str()