Skip to content

Instantly share code, notes, and snippets.

@nicoddemus
Created March 14, 2015 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoddemus/8add4573dc62bce86980 to your computer and use it in GitHub Desktop.
Save nicoddemus/8add4573dc62bce86980 to your computer and use it in GitHub Desktop.
Expect conftest with small modification
# original from: http://pythontesting.net/framework/pytest/pytest-expect-plugin-1
import pytest
import inspect
import os.path
@pytest.fixture
def expect(request):
def do_expect(expr, msg=''):
if not expr:
_log_failure(request.node, msg)
return do_expect
def _log_failure(node, msg=''):
# get filename, line, and context
(filename, line, funcname, contextlist) = inspect.stack()[2][1:5]
filename = os.path.basename(filename)
context = contextlist[0]
# format entry
msg = '%s\n' % msg if msg else ''
entry = '>%s%s%s:%s\n--------' % (context, msg, filename, line)
# add entry
if not hasattr(node, '_failed_expect'):
node._failed_expect = []
node._failed_expect.append(entry)
@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
report = __multicall__.execute()
if (call.when == "call") and hasattr(item, '_failed_expect'):
report.outcome = "failed"
summary = 'Failed Expectations:%s' % len(item._failed_expect)
item._failed_expect.append(summary)
report.longrepr = str(report.longrepr) + '\n' + ('\n'.join(item._failed_expect))
return report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment