config: logging: add suppress-logger option tests

This commit is contained in:
Itxaso Aizpurua 2022-10-11 22:16:45 +02:00
parent abb25863ab
commit 6c7e41b3e2
2 changed files with 12 additions and 17 deletions

View File

@ -302,7 +302,7 @@ def pytest_addoption(parser: Parser) -> None:
action="append", action="append",
default=[], default=[],
dest="suppress_logger", dest="suppress_logger",
help="Suppress logger by name", help="Suppress loggers by name",
) )
@ -611,7 +611,6 @@ class LoggingPlugin:
logger = logging.getLogger(name) logger = logging.getLogger(name)
logger.addFilter(lambda _: 0) logger.addFilter(lambda _: 0)
def _create_formatter(self, log_format, log_date_format, auto_indent): def _create_formatter(self, log_format, log_date_format, auto_indent):
# Color option doesn't exist if terminal plugin is disabled. # Color option doesn't exist if terminal plugin is disabled.
color = getattr(self._config.option, "color", "no") color = getattr(self._config.option, "color", "no")

View File

@ -1173,20 +1173,16 @@ def test_suppress_loggers(testdir):
import logging import logging
import os import os
suppressed_log = logging.getLogger('suppressed') suppressed_log = logging.getLogger('suppressed')
other_log = logging.getLogger('other') test_log = logging.getLogger('test')
normal_log = logging.getLogger('normal')
def test_logger_propagation(caplog): def test_logger_propagation(caplog):
with caplog.at_level(logging.DEBUG): with caplog.at_level(logging.DEBUG):
suppressed_log.warning("no log; no stderr") suppressed_log.warning("no log; no stderr")
other_log.info("no log") test_log.debug("Visible text!")
normal_log.debug("Unsuppressed!")
print(os.linesep) print(os.linesep)
assert caplog.record_tuples == [('normal', 10, 'Unsuppressed!')] assert caplog.record_tuples == [('test', 10, 'Visible text!')]
""" """
) )
result = testdir.runpytest( result = testdir.runpytest("--suppress-logger=suppressed", "-s")
"--suppress-logger=suppressed", "--suppress-logger=other", "-s"
)
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
assert not result.stderr.lines assert not result.stderr.lines
@ -1202,23 +1198,23 @@ def test_log_suppressing_works_with_log_cli(testdir):
testdir.makepyfile( testdir.makepyfile(
""" """
import logging import logging
log = logging.getLogger('mylog')
suppressed_log = logging.getLogger('suppressed') suppressed_log = logging.getLogger('suppressed')
test_log = logging.getLogger('test')
def test_log_cli_works(caplog): def test_log_cli_works(caplog):
log.info("hello world") test_log.info("Visible text!")
suppressed_log.warning("Hello World") suppressed_log.warning("This string will be suppressed.")
""" """
) )
result = testdir.runpytest( result = testdir.runpytest(
"--log-cli-level=DEBUG", "--log-cli-level=DEBUG",
"--suppress-logger=suppressed", "--suppress-logger=suppressed",
"--suppress-logger=mylog",
) )
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
result.stdout.no_fnmatch_line( result.stdout.fnmatch_lines(
"INFO mylog:test_log_suppressing_works_with_log_cli.py:5 hello world" "INFO test:test_log_suppressing_works_with_log_cli.py:6 Visible text!"
) )
result.stdout.no_fnmatch_line( result.stdout.no_fnmatch_line(
"WARNING suppressed:test_log_suppressing_works_with_log_cli.py:6 Hello World" "WARNING suppressed:test_log_suppressing_works_with_log_cli.py:7 This string will be suppressed."
) )
assert not result.stderr.lines assert not result.stderr.lines