MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_root_logger_affected

Function test_root_logger_affected

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected