Skip to content

Commit eb73db5

Browse files
committed
Fix issue where pytest.raises() doesn't always return Exception instance in py26
Fixes #767
1 parent 0431b8b commit eb73db5

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
2.7.2 (compared to 2.7.1)
22
-----------------------------
33

4+
- fix issue767: pytest.raises value attribute does not contain the exception
5+
instance on Python 2.6. Thanks Eric Siegerman for providing the test
6+
case and Bruno Oliveira for PR.
7+
48
- Automatically create directory for junitxml and results log.
59
Thanks Aron Curzon.
610

_pytest/python.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,13 @@ def __exit__(self, *tp):
10991099
__tracebackhide__ = True
11001100
if tp[0] is None:
11011101
pytest.fail("DID NOT RAISE")
1102+
if sys.version_info < (2, 7):
1103+
# py26: on __exit__() exc_value often does not contain the
1104+
# exception value.
1105+
# http://bugs.python.org/issue7853
1106+
if not isinstance(tp[1], BaseException):
1107+
exc_type, value, traceback = tp
1108+
tp = exc_type, exc_type(value), traceback
11021109
self.excinfo.__init__(tp)
11031110
return issubclass(self.excinfo.type, self.ExpectedException)
11041111

testing/python/raises.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_simple():
4646
1/0
4747
print (excinfo)
4848
assert excinfo.type == ZeroDivisionError
49+
assert isinstance(excinfo.value, ZeroDivisionError)
4950
5051
def test_noraise():
5152
with pytest.raises(pytest.raises.Exception):

0 commit comments

Comments
 (0)