diff options
author | Jordan Moldow <jmoldow@box.com> | 2017-07-29 02:39:17 -0700 |
---|---|---|
committer | Jordan Moldow <jmoldow@box.com> | 2017-07-29 02:39:17 -0700 |
commit | 2e61f702c09899bf375dba0c59eec3760fdd8990 (patch) | |
tree | 0283221fc26d911345186aa423ff67a510a8ecb5 /testing/code | |
parent | 768edde899fe3629a69d55289f82bb0d95635c06 (diff) | |
download | pytest-2e61f702c09899bf375dba0c59eec3760fdd8990.tar.gz |
Support PEP-415's Exception.__suppress_context__
PEP-415 states that `exception.__context__` should be suppressed
in traceback outputs, if `exception.__suppress_context__` is
`True`.
Now if a ``raise exception from None`` is caught by pytest,
pytest will no longer chain the context in the test report.
The algorithm in `FormattedExcinfo` now better matches the one
in `traceback.TracebackException`.
`Exception.__suppress_context__` is available in all of the
versions of Python 3 that are supported by pytest.
Fixes #2631.
Diffstat (limited to 'testing/code')
-rw-r--r-- | testing/code/test_excinfo.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 37ceeb423..f8f8a0365 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -1096,6 +1096,36 @@ raise ValueError() assert tw.lines[47] == ":15: AttributeError" @pytest.mark.skipif("sys.version_info[0] < 3") + def test_exc_repr_with_raise_from_none_chain_suppression(self, importasmod): + mod = importasmod(""" + def f(): + try: + g() + except Exception: + raise AttributeError() from None + def g(): + raise ValueError() + """) + excinfo = pytest.raises(AttributeError, mod.f) + r = excinfo.getrepr(style="long") + tw = TWMock() + r.toterminal(tw) + for line in tw.lines: + print(line) + assert tw.lines[0] == "" + assert tw.lines[1] == " def f():" + assert tw.lines[2] == " try:" + assert tw.lines[3] == " g()" + assert tw.lines[4] == " except Exception:" + assert tw.lines[5] == "> raise AttributeError() from None" + assert tw.lines[6] == "E AttributeError" + assert tw.lines[7] == "" + line = tw.get_write_msg(8) + assert line.endswith('mod.py') + assert tw.lines[9] == ":6: AttributeError" + assert len(tw.lines) == 10 + + @pytest.mark.skipif("sys.version_info[0] < 3") @pytest.mark.parametrize('reason, description', [ ('cause', 'The above exception was the direct cause of the following exception:'), ('context', 'During handling of the above exception, another exception occurred:'), |