Skip to content

Commit 5b35241

Browse files
authored
Merge pull request #4812 from mitzkia/logging_from_runtest_logreport
Logging: Make pytest_runtest_logreport() hook available for logging
2 parents da30596 + b26b731 commit 5b35241

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

changelog/4810.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Logging messages inside ``pytest_runtest_logreport()`` are now properly captured and displayed.

src/_pytest/logging.py

+5
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ def pytest_runtest_logfinish(self):
567567
with self._runtest_for(None, "finish"):
568568
yield
569569

570+
@pytest.hookimpl(hookwrapper=True)
571+
def pytest_runtest_logreport(self):
572+
with self._runtest_for(None, "logreport"):
573+
yield
574+
570575
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
571576
def pytest_sessionfinish(self):
572577
with self.live_logs_context():

testing/logging/test_reporting.py

+34
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,40 @@ def pytest_sessionfinish(session, exitstatus):
10041004
assert "sessionfinish" in contents
10051005

10061006

1007+
def test_log_in_runtest_logreport(testdir):
1008+
log_file = testdir.tmpdir.join("pytest.log").strpath
1009+
1010+
testdir.makeini(
1011+
"""
1012+
[pytest]
1013+
log_file={}
1014+
log_file_level = INFO
1015+
log_cli=true
1016+
""".format(
1017+
log_file
1018+
)
1019+
)
1020+
testdir.makeconftest(
1021+
"""
1022+
import logging
1023+
logger = logging.getLogger(__name__)
1024+
1025+
def pytest_runtest_logreport(report):
1026+
logger.info("logreport")
1027+
"""
1028+
)
1029+
testdir.makepyfile(
1030+
"""
1031+
def test_first():
1032+
assert True
1033+
"""
1034+
)
1035+
testdir.runpytest()
1036+
with open(log_file) as rfh:
1037+
contents = rfh.read()
1038+
assert contents.count("logreport") == 3
1039+
1040+
10071041
def test_log_set_path(testdir):
10081042
report_dir_base = testdir.tmpdir.strpath
10091043

0 commit comments

Comments
 (0)