Skip to content

Instantly share code, notes, and snippets.

@hjwp
Last active August 29, 2015 14:23
Show Gist options
  • Save hjwp/ba172b27d8dec7565f7b to your computer and use it in GitHub Desktop.
Save hjwp/ba172b27d8dec7565f7b to your computer and use it in GitHub Desktop.
pytest print in finalizer 2
➜ t2 py.test tests.py
============================= test session starts =============================
platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2
rootdir: /tmp/t2, inifile:
collected 2 items
tests.py .F
================================== FAILURES ===================================
______________________________ test_should_fail _______________________________
print_after = None
def test_should_fail(print_after):
print('test, should fail ' + str(time.time()))
> assert False
E assert False
tests.py:9: AssertionError
---------------------------- Captured stdout call -----------------------------
test, should fail 1435575320.4597855
after test 1435575320.4598296
===================== 1 failed, 1 passed in 0.01 seconds ======================
import time
import pytest
def pytest_runtest_call(item):
try:
item.runtest()
finally:
if hasattr(item, "_request"):
if hasattr(item._request, "run_after"):
item._request.run_after()
@pytest.fixture
def print_after(request):
def print_message():
print ("after test " + str(time.time()))
request._parent_request.run_after = print_message
import time
def test_should_pass(print_after):
print('test, should pass ' + str(time.time()))
assert True
def test_should_fail(print_after):
print('test, should fail ' + str(time.time()))
assert False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment