Skip to content

Instantly share code, notes, and snippets.

@hjwp
Created June 30, 2015 15:41
Show Gist options
  • Save hjwp/4294f0acbef5345a7d46 to your computer and use it in GitHub Desktop.
Save hjwp/4294f0acbef5345a7d46 to your computer and use it in GitHub Desktop.
pytest: print fixture output after test runs
import pytest
@pytest.mark.hookwrapper
def pytest_pyfunc_call(pyfuncitem):
yield
print('pyfunc call after')
if 'watch_logs' in pyfuncitem.funcargs:
print(pyfuncitem.funcargs['watch_logs']())
@pytest.fixture
def watch_logs(request):
with open('/tmp/mylogs.txt') as f:
log_before = f.read()
def get_new_logs():
with open('/tmp/mylogs.txt') as f:
log_after = f.read()
return log_after.replace(log_before, '')
return get_new_logs
from thing import do_stuff
def test_doing_stuff_passes(watch_logs):
print('in test, should pass')
assert do_stuff() == 42
def test_doing_stuff_fails(watch_logs):
print('in test, should fail')
assert do_stuff() == 43
import time
def do_stuff():
with open('/tmp/mylogs.txt', 'a') as f:
f.write('ran at {}\n'.format(time.time()))
return 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment