Created
June 30, 2015 15:41
-
-
Save hjwp/4294f0acbef5345a7d46 to your computer and use it in GitHub Desktop.
pytest: print fixture output after test runs
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
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 |
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
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 |
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
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