Skip to content

Instantly share code, notes, and snippets.

@rvprasad
Last active May 16, 2016 04:53
Show Gist options
  • Save rvprasad/0155e03bc791a74d1037b63e7eb76e15 to your computer and use it in GitHub Desktop.
Save rvprasad/0155e03bc791a74d1037b63e7eb76e15 to your computer and use it in GitHub Desktop.
In test cases, this adaptation of Pytest hook implementation reports uncaught AssertionError exceptions as failures and all other uncaught exceptions as errors.
import pytest # added
from _pytest import runner, _code # added
def pytest_runtest_makereport(item, call):
when = call.when
duration = call.stop-call.start
keywords = dict([(x,1) for x in item.keywords])
excinfo = call.excinfo
sections = []
if not call.excinfo:
outcome = "passed"
longrepr = None
else:
if not isinstance(excinfo, _code.ExceptionInfo):
outcome = "failed"
longrepr = excinfo
elif excinfo.errisinstance(pytest.skip.Exception):
outcome = "skipped"
r = excinfo._getreprcrash()
longrepr = (str(r.path), r.lineno, r.message)
else:
outcome = "failed"
if call.when == "call" and \
isinstance(excinfo.value, AssertionError): #added
longrepr = item.repr_failure(excinfo)
else: # exception in setup or teardown
when = 'setup' if when == 'call' else when #added
longrepr = item._repr_failure_py(excinfo,
style=item.config.option.tbstyle)
for rwhen, key, content in item._report_sections:
sections.append(("Captured %s %s" %(key, rwhen), content))
return runner.TestReport(item.nodeid, item.location,
keywords, outcome, longrepr, when,
sections, duration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment