Fix issue where pytest.raises() doesn't always return Exception instance in py26

Fixes #767
This commit is contained in:
Bruno Oliveira 2015-06-18 21:04:47 -03:00
parent 0431b8bb74
commit eb73db56c7
3 changed files with 12 additions and 0 deletions

View File

@ -1,6 +1,10 @@
2.7.2 (compared to 2.7.1) 2.7.2 (compared to 2.7.1)
----------------------------- -----------------------------
- fix issue767: pytest.raises value attribute does not contain the exception
instance on Python 2.6. Thanks Eric Siegerman for providing the test
case and Bruno Oliveira for PR.
- Automatically create directory for junitxml and results log. - Automatically create directory for junitxml and results log.
Thanks Aron Curzon. Thanks Aron Curzon.

View File

@ -1099,6 +1099,13 @@ class RaisesContext(object):
__tracebackhide__ = True __tracebackhide__ = True
if tp[0] is None: if tp[0] is None:
pytest.fail("DID NOT RAISE") pytest.fail("DID NOT RAISE")
if sys.version_info < (2, 7):
# py26: on __exit__() exc_value often does not contain the
# exception value.
# http://bugs.python.org/issue7853
if not isinstance(tp[1], BaseException):
exc_type, value, traceback = tp
tp = exc_type, exc_type(value), traceback
self.excinfo.__init__(tp) self.excinfo.__init__(tp)
return issubclass(self.excinfo.type, self.ExpectedException) return issubclass(self.excinfo.type, self.ExpectedException)

View File

@ -46,6 +46,7 @@ class TestRaises:
1/0 1/0
print (excinfo) print (excinfo)
assert excinfo.type == ZeroDivisionError assert excinfo.type == ZeroDivisionError
assert isinstance(excinfo.value, ZeroDivisionError)
def test_noraise(): def test_noraise():
with pytest.raises(pytest.raises.Exception): with pytest.raises(pytest.raises.Exception):