2023-05-18 21:18:59 +08:00
|
|
|
# mypy: disable-error-code="attr-defined"
|
2023-10-22 03:02:54 +08:00
|
|
|
# mypy: disallow-untyped-defs
|
2017-09-13 19:12:07 +08:00
|
|
|
import logging
|
2023-10-22 02:35:07 +08:00
|
|
|
from typing import Iterator
|
2017-09-13 19:12:07 +08:00
|
|
|
|
2020-04-30 16:30:43 +08:00
|
|
|
from _pytest.logging import caplog_records_key
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2018-01-15 22:01:01 +08:00
|
|
|
import pytest
|
2024-02-01 04:12:33 +08:00
|
|
|
|
2017-09-13 19:12:07 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2017-10-25 07:01:00 +08:00
|
|
|
sublogger = logging.getLogger(__name__ + ".baz")
|
2017-09-13 19:12:07 +08:00
|
|
|
|
|
|
|
|
2023-10-22 02:35:07 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def cleanup_disabled_logging() -> Iterator[None]:
|
2023-05-18 21:18:59 +08:00
|
|
|
"""Simple fixture that ensures that a test doesn't disable logging.
|
|
|
|
|
|
|
|
This is necessary because ``logging.disable()`` is global, so a test disabling logging
|
|
|
|
and not cleaning up after will break every test that runs after it.
|
|
|
|
|
|
|
|
This behavior was moved to a fixture so that logging will be un-disabled even if the test fails an assertion.
|
|
|
|
"""
|
|
|
|
yield
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_fixture_help(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--fixtures")
|
2017-09-13 19:12:07 +08:00
|
|
|
result.stdout.fnmatch_lines(["*caplog*"])
|
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_change_level(caplog: pytest.LogCaptureFixture) -> None:
|
2017-09-13 19:12:07 +08:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
logger.debug("handler DEBUG level")
|
|
|
|
logger.info("handler INFO level")
|
|
|
|
|
|
|
|
caplog.set_level(logging.CRITICAL, logger=sublogger.name)
|
|
|
|
sublogger.warning("logger WARNING level")
|
|
|
|
sublogger.critical("logger CRITICAL level")
|
|
|
|
|
|
|
|
assert "DEBUG" not in caplog.text
|
|
|
|
assert "INFO" in caplog.text
|
|
|
|
assert "WARNING" not in caplog.text
|
|
|
|
assert "CRITICAL" in caplog.text
|
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_change_level_logging_disabled(caplog: pytest.LogCaptureFixture) -> None:
|
2023-05-18 21:18:59 +08:00
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
assert logging.root.manager.disable == logging.CRITICAL
|
|
|
|
caplog.set_level(logging.WARNING)
|
|
|
|
logger.info("handler INFO level")
|
|
|
|
logger.warning("handler WARNING level")
|
|
|
|
|
|
|
|
caplog.set_level(logging.CRITICAL, logger=sublogger.name)
|
|
|
|
sublogger.warning("logger SUB_WARNING level")
|
|
|
|
sublogger.critical("logger SUB_CRITICAL level")
|
|
|
|
|
|
|
|
assert "INFO" not in caplog.text
|
|
|
|
assert "WARNING" in caplog.text
|
|
|
|
assert "SUB_WARNING" not in caplog.text
|
|
|
|
assert "SUB_CRITICAL" in caplog.text
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_change_level_undo(pytester: Pytester) -> None:
|
2020-07-29 19:46:11 +08:00
|
|
|
"""Ensure that 'set_level' is undone after the end of the test.
|
|
|
|
|
2023-05-18 21:18:59 +08:00
|
|
|
Tests the logging output themselves (affected both by logger and handler levels).
|
2020-07-29 19:46:11 +08:00
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-01-17 06:32:59 +08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
def test1(caplog):
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
# using + operator here so fnmatch_lines doesn't match the code in the traceback
|
|
|
|
logging.info('log from ' + 'test1')
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test2(caplog):
|
|
|
|
# using + operator here so fnmatch_lines doesn't match the code in the traceback
|
|
|
|
logging.info('log from ' + 'test2')
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-01-17 06:32:59 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2018-01-17 06:32:59 +08:00
|
|
|
result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*log from test2*")
|
2018-01-17 06:32:59 +08:00
|
|
|
|
|
|
|
|
2023-10-22 02:35:07 +08:00
|
|
|
def test_change_disabled_level_undo(pytester: Pytester) -> None:
|
2023-05-18 21:18:59 +08:00
|
|
|
"""Ensure that '_force_enable_logging' in 'set_level' is undone after the end of the test.
|
|
|
|
|
|
|
|
Tests the logging output themselves (affected by disabled logging level).
|
|
|
|
"""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
def test1(caplog):
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
# using + operator here so fnmatch_lines doesn't match the code in the traceback
|
|
|
|
logging.info('log from ' + 'test1')
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test2(caplog):
|
|
|
|
# using + operator here so fnmatch_lines doesn't match the code in the traceback
|
|
|
|
# use logging.warning because we need a level that will show up if logging.disabled
|
|
|
|
# isn't reset to ``CRITICAL`` after test1.
|
|
|
|
logging.warning('log from ' + 'test2')
|
|
|
|
assert 0
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"])
|
|
|
|
result.stdout.no_fnmatch_line("*log from test2*")
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_change_level_undos_handler_level(pytester: Pytester) -> None:
|
2020-07-29 19:46:11 +08:00
|
|
|
"""Ensure that 'set_level' is undone after the end of the test (handler).
|
|
|
|
|
|
|
|
Issue #7569. Tests the handler level specifically.
|
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-07-29 19:46:11 +08:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
def test1(caplog):
|
|
|
|
assert caplog.handler.level == 0
|
2020-08-22 22:23:26 +08:00
|
|
|
caplog.set_level(9999)
|
2020-07-29 19:46:11 +08:00
|
|
|
caplog.set_level(41)
|
|
|
|
assert caplog.handler.level == 41
|
|
|
|
|
|
|
|
def test2(caplog):
|
|
|
|
assert caplog.handler.level == 0
|
|
|
|
|
|
|
|
def test3(caplog):
|
|
|
|
assert caplog.handler.level == 0
|
|
|
|
caplog.set_level(43)
|
|
|
|
assert caplog.handler.level == 43
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-07-29 19:46:11 +08:00
|
|
|
result.assert_outcomes(passed=3)
|
|
|
|
|
|
|
|
|
2023-11-24 20:38:34 +08:00
|
|
|
def test_with_statement_at_level(caplog: pytest.LogCaptureFixture) -> None:
|
2017-09-13 19:12:07 +08:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
logger.debug("handler DEBUG level")
|
|
|
|
logger.info("handler INFO level")
|
|
|
|
|
|
|
|
with caplog.at_level(logging.CRITICAL, logger=sublogger.name):
|
|
|
|
sublogger.warning("logger WARNING level")
|
|
|
|
sublogger.critical("logger CRITICAL level")
|
|
|
|
|
|
|
|
assert "DEBUG" not in caplog.text
|
|
|
|
assert "INFO" in caplog.text
|
|
|
|
assert "WARNING" not in caplog.text
|
|
|
|
assert "CRITICAL" in caplog.text
|
|
|
|
|
|
|
|
|
2023-11-24 20:38:34 +08:00
|
|
|
def test_with_statement_at_level_logging_disabled(
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
) -> None:
|
2023-05-18 21:18:59 +08:00
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
assert logging.root.manager.disable == logging.CRITICAL
|
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
|
logger.debug("handler DEBUG level")
|
|
|
|
logger.info("handler INFO level")
|
|
|
|
logger.warning("handler WARNING level")
|
|
|
|
logger.error("handler ERROR level")
|
|
|
|
logger.critical("handler CRITICAL level")
|
|
|
|
|
|
|
|
assert logging.root.manager.disable == logging.INFO
|
|
|
|
|
|
|
|
with caplog.at_level(logging.CRITICAL, logger=sublogger.name):
|
|
|
|
sublogger.warning("logger SUB_WARNING level")
|
|
|
|
sublogger.critical("logger SUB_CRITICAL level")
|
|
|
|
|
|
|
|
assert "DEBUG" not in caplog.text
|
|
|
|
assert "INFO" not in caplog.text
|
|
|
|
assert "WARNING" in caplog.text
|
|
|
|
assert "ERROR" in caplog.text
|
|
|
|
assert " CRITICAL" in caplog.text
|
|
|
|
assert "SUB_WARNING" not in caplog.text
|
|
|
|
assert "SUB_CRITICAL" in caplog.text
|
|
|
|
assert logging.root.manager.disable == logging.CRITICAL
|
|
|
|
|
|
|
|
|
2023-11-24 20:38:34 +08:00
|
|
|
def test_with_statement_filtering(caplog: pytest.LogCaptureFixture) -> None:
|
|
|
|
class TestFilter(logging.Filter):
|
|
|
|
def filter(self, record: logging.LogRecord) -> bool:
|
|
|
|
record.msg = "filtered handler call"
|
|
|
|
return True
|
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
with caplog.filtering(TestFilter()):
|
|
|
|
logger.info("handler call")
|
|
|
|
logger.info("handler call")
|
|
|
|
|
|
|
|
filtered_tuple, unfiltered_tuple = caplog.record_tuples
|
|
|
|
assert filtered_tuple == ("test_fixture", 20, "filtered handler call")
|
|
|
|
assert unfiltered_tuple == ("test_fixture", 20, "handler call")
|
|
|
|
|
|
|
|
|
2023-05-18 21:18:59 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"level_str,expected_disable_level",
|
|
|
|
[
|
|
|
|
("CRITICAL", logging.ERROR),
|
|
|
|
("ERROR", logging.WARNING),
|
|
|
|
("WARNING", logging.INFO),
|
|
|
|
("INFO", logging.DEBUG),
|
|
|
|
("DEBUG", logging.NOTSET),
|
|
|
|
("NOTSET", logging.NOTSET),
|
|
|
|
("NOTVALIDLEVEL", logging.NOTSET),
|
|
|
|
],
|
|
|
|
)
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_force_enable_logging_level_string(
|
|
|
|
caplog: pytest.LogCaptureFixture, level_str: str, expected_disable_level: int
|
|
|
|
) -> None:
|
2023-05-18 21:18:59 +08:00
|
|
|
"""Test _force_enable_logging using a level string.
|
|
|
|
|
|
|
|
``expected_disable_level`` is one level below ``level_str`` because the disabled log level
|
|
|
|
always needs to be *at least* one level lower than the level that caplog is trying to capture.
|
|
|
|
"""
|
|
|
|
test_logger = logging.getLogger("test_str_level_force_enable")
|
|
|
|
# Emulate a testing environment where all logging is disabled.
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
# Make sure all logging is disabled.
|
|
|
|
assert not test_logger.isEnabledFor(logging.CRITICAL)
|
|
|
|
# Un-disable logging for `level_str`.
|
|
|
|
caplog._force_enable_logging(level_str, test_logger)
|
|
|
|
# Make sure that the disabled level is now one below the requested logging level.
|
|
|
|
# We don't use `isEnabledFor` here because that also checks the level set by
|
|
|
|
# `logging.setLevel()` which is irrelevant to `logging.disable()`.
|
|
|
|
assert test_logger.manager.disable == expected_disable_level
|
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_log_access(caplog: pytest.LogCaptureFixture) -> None:
|
2018-01-17 06:32:59 +08:00
|
|
|
caplog.set_level(logging.INFO)
|
2017-09-13 19:12:07 +08:00
|
|
|
logger.info("boo %s", "arg")
|
|
|
|
assert caplog.records[0].levelname == "INFO"
|
|
|
|
assert caplog.records[0].msg == "boo %s"
|
|
|
|
assert "boo arg" in caplog.text
|
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_messages(caplog: pytest.LogCaptureFixture) -> None:
|
2018-06-15 03:20:06 +08:00
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
logger.info("boo %s", "arg")
|
|
|
|
logger.info("bar %s\nbaz %s", "arg1", "arg2")
|
|
|
|
assert "boo arg" == caplog.messages[0]
|
|
|
|
assert "bar arg1\nbaz arg2" == caplog.messages[1]
|
|
|
|
assert caplog.text.count("\n") > len(caplog.messages)
|
|
|
|
assert len(caplog.text.splitlines()) > len(caplog.messages)
|
|
|
|
|
|
|
|
try:
|
|
|
|
raise Exception("test")
|
|
|
|
except Exception:
|
|
|
|
logger.exception("oops")
|
|
|
|
|
|
|
|
assert "oops" in caplog.text
|
|
|
|
assert "oops" in caplog.messages[-1]
|
|
|
|
# Tracebacks are stored in the record and not added until the formatter or handler.
|
|
|
|
assert "Exception" in caplog.text
|
|
|
|
assert "Exception" not in caplog.messages[-1]
|
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_record_tuples(caplog: pytest.LogCaptureFixture) -> None:
|
2018-01-17 06:32:59 +08:00
|
|
|
caplog.set_level(logging.INFO)
|
2017-09-13 19:12:07 +08:00
|
|
|
logger.info("boo %s", "arg")
|
|
|
|
|
|
|
|
assert caplog.record_tuples == [(__name__, logging.INFO, "boo arg")]
|
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_unicode(caplog: pytest.LogCaptureFixture) -> None:
|
2018-01-17 06:32:59 +08:00
|
|
|
caplog.set_level(logging.INFO)
|
2017-09-27 05:27:38 +08:00
|
|
|
logger.info("bū")
|
2017-09-13 19:12:07 +08:00
|
|
|
assert caplog.records[0].levelname == "INFO"
|
2017-09-27 05:27:38 +08:00
|
|
|
assert caplog.records[0].msg == "bū"
|
|
|
|
assert "bū" in caplog.text
|
2017-09-13 19:12:07 +08:00
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_clear(caplog: pytest.LogCaptureFixture) -> None:
|
2018-01-17 06:32:59 +08:00
|
|
|
caplog.set_level(logging.INFO)
|
2017-09-27 05:27:38 +08:00
|
|
|
logger.info("bū")
|
2017-09-13 19:12:07 +08:00
|
|
|
assert len(caplog.records)
|
2018-03-13 06:28:47 +08:00
|
|
|
assert caplog.text
|
2017-09-13 19:12:07 +08:00
|
|
|
caplog.clear()
|
|
|
|
assert not len(caplog.records)
|
2018-03-13 06:28:47 +08:00
|
|
|
assert not caplog.text
|
2018-01-15 22:01:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-10-22 03:02:54 +08:00
|
|
|
def logging_during_setup_and_teardown(
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
) -> Iterator[None]:
|
2018-01-20 22:04:28 +08:00
|
|
|
caplog.set_level("INFO")
|
2018-01-15 22:01:01 +08:00
|
|
|
logger.info("a_setup_log")
|
|
|
|
yield
|
|
|
|
logger.info("a_teardown_log")
|
2018-01-25 04:59:08 +08:00
|
|
|
assert [x.message for x in caplog.get_records("teardown")] == ["a_teardown_log"]
|
2018-01-15 22:01:01 +08:00
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_caplog_captures_for_all_stages(
|
|
|
|
caplog: pytest.LogCaptureFixture, logging_during_setup_and_teardown: None
|
|
|
|
) -> None:
|
2018-01-15 22:01:01 +08:00
|
|
|
assert not caplog.records
|
2018-01-25 04:59:08 +08:00
|
|
|
assert not caplog.get_records("call")
|
2018-01-15 22:01:01 +08:00
|
|
|
logger.info("a_call_log")
|
2018-01-25 04:59:08 +08:00
|
|
|
assert [x.message for x in caplog.get_records("call")] == ["a_call_log"]
|
2018-01-15 22:01:01 +08:00
|
|
|
|
2018-01-25 04:59:08 +08:00
|
|
|
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
|
2018-01-15 22:01:01 +08:00
|
|
|
|
2018-11-09 08:44:53 +08:00
|
|
|
# This reaches into private API, don't use this type of thing in real tests!
|
2023-10-22 03:02:54 +08:00
|
|
|
caplog_records = caplog._item.stash[caplog_records_key]
|
|
|
|
assert set(caplog_records) == {"setup", "call"}
|
2020-06-09 09:05:46 +08:00
|
|
|
|
|
|
|
|
2023-10-22 03:02:54 +08:00
|
|
|
def test_clear_for_call_stage(
|
|
|
|
caplog: pytest.LogCaptureFixture, logging_during_setup_and_teardown: None
|
|
|
|
) -> None:
|
2022-06-15 23:11:55 +08:00
|
|
|
logger.info("a_call_log")
|
|
|
|
assert [x.message for x in caplog.get_records("call")] == ["a_call_log"]
|
|
|
|
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
|
2023-10-22 03:02:54 +08:00
|
|
|
caplog_records = caplog._item.stash[caplog_records_key]
|
|
|
|
assert set(caplog_records) == {"setup", "call"}
|
2022-06-15 23:11:55 +08:00
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
assert caplog.get_records("call") == []
|
|
|
|
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
|
2023-10-22 03:02:54 +08:00
|
|
|
caplog_records = caplog._item.stash[caplog_records_key]
|
|
|
|
assert set(caplog_records) == {"setup", "call"}
|
2022-06-15 23:11:55 +08:00
|
|
|
|
2022-06-24 19:17:55 +08:00
|
|
|
logging.info("a_call_log_after_clear")
|
|
|
|
assert [x.message for x in caplog.get_records("call")] == ["a_call_log_after_clear"]
|
|
|
|
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
|
2023-10-22 03:02:54 +08:00
|
|
|
caplog_records = caplog._item.stash[caplog_records_key]
|
|
|
|
assert set(caplog_records) == {"setup", "call"}
|
2022-06-24 19:17:55 +08:00
|
|
|
|
2022-06-15 23:11:55 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_ini_controls_global_log_level(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-06-09 09:05:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
def test_log_level_override(request, caplog):
|
|
|
|
plugin = request.config.pluginmanager.getplugin('logging-plugin')
|
|
|
|
assert plugin.log_level == logging.ERROR
|
|
|
|
logger = logging.getLogger('catchlog')
|
|
|
|
logger.warning("WARNING message won't be shown")
|
|
|
|
logger.error("ERROR message will be shown")
|
|
|
|
assert 'WARNING' not in caplog.text
|
|
|
|
assert 'ERROR' in caplog.text
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2020-06-09 09:05:46 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
log_level=ERROR
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-06-09 09:05:46 +08:00
|
|
|
# make sure that that we get a '0' exit code for the testsuite
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_caplog_can_override_global_log_level(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-06-09 09:05:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
def test_log_level_override(request, caplog):
|
|
|
|
logger = logging.getLogger('catchlog')
|
|
|
|
plugin = request.config.pluginmanager.getplugin('logging-plugin')
|
|
|
|
assert plugin.log_level == logging.WARNING
|
|
|
|
|
|
|
|
logger.info("INFO message won't be shown")
|
|
|
|
|
|
|
|
caplog.set_level(logging.INFO, logger.name)
|
|
|
|
|
|
|
|
with caplog.at_level(logging.DEBUG, logger.name):
|
|
|
|
logger.debug("DEBUG message will be shown")
|
|
|
|
|
|
|
|
logger.debug("DEBUG message won't be shown")
|
|
|
|
|
|
|
|
with caplog.at_level(logging.CRITICAL, logger.name):
|
|
|
|
logger.warning("WARNING message won't be shown")
|
|
|
|
|
|
|
|
logger.debug("DEBUG message won't be shown")
|
|
|
|
logger.info("INFO message will be shown")
|
|
|
|
|
|
|
|
assert "message won't be shown" not in caplog.text
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2020-06-09 09:05:46 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
log_level=WARNING
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-06-09 09:05:46 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_caplog_captures_despite_exception(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-06-09 09:05:46 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
def test_log_level_override(request, caplog):
|
|
|
|
logger = logging.getLogger('catchlog')
|
|
|
|
plugin = request.config.pluginmanager.getplugin('logging-plugin')
|
|
|
|
assert plugin.log_level == logging.WARNING
|
|
|
|
|
2020-04-30 16:30:43 +08:00
|
|
|
logger.error("ERROR message " + "will be shown")
|
2020-06-09 09:05:46 +08:00
|
|
|
|
|
|
|
with caplog.at_level(logging.DEBUG, logger.name):
|
2020-04-30 16:30:43 +08:00
|
|
|
logger.debug("DEBUG message " + "won't be shown")
|
2020-06-09 09:05:46 +08:00
|
|
|
raise Exception()
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeini(
|
2020-06-09 09:05:46 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
log_level=WARNING
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2020-04-30 16:30:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*ERROR message will be shown*"])
|
|
|
|
result.stdout.no_fnmatch_line("*DEBUG message won't be shown*")
|
|
|
|
assert result.ret == 1
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_log_report_captures_according_to_config_option_upon_failure(
|
|
|
|
pytester: Pytester,
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test that upon failure:
|
|
|
|
(1) `caplog` succeeded to capture the DEBUG message and assert on it => No `Exception` is raised.
|
|
|
|
(2) The `DEBUG` message does NOT appear in the `Captured log call` report.
|
|
|
|
(3) The stdout, `INFO`, and `WARNING` messages DO appear in the test reports due to `--log-level=INFO`.
|
2020-04-30 16:30:43 +08:00
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-04-30 16:30:43 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
|
|
|
|
def function_that_logs():
|
|
|
|
logging.debug('DEBUG log ' + 'message')
|
|
|
|
logging.info('INFO log ' + 'message')
|
|
|
|
logging.warning('WARNING log ' + 'message')
|
|
|
|
print('Print ' + 'message')
|
|
|
|
|
|
|
|
def test_that_fails(request, caplog):
|
|
|
|
plugin = request.config.pluginmanager.getplugin('logging-plugin')
|
|
|
|
assert plugin.log_level == logging.INFO
|
|
|
|
|
|
|
|
with caplog.at_level(logging.DEBUG):
|
|
|
|
function_that_logs()
|
|
|
|
|
|
|
|
if 'DEBUG log ' + 'message' not in caplog.text:
|
|
|
|
raise Exception('caplog failed to ' + 'capture DEBUG')
|
|
|
|
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--log-level=INFO")
|
2020-04-30 16:30:43 +08:00
|
|
|
result.stdout.no_fnmatch_line("*Exception: caplog failed to capture DEBUG*")
|
|
|
|
result.stdout.no_fnmatch_line("*DEBUG log message*")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*Print message*", "*INFO log message*", "*WARNING log message*"]
|
|
|
|
)
|
2020-06-09 09:05:46 +08:00
|
|
|
assert result.ret == 1
|