Skip to content

Commit

Permalink
Merge pull request #3792 from decisio/pr-3788-fix-teardown-exception
Browse files Browse the repository at this point in the history
Pr 3788 fix teardown exception
  • Loading branch information
RonnyPfannschmidt committed Aug 9, 2018
2 parents e723069 + 74d9f56 commit 5d3c512
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/3788.bugfix.rst
@@ -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
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
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()

0 comments on commit 5d3c512

Please sign in to comment.