Skip to content

Instantly share code, notes, and snippets.

@shauns
Created September 4, 2014 10:18
Show Gist options
  • Save shauns/a5728deeb411a51d0f19 to your computer and use it in GitHub Desktop.
Save shauns/a5728deeb411a51d0f19 to your computer and use it in GitHub Desktop.
Tracking pytest errors
import pytest
def track_test_fails():
@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
# execute all other hooks to obtain the report object
rep = __multicall__.execute()
# `when` is setup, call, teardown
setattr(item, "rep_" + rep.when, rep)
# mark failures explicitly
if hasattr(item, 'rep_setup') and hasattr(item, 'rep_call'):
item.test_failed = (
item.rep_setup.passed and item.rep_call.failed
)
return rep
return pytest_runtest_makereport
pytest_runtest_makereport = track_test_fails()
@pytest.yield_fixture
def foo(request):
if pytest.config.option.skip_foo:
pytest.skip('Foo tests are skipped')
if not platform.config.get('foo_uri'):
pytest.fail(
'Please configure foo uri or skip with ...'
)
yield
if request.node.test_failed:
pytest.fail(
"Maybe your test failed because X, Y, Z or you can skip with ..."
)
def test_foo(foo):
assert False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment