MCPcopy
hub / github.com/pytest-dev/pytest / test_root_logger_affected

Function test_root_logger_affected

testing/logging/test_reporting.py:58–91  ·  view source on GitHub ↗
(pytester: Pytester)

Source from the content-addressed store, hash-verified

56
57
58def test_root_logger_affected(pytester: Pytester) -> None:
59 pytester.makepyfile(
60 """
61 import logging
62 logger = logging.getLogger()
63
64 def test_foo():
65 logger.info('info text ' + 'going to logger')
66 logger.warning('warning text ' + 'going to logger')
67 logger.error('error text ' + 'going to logger')
68
69 assert 0
70 """
71 )
72 log_file = str(pytester.path.joinpath("pytest.log"))
73 result = pytester.runpytest("--log-level=ERROR", "--log-file=pytest.log")
74 assert result.ret == 1
75
76 # The capture log calls in the stdout section only contain the
77 # logger.error msg, because of --log-level=ERROR.
78 result.stdout.fnmatch_lines(["*error text going to logger*"])
79 stdout = result.stdout.str()
80 assert "warning text going to logger" not in stdout
81 assert "info text going to logger" not in stdout
82
83 # The log file should only contain the error log messages and
84 # not the warning or info ones, because the root logger is set to
85 # ERROR using --log-level=ERROR.
86 assert os.path.isfile(log_file)
87 with open(log_file, encoding="utf-8") as rfh:
88 contents = rfh.read()
89 assert "info text going to logger" not in contents
90 assert "warning text going to logger" not in contents
91 assert "error text going to logger" in contents
92
93
94def test_log_cli_level_log_level_interaction(pytester: Pytester) -> None:

Callers

nothing calls this directly

Calls 6

fnmatch_linesMethod · 0.80
strMethod · 0.80
makepyfileMethod · 0.45
runpytestMethod · 0.45
isfileMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected