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

后端 未结 15 778
爱一瞬间的悲伤
爱一瞬间的悲伤 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:32

    I am using a simple method decorator to disable logging only in a particular test method.

    def disable_logging(f):
    
        def wrapper(*args):
            logging.disable(logging.CRITICAL)
            result = f(*args)
            logging.disable(logging.NOTSET)
    
            return result
    
        return wrapper
    

    And then I use it as in the following example:

    class ScenarioTestCase(TestCase):
    
        @disable_logging
        test_scenario(self):
            pass
    

提交回复
热议问题