Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4810.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Logging messages inside ``pytest_runtest_logreport()`` are now properly captured and displayed.
5 changes: 5 additions & 0 deletions src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ def pytest_runtest_logfinish(self):
with self._runtest_for(None, "finish"):
yield

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_logreport(self):
with self._runtest_for(None, "logreport"):
yield

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_sessionfinish(self):
with self.live_logs_context():
Expand Down
34 changes: 34 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,40 @@ def pytest_sessionfinish(session, exitstatus):
assert "sessionfinish" in contents


def test_log_in_runtest_logreport(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
logger = logging.getLogger(__name__)

def pytest_runtest_logreport(report):
logger.info("logreport")
"""
)
testdir.makepyfile(
"""
def test_first():
assert True
"""
)
testdir.runpytest()
with open(log_file) as rfh:
contents = rfh.read()
assert contents.count("logreport") == 3


def test_log_set_path(testdir):
report_dir_base = testdir.tmpdir.strpath

Expand Down