Skip to content

Commit

Permalink
Capture logs from all setup/call/teardown functions
Browse files Browse the repository at this point in the history
Just define three almost identical hooks for that:

    pytest_runtest_setup(item)
    pytest_runtest_call(item)
    pytest_runtest_teardown(item)

Also add tests for that.
  • Loading branch information
abusalimov committed Oct 1, 2015
1 parent e5131e6 commit 6e4fb4b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
39 changes: 26 additions & 13 deletions pytest_catchlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,32 @@ def __init__(self, config):
get_option_ini(config, 'log_format'),
get_option_ini(config, 'log_date_format'))

@pytest.mark.hookwrapper
def pytest_runtest_call(self, item):
with catching_logs(CatchLogHandler()) as log_handler:
item.catch_log_handler = log_handler
try:
yield # run test
finally:
del item.catch_log_handler

if self.print_logs:
# Add a captured log section to the report.
log = log_handler.stream.getvalue().strip()
item.add_report_section('call', 'log', log)
def _make_runtest_for(when):
"""Create a hook method for a given context (setup/call/teardown)."""

@pytest.mark.hookwrapper
def runtest_func(self, item):
"""Implements pytest_runtest_xxx() hook."""
with catching_logs(CatchLogHandler()) as log_handler:
item.catch_log_handler = log_handler
try:
yield # run test
finally:
del item.catch_log_handler

if self.print_logs:
# Add a captured log section to the report.
log = log_handler.stream.getvalue().strip()
item.add_report_section(when, 'log', log)

runtest_func.__name__ = 'pytest_runtest_' + when # just in case
return runtest_func

pytest_runtest_setup = _make_runtest_for('setup')
pytest_runtest_call = _make_runtest_for('call')
pytest_runtest_teardown = _make_runtest_for('teardown')

del _make_runtest_for


class CatchLogHandler(logging.StreamHandler):
Expand Down
44 changes: 44 additions & 0 deletions test_pytest_catchlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ def test_foo():
'text going to stderr'])


def test_setup_logging(testdir):
testdir.makepyfile('''
import sys
import logging
pytest_plugins = 'catchlog'
def setup_function(function):
logging.getLogger().info('text going to logger from setup')
def test_foo():
logging.getLogger().info('text going to logger from call')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log setup -*',
'*text going to logger from setup*',
'*- Captured *log call -*',
'*text going to logger from call*'])


def test_teardown_logging(testdir):
testdir.makepyfile('''
import sys
import logging
pytest_plugins = 'catchlog'
def test_foo():
logging.getLogger().info('text going to logger from call')
def teardown_function(function):
logging.getLogger().info('text going to logger from teardown')
assert False
''')
result = testdir.runpytest()
assert result.ret == 1
result.stdout.fnmatch_lines(['*- Captured *log call -*',
'*text going to logger from call*',
'*- Captured *log teardown -*',
'*text going to logger from teardown*'])


def test_change_level(testdir):
testdir.makepyfile('''
import sys
Expand Down

0 comments on commit 6e4fb4b

Please sign in to comment.