How can I disable logging while running unit tests in Python Django?

后端 未结 15 818
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 09:50

I am using a simple unit test based test runner to test my Django application.

My application itself is configured to use a basic logger in settings.py using:

<
15条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 10:29

    If you're using pytest:

    Since pytest captures log messages and only displays them for failed tests, you typically don't want to disable any logging. Instead, use a separate settings.py file for tests (e.g., test_settings.py), and add to it:

    LOGGING_CONFIG = None
    

    This tells Django to skip configuring the logging altogether. The LOGGING setting will be ignored and can be removed from the settings.

    With this approach, you don't get any logging for passed tests, and you get all available logging for failed tests.

    The tests will run using the logging that was set up by pytest. It can be configured to your liking in the pytest settings (e.g., tox.ini). To include debug level log messages, use log_level = DEBUG (or the corresponding command line argument).

提交回复
热议问题