开发者

Disable logging during manage.py test?

I utilize the stan开发者_Go百科dard python logging module. When I call python manage.py test I'd like to disable logging before all the tests are ran. Is there a signal or some other kind of hook I could use to call logging.disable? Or is there some other way to disable logging when python manage.py test is ran?


There is actually a much better way to do so, with django-nose there is a kwarg:

Just run:

./bin/manage.py test --logging-clear-handlers


Easiest thing I've used:

import logging

class MyTestClass(TestCase):
    def setUp(self):
        logging.disable(logging.CRITICAL)

This requires no editing, patching, extra installs, and so on. All logging is turned completely off.


As an easy alternative, you can disable logging when running tests in your settings file like this:

if 'test' in sys.argv:
    logger.removeHandler(handler)
    logger.setLevel(logging.ERROR)


I prefer run tests with separate settings_test.py, where I can remove unnecessary applications, middleware and other stuff that I don't need during testing. And I can disable logging output here also by this way:

from settings import *
import logging

# direct all logging output to nowhere
class NullHandler(logging.Handler):
    def emit(self, record):
        pass
logging.getLogger().addHandler(NullHandler())


The only way I know of is to edit manage.py itself... not very elegant, of course, but at least it should get you to where you need to be.


I patched test_extensions to also do this.


If you are using django-nose, then you can add the below snippet to your settings.py file to disable logging output when running ./manage.py test

NOSE_ARGS = [
    '--nologcapture'
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜