Best Python code snippet using toolium_python
test_client.py
Source:test_client.py
...5import loguru6import pytest7from lognub.client import LOGGER_DEFAULT_CONFIG, configure_logger, get_logger, log_instance8@patch("lognub.client.loguru.logger.configure")9def test_configure_logger(mocked_configure: MagicMock):10 """Ensure configure_logger configures the loguru logger."""11 configure_logger()12 mocked_configure.assert_called_once_with(**LOGGER_DEFAULT_CONFIG)13@patch("lognub.client.loguru.logger.configure")14def test_configure_logger_uses_provided_logger_config(mocked_configure: MagicMock):15 """Ensure configure_logger uses the given loguru logger config."""16 config = {"handlers": {"sink": sys.stdout, "level": "DEBUG"}}17 configure_logger(config)18 mocked_configure.assert_called_once_with(**config)19def test_configure_logger_raises_valueerror_with_both_propagate_and_intercept():20 """Ensure configure_logger raises ValueError with both propagate and intercept."""21 with pytest.raises(ValueError):22 configure_logger(propagate=True, intercept=True)23@patch("lognub.captures.python_warnings.capture")24@patch("lognub.captures.python_warnings.release")25def test_configure_logger_captures_warnings(26 mocked_release: MagicMock,27 mocked_capture: MagicMock,28):29 """Ensure calling configure_logger with capture_warnings works."""30 configure_logger(capture_warnings=True)31 mocked_capture.assert_called_once_with(loguru.logger)32 mocked_release.assert_not_called()33 mocked_capture.reset_mock()34 configure_logger(capture_warnings=False)35 mocked_capture.assert_not_called()36 mocked_release.assert_called_once()37@patch("lognub.captures.python_exceptions.capture")38@patch("lognub.captures.python_exceptions.release")39def test_configure_logger_captures_exceptions(40 mocked_release: MagicMock,41 mocked_capture: MagicMock,42):43 """Ensure calling configure_logger with capture_exceptions works."""44 configure_logger(capture_exceptions=True)45 mocked_capture.assert_called_once_with(loguru.logger)46 mocked_release.assert_not_called()47 mocked_capture.reset_mock()48 configure_logger(capture_exceptions=False)49 mocked_capture.assert_not_called()50 mocked_release.assert_called_once()51@patch("lognub.handles.propagate_handler.PropagateHandler.add_handle")52@patch("lognub.handles.propagate_handler.PropagateHandler.remove_handle")53def test_configure_logger_propagate(mocked_remove_handle: MagicMock, mocked_add_handle: MagicMock):54 """Ensure calling configure_logger with propagate works."""55 configure_logger(propagate=True)56 mocked_add_handle.assert_called_once_with(loguru.logger)57 mocked_remove_handle.assert_not_called()58 mocked_add_handle.reset_mock()59 configure_logger(propagate=False)60 mocked_add_handle.assert_not_called()61 mocked_remove_handle.assert_called_once_with(loguru.logger)62@patch("lognub.handles.intercept_handler.InterceptHandler.add_handle")63@patch("lognub.handles.intercept_handler.InterceptHandler.remove_handle")64def test_configure_logger_intercept(mocked_remove_handle: MagicMock, mocked_add_handle: MagicMock):65 """Ensure calling configure_logger with intercept works."""66 configure_logger(intercept=True)67 mocked_add_handle.assert_called_once_with(loguru.logger)68 mocked_remove_handle.assert_not_called()69 mocked_add_handle.reset_mock()70 configure_logger(intercept=False)71 mocked_add_handle.assert_not_called()72 mocked_remove_handle.assert_called_once_with(loguru.logger)73def test_get_logger():74 """Ensure get_logger works."""75 get_logger.cache_clear()76 assert isinstance(get_logger(), loguru._logger.Logger)77@patch("lognub.client.patch_logger")78def test_get_logger_patches_logger(mocked_patch_logger: MagicMock):79 """Ensure the logger from get_logger will attempt to patch itself."""80 get_logger.cache_clear()81 get_logger()82 mocked_patch_logger.assert_called_once_with(loguru.logger)83def test_get_logger_remaps_warn_callable():84 """Ensure the logger from get_logger contains a remapped ``warn`` callable."""...
log.py
Source:log.py
...17 logging handler settings, you should do so through the18 :func:`~.log.configure_logger` function:19 .. code-block:: python20 from .log import configure_logger, instance21 configure_logger(instance, debug=True)22Attributes:23 instance (:class:`loguru.Logger`):24 The configured global logger instance that should likely always be used.25"""26from __future__ import annotations27import sys28from functools import lru_cache29from typing import Any, Dict, List30import loguru31from .config import instance as config32DEFAULT_LOG_FORMAT = "<dim>{time}</dim> <level>{level:8s}</level> {message}"33DEFAULT_RECORD_HANDLER = dict(34 sink=config.log_dir.joinpath(f"{config.app_name!s}.log").as_posix(),35 level="DEBUG",36 format=DEFAULT_LOG_FORMAT,37 rotation="00:00",38 retention="10 days",39 compression="zip",40 serialize=True,41)42DEFAULT_STDOUT_HANDLER = dict(43 sink=sys.stdout,44 level="CRITICAL",45 format=DEFAULT_LOG_FORMAT,46)47def configure_logger(48 logger: loguru.Logger,49 level: str = "CRITICAL",50 debug: bool = False,51 record: bool = False,52) -> loguru.Logger:53 """Configure the global logger.54 Args:55 logger (:class:`loguru.Logger`):56 The global logger instance to configure.57 level (str, optional):58 The string level to filter logging messages through.59 Defaults to "CRITICAL"60 debug (bool, optional):61 If True, configures the logger with the debug configuration.62 Defaults to False.63 record (bool, optional):64 If True, logs will be recorded and written out to the log directory.65 Defaults to False.66 Returns:67 :class:`loguru.Logger`:68 The newly configured global logger69 """70 handlers: List[Dict[str, Any]] = [71 {72 **DEFAULT_STDOUT_HANDLER,73 **dict(level=level, diagnose=debug, backtrace=debug),74 }75 ]76 if record:77 handlers.append(DEFAULT_RECORD_HANDLER)78 logger.configure(handlers=handlers)79 return logger.bind(version=config.app_version)80@lru_cache(maxsize=2)81def get_logger(debug=False) -> loguru.Logger:82 """Get the configured global logger.83 Args:84 debug (bool, optional):85 If True, enables debug logging.86 Defaults to False.87 Returns:88 :class:`loguru.Logger`:89 The configured global logger90 """91 return configure_logger(loguru.logger, debug=debug)...
test_configure_logger.py
Source:test_configure_logger.py
...5from test.dump_aci import DumpOption, DumpDevice, DumpApplicationConfig6class TestConfigureLogger(unittest.TestCase):7 def setUp(self):8 options = DumpOption()9 logger = configure_logger(options.log_level, options.devices[0].split("/")[-1], options.devices[0])10 self.session = MeshSerialSession(DumpDevice(), logger, DumpApplicationConfig(), None)11 self.session.logger.error("Test error")12 self.session.logger.warning("Test warn")13 self.session.logger.info("Test info")14 self.session.logger.debug("Test debug")15 def test_check_logger_singleton(self):16 options = DumpOption()17 logger = configure_logger(options.log_level, options.devices[0].split("/")[-1], options.devices[0])18 self.assertIs(logger, self.session.logger)19if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!