测试的时候python manage.py test -p "test_tasks.py" -v 3
,默认使用的EMAIL_BACKEND配置为:'django.core.mail.backends.locmem.EmailBackend',此时,在settings.py里的配置项EMAIL_BACKEND是不生效的;
若是想在测试的时候剩下,须要另外配置,好比:
#!/usr/bin/env python
# -- coding: utf-8 --python
from django.test import TestCase import unittest from plugin_security.tasks import send_email from django.conf import settings class TestEmail(TestCase): # @unittest.skip('skip') def test_send_email(self): # 这个配置必须加,由于django测试环境,修改了默认配置,致使邮件发送失败 # 参考/Users/xxx/.virtualenvs/erebus_app/lib/python3.6/site-packages/django/test/utils.py里的函数setup_test_environment settings.EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' res = send_email() self.assertEqual(res, True)
/Users/xxx/.virtualenvs/erebus_app/lib/python3.6/site-packages/django/conf/global_settings.pydjango
https://docs.djangoproject.com/en/2.2/topics/email/app