我试图让我的测试套件创建一个与我在开发中使用的不同的日志文件,但由于某种原因, override_settings 装饰器似乎不起作用。当我运行测试时,会写入相同的“项目/项目/调试日志文件”。我在哪里搞砸了?
# settings.py
...
LOGFILE = ROOT + '/debug_logfile'
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'standard': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'django.utils.log.NullHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['require_debug_false']
},
'logfile': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': LOGFILE,
'maxBytes': 50000,
'backupCount': 2,
'formatter': 'standard',
},
},
'loggers': {
'django': {
'handlers': ['null'],
'propagate': True,
'level': 'INFO',
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
},
'clients': {
'handlers': ['console', 'logfile'],
'level': 'DEBUG',
},
# display the db queries
#'django.db.backends': {
# 'handlers': ['console'],
# 'level': 'DEBUG',
#}
}
}
# clients.management.commands.remindmanagers.py
class Command(BaseCommand):
help = 'Creates task reminders and send emails to account and senior \
managers when their client\'s contracts are about to expire'
def handle(self, *args, **kwargs):
three_months = datetime.date.today() + datetime.timedelta(days=90)
# get all contracts that will be completed in the next 3 months
contracts = Contract.objects.filter(finish__lte=three_months
).exclude(notices_left=0)
if not contracts.exists():
log.info('Tried running but there are no contracts about to expire.')
return 0
...
# tests.test_clients
...
from django.core.management import call_command
@override_settings(LOGFILE=settings.ROOT + "/test_logfile")
class ClientCommandTest(BaseTestCase):
def _file_exists(file_path):
return os.path.exists(file_path)
def test_remindmanagers_no_contracts(self):
args = []
kwargs = {}
#self.assertFalse()
# since there are no contracts yet, this should create an entry in ROOT + /logfile
call_command('remindmanagers', *args, **kwargs) # this should log in project/project/test_logfile not debug_logfile