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

后端 未结 15 772
爱一瞬间的悲伤
爱一瞬间的悲伤 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:42
    logging.disable(logging.CRITICAL)
    

    will disable all logging calls with levels less severe than or equal to CRITICAL. Logging can be re-enabled with

    logging.disable(logging.NOTSET)
    
    0 讨论(0)
  • 2020-12-07 10:45

    In my case I have a settings file settings/test.py created specifically for testing purposes, here's what it looks like:

    from .base import *
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'test_db'
        }
    }
    
    PASSWORD_HASHERS = (
        'django.contrib.auth.hashers.MD5PasswordHasher',
    )
    
    LOGGING = {}
    

    I put an environment variable DJANGO_SETTINGS_MODULE=settings.test to /etc/environment.

    0 讨论(0)
  • 2020-12-07 10:50

    If you have different initaliser modules for test, dev and production then you can disable anything or redirect it in the initialser. I have local.py, test.py and production.py that all inherit from common.y

    common.py does all the main config including this snippet :

    LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'django.server': {
            '()': 'django.utils.log.ServerFormatter',
            'format': '[%(server_time)s] %(message)s',
        },
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'django.server': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'django.server',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': True,
        },
        'celery.tasks': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.server': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False,
        },
    }
    

    Then in test.py I have this:

    console_logger = Common.LOGGING.get('handlers').get('console')
    console_logger['class'] = 'logging.FileHandler
    console_logger['filename'] = './unitest.log
    

    This replaces the console handler with a FileHandler and means still get logging but I do not have to touch the production code base.

    0 讨论(0)
提交回复
热议问题