Fix AttributeError bug in TestCaseFunction.teardown by creating TestCaseFunction._testcase as attribute of class with a None default.

This commit is contained in:
Wes Thomas 2018-08-08 18:13:21 -05:00
parent e723069165
commit aa358433b0
3 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1 @@
Fix AttributeError bug in TestCaseFunction.teardown by creating TestCaseFunction._testcase as attribute of class with a None default.

View File

@ -69,6 +69,7 @@ class UnitTestCase(Class):
class TestCaseFunction(Function):
nofuncargs = True
_excinfo = None
_testcase = None
def setup(self):
self._testcase = self.parent.obj(self.name)

View File

@ -989,3 +989,24 @@ def test_usefixtures_marker_on_unittest(base, testdir):
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()