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

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

    If you don't want it repeatedly turn it on/off in setUp() and tearDown() for unittest (don't see the reason for that), you could just do it once per class:

        import unittest
        import logging
    
        class TestMyUnitTest(unittest.TestCase):
            @classmethod
            def setUpClass(cls):
                logging.disable(logging.CRITICAL)
            @classmethod
            def tearDownClass(cls):
                logging.disable(logging.NOTSET)
    

提交回复
热议问题