Welcome to my blog!
Check out my latest posts below.
Welcome to my blog!
Check out my latest posts below.
Python’s unittest.mock library provides powerful and flexible mocking capabilities, simplifying test setups compared to many other programming languages. However, ensuring that mocks remain isolated and do not inadvertently affect other parts of your tests is crucial for maintaining reliable test suites. Example Scenario Consider the following logging setup: import logging import sys from google.cloud.logging_v2.handlers.structured_log import StructuredLogHandler import settings def setup_logging() -> None: logger = logging.getLogger() # Clear existing handlers to prevent duplicate logs logger.handlers = [] logger.setLevel(settings.LOG_LEVEL) handler_out = StructuredLogHandler(stream=sys.stdout) logger.addHandler(handler_out) # Manually configure handlers for loggers not inheriting from root logger_gunicorn = logging.getLogger("gunicorn.error") logger_gunicorn.handlers = [] logger_gunicorn.addHandler(handler_out) We want to test whether both logger and logger_gunicorn have the handler correctly added. ...
Hello World! For the 100th time my new blog was created.