Skip to content

added exception object to the TestReport #3361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions _pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def pytest_runtest_makereport(item, call):
duration = call.stop - call.start
keywords = dict([(x, 1) for x in item.keywords])
excinfo = call.excinfo
exception = None
sections = []
if not call.excinfo:
outcome = "passed"
Expand All @@ -308,6 +309,7 @@ def pytest_runtest_makereport(item, call):
longrepr = (str(r.path), r.lineno, r.message)
else:
outcome = "failed"
exception = excinfo.value
if call.when == "call":
longrepr = item.repr_failure(excinfo)
else: # exception in setup or teardown
Expand All @@ -317,7 +319,7 @@ def pytest_runtest_makereport(item, call):
sections.append(("Captured %s %s" % (key, rwhen), content))
return TestReport(item.nodeid, item.location,
keywords, outcome, longrepr, when,
sections, duration, user_properties=item.user_properties)
sections, duration, exception=exception, user_properties=item.user_properties)


class TestReport(BaseReport):
Expand All @@ -326,7 +328,7 @@ class TestReport(BaseReport):
"""

def __init__(self, nodeid, location, keywords, outcome,
longrepr, when, sections=(), duration=0, user_properties=(), **extra):
longrepr, when, sections=(), duration=0, user_properties=(), exception=None, **extra):
#: normalized collection node id
self.nodeid = nodeid

Expand All @@ -345,6 +347,9 @@ def __init__(self, nodeid, location, keywords, outcome,
#: None or a failure representation.
self.longrepr = longrepr

#: None or the exception which caused the failure
self.exception = exception

#: one of 'setup', 'call', 'teardown' to indicate runtest phase.
self.when = when

Expand Down
1 change: 1 addition & 0 deletions changelog/3343.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the exception object to the ``TestReport``, for reference in hook functions.
58 changes: 58 additions & 0 deletions testing/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,3 +825,61 @@ def test_func():
rep = reports[1]
assert rep.capstdout == ''
assert rep.capstderr == ''

def test_exception(self, testdir):
reports = testdir.runitem("""
def test_func():
x = 1
assert x == 4
""")
rep = reports[1]
assert rep.failed
assert rep.when == "call"
assert isinstance(rep.exception, AssertionError)
assert str(rep.exception) == 'assert 1 == 4'

def test_setup_exception(self, testdir):
reports = testdir.runitem("""
import pytest

@pytest.fixture
def fix():
assert False
yield

def test_func(fix):
pass
""")
rep = reports[0]
assert rep.failed
assert rep.when == "setup"
assert isinstance(rep.exception, AssertionError)
assert str(rep.exception) == 'assert False'

def test_teardown_exception(self, testdir):
reports = testdir.runitem("""
import pytest

@pytest.fixture
def fix():
yield
assert False

def test_func(fix):
pass
""")
rep = reports[2]
assert rep.failed
assert rep.when == "teardown"
assert isinstance(rep.exception, AssertionError)
assert str(rep.exception) == 'assert False'

def test_no_exception(self, testdir):
reports = testdir.runitem("""
def test_func():
x = 1
assert x == 1
""")
rep = reports[1]
assert not rep.failed
assert rep.exception is None