diff options
author | Andras Mitzki <andras.mitzki@balabit.com> | 2019-02-08 22:35:50 +0100 |
---|---|---|
committer | Andras Mitzki <andras.mitzki@balabit.com> | 2019-02-15 16:05:10 +0100 |
commit | e3824d23bcf1ee255d0720e393f2bd9a289b0853 (patch) | |
tree | f2b1b0f3b67ec60d0f40924a4a84eff224341187 /testing/logging | |
parent | a131cd6c3b4ff60be8631fa2b75bcc4f3238f492 (diff) | |
download | pytest-e3824d23bcf1ee255d0720e393f2bd9a289b0853.tar.gz |
LoggingPlugin: Expose setting log_file_handler
- This patch allows to set log_file (path) from hook
Signed-off-by: Thomas Hisch
Signed-off-by: Andras Mitzki <andras.mitzki@balabit.com>
Diffstat (limited to 'testing/logging')
-rw-r--r-- | testing/logging/test_reporting.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 9debc2165..afeccfcc5 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -1002,3 +1002,51 @@ def test_log_in_hooks(testdir): assert "sessionstart" in contents assert "runtestloop" in contents assert "sessionfinish" in contents + + +def test_log_set_path(testdir): + report_dir_base = testdir.tmpdir.strpath + + testdir.makeini( + """ + [pytest] + log_file_level = DEBUG + log_cli=true + """ + ) + testdir.makeconftest( + """ + import os + import pytest + @pytest.hookimpl(hookwrapper=True, tryfirst=True) + def pytest_runtest_setup(item): + config = item.config + logging_plugin = config.pluginmanager.get_plugin("logging-plugin") + report_file = os.path.join({}, item._request.node.name) + logging_plugin.set_log_path(report_file) + yield + """.format( + repr(report_dir_base) + ) + ) + testdir.makepyfile( + """ + import logging + logger = logging.getLogger("testcase-logger") + def test_first(): + logger.info("message from test 1") + assert True + + def test_second(): + logger.debug("message from test 2") + assert True + """ + ) + testdir.runpytest() + with open(os.path.join(report_dir_base, "test_first"), "r") as rfh: + content = rfh.read() + assert "message from test 1" in content + + with open(os.path.join(report_dir_base, "test_second"), "r") as rfh: + content = rfh.read() + assert "message from test 2" in content |