From 6e4fb4ba50c12b98f463f5ef82b441017ac746ed Mon Sep 17 00:00:00 2001 From: Eldar Abusalimov Date: Sun, 27 Sep 2015 18:52:53 +0300 Subject: [PATCH] Capture logs from all setup/call/teardown functions 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. --- pytest_catchlog.py | 39 ++++++++++++++++++++++++------------ test_pytest_catchlog.py | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/pytest_catchlog.py b/pytest_catchlog.py index 4f2cd92..d8fa8ec 100644 --- a/pytest_catchlog.py +++ b/pytest_catchlog.py @@ -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): diff --git a/test_pytest_catchlog.py b/test_pytest_catchlog.py index 3152bb7..d5b7cfd 100644 --- a/test_pytest_catchlog.py +++ b/test_pytest_catchlog.py @@ -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