Merge pull request #4204 from vbarbaresi/3340_log_hooks
Fix logging usage in hooks pytest_sessionstart/finish
This commit is contained in:
commit
b9990f77ff
|
@ -0,0 +1 @@
|
||||||
|
Fix logging messages not shown in hooks ``pytest_sessionstart()`` and ``pytest_sessionfinish()``.
|
|
@ -497,6 +497,29 @@ class LoggingPlugin(object):
|
||||||
with self._runtest_for(None, "finish"):
|
with self._runtest_for(None, "finish"):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
||||||
|
def pytest_sessionfinish(self):
|
||||||
|
with self.live_logs_context():
|
||||||
|
if self.log_cli_handler:
|
||||||
|
self.log_cli_handler.set_when("sessionfinish")
|
||||||
|
if self.log_file_handler is not None:
|
||||||
|
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
||||||
|
yield
|
||||||
|
else:
|
||||||
|
yield
|
||||||
|
|
||||||
|
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
||||||
|
def pytest_sessionstart(self):
|
||||||
|
self._setup_cli_logging()
|
||||||
|
with self.live_logs_context():
|
||||||
|
if self.log_cli_handler:
|
||||||
|
self.log_cli_handler.set_when("sessionstart")
|
||||||
|
if self.log_file_handler is not None:
|
||||||
|
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
||||||
|
yield
|
||||||
|
else:
|
||||||
|
yield
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
def pytest_runtestloop(self, session):
|
def pytest_runtestloop(self, session):
|
||||||
"""Runs all collected test items."""
|
"""Runs all collected test items."""
|
||||||
|
|
|
@ -966,3 +966,39 @@ def test_collection_logging_to_file(testdir):
|
||||||
assert "Normal message" in contents
|
assert "Normal message" in contents
|
||||||
assert "debug message in test_simple" not in contents
|
assert "debug message in test_simple" not in contents
|
||||||
assert "info message in test_simple" in contents
|
assert "info message in test_simple" in contents
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_in_hooks(testdir):
|
||||||
|
log_file = testdir.tmpdir.join("pytest.log").strpath
|
||||||
|
|
||||||
|
testdir.makeini(
|
||||||
|
"""
|
||||||
|
[pytest]
|
||||||
|
log_file={}
|
||||||
|
log_file_level = INFO
|
||||||
|
log_cli=true
|
||||||
|
""".format(
|
||||||
|
log_file
|
||||||
|
)
|
||||||
|
)
|
||||||
|
testdir.makeconftest(
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def pytest_runtestloop(session):
|
||||||
|
logging.info('runtestloop')
|
||||||
|
|
||||||
|
def pytest_sessionstart(session):
|
||||||
|
logging.info('sessionstart')
|
||||||
|
|
||||||
|
def pytest_sessionfinish(session, exitstatus):
|
||||||
|
logging.info('sessionfinish')
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
result = testdir.runpytest()
|
||||||
|
result.stdout.fnmatch_lines(["*sessionstart*", "*runtestloop*", "*sessionfinish*"])
|
||||||
|
with open(log_file) as rfh:
|
||||||
|
contents = rfh.read()
|
||||||
|
assert "sessionstart" in contents
|
||||||
|
assert "runtestloop" in contents
|
||||||
|
assert "sessionfinish" in contents
|
||||||
|
|
Loading…
Reference in New Issue