From 2d690b83bf5c434a03bce4842e269418aa928141 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 23 Mar 2019 00:29:36 +0100 Subject: [PATCH] ExceptionInfo.from_current: assert current exception --- src/_pytest/_code/code.py | 1 + testing/code/test_code.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 1105310cf..79845db73 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -413,6 +413,7 @@ class ExceptionInfo(object): to the exception message/``__str__()`` """ tup = sys.exc_info() + assert tup[0] is not None, "no current exception" _striptext = "" if exprinfo is None and isinstance(tup[1], AssertionError): exprinfo = getattr(tup[1], "msg", None) diff --git a/testing/code/test_code.py b/testing/code/test_code.py index 3362d4604..81a87481c 100644 --- a/testing/code/test_code.py +++ b/testing/code/test_code.py @@ -172,6 +172,10 @@ class TestExceptionInfo(object): exci = _pytest._code.ExceptionInfo.from_current() assert exci.getrepr() + def test_from_current_with_missing(self): + with pytest.raises(AssertionError, match="no current exception"): + _pytest._code.ExceptionInfo.from_current() + class TestTracebackEntry(object): def test_getsource(self):