Created
March 14, 2015 13:22
-
-
Save nicoddemus/8add4573dc62bce86980 to your computer and use it in GitHub Desktop.
Expect conftest with small modification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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