How to disable logging when running tests in Python

Better Stack Team
Updated on August 25, 2023

To disable logging when running tests in Python, you can temporarily adjust the log level or remove the log handlers used during the test execution. This will prevent logs from being displayed or saved during testing. Here are two common approaches to achieve this:

1. Temporarily change the log level during testing

This approach sets the log level to CRITICAL, which disables all logging with levels lower than or equal to CRITICAL. You can adjust the log level to WARNING, ERROR, or other levels based on your requirements.

 
import logging

def disable_logging_during_tests():
    # Store the current log level to restore it later
    original_log_level = logging.getLogger().getEffectiveLevel()

    # Set the log level to a higher level, e.g., WARNING or CRITICAL
    logging.disable(logging.CRITICAL)

    # Run your tests here

    # Restore the original log level after the tests
    logging.disable(original_log_level)

# Call this function before running your tests
disable_logging_during_tests()

2. Temporarily remove the log handlers during testing

This approach removes all existing log handlers from the root logger, effectively disabling any log output during testing. After the tests are completed, the original log handlers are restored.

 
import logging

def disable_logging_during_tests():
    # Store the current log handlers to restore them later
    original_log_handlers = logging.getLogger().handlers[:]

    # Remove all existing log handlers
    for handler in original_log_handlers:
        logging.getLogger().removeHandler(handler)

    # Run your tests here

    # Restore the original log handlers after the tests
    for handler in original_log_handlers:
        logging.getLogger().addHandler(handler)

# Call this function before running your tests
disable_logging_during_tests()

Choose the approach that suits your needs. If you only need to suppress log messages during testing, changing the log level is usually sufficient. However, if you have more complex logging setups or want to ensure that no logs are written or displayed during testing, removing the log handlers might be a better option.

To learn more about logging in Python, visit Better Stack Community.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github