Skip to content

Instantly share code, notes, and snippets.

@hjwp
Last active August 29, 2015 14:23
Show Gist options
  • Save hjwp/6e221dad25ea8c7430ff to your computer and use it in GitHub Desktop.
Save hjwp/6e221dad25ea8c7430ff to your computer and use it in GitHub Desktop.
pytest print in finalizer attempts
import time
import pytest
def pytest_runtest_call(item):
if hasattr(item, "_request"):
if hasattr(item._request, "_addoutput_on_failure"):
item._request._addoutput_on_failure()
@pytest.fixture
def print_on_fail(request):
def add():
print ("test failed" + str(time.time()))
request._parent_request._addoutput_on_failure = add
import time
def test_doing_stuff(print_on_fail):
print('testing' + str(time.time()))
assert False
============================= test session starts =============================
platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2
rootdir: /tmp/t2, inifile:
collected 1 items
tests.py F
================================== FAILURES ===================================
______________________________ test_doing_stuff _______________________________
print_on_fail = None
def test_doing_stuff(print_on_fail):
print('testing' + str(time.time()))
> assert False
E assert False
tests.py:5: AssertionError
---------------------------- Captured stdout call -----------------------------
test failed1435573940.9372423
testing1435573940.9373307
========================== 1 failed in 0.01 seconds ===========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment