From aa358433b0d63655981e2bac112ab5a490927716 Mon Sep 17 00:00:00 2001 From: Wes Thomas Date: Wed, 8 Aug 2018 18:13:21 -0500 Subject: [PATCH 1/3] Fix AttributeError bug in TestCaseFunction.teardown by creating TestCaseFunction._testcase as attribute of class with a None default. --- changelog/3788.bugfix.rst | 1 + src/_pytest/unittest.py | 1 + testing/test_unittest.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 changelog/3788.bugfix.rst diff --git a/changelog/3788.bugfix.rst b/changelog/3788.bugfix.rst new file mode 100644 index 000000000..d1bf68ba5 --- /dev/null +++ b/changelog/3788.bugfix.rst @@ -0,0 +1 @@ +Fix AttributeError bug in TestCaseFunction.teardown by creating TestCaseFunction._testcase as attribute of class with a None default. diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index d6e7cb272..a135dbd53 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -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) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 482e89280..444725ab9 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -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() From 051db6a33d75e0aff4664be12eb85206ec9d8234 Mon Sep 17 00:00:00 2001 From: Wes Thomas Date: Wed, 8 Aug 2018 18:18:18 -0500 Subject: [PATCH 2/3] Trimming Trailing Whitespace --- testing/test_unittest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 444725ab9..56fdebf48 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1002,7 +1002,7 @@ def test_testcase_handles_init_exceptions(testdir): import pytest class MyTestCase(TestCase): def __init__(self, *args, **kwargs): - raise Exception("should raise this exception") + raise Exception("should raise this exception") def test_hello(self): pass """ From 74d9f56d0f254f63468a3e53f1691e591d2e4c75 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 8 Aug 2018 21:24:14 -0300 Subject: [PATCH 3/3] Improve CHANGELOG a bit --- changelog/3788.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3788.bugfix.rst b/changelog/3788.bugfix.rst index d1bf68ba5..aa391e28b 100644 --- a/changelog/3788.bugfix.rst +++ b/changelog/3788.bugfix.rst @@ -1 +1 @@ -Fix AttributeError bug in TestCaseFunction.teardown by creating TestCaseFunction._testcase as attribute of class with a None default. +Fix ``AttributeError`` during teardown of ``TestCase`` subclasses which raise an exception during ``__init__``.