Django Test framework with file based Email backend server

前端 未结 2 1572
难免孤独
难免孤独 2021-01-18 02:25

I have formulated test cases in Django framework.

Use Case: I am using API that register user by sending them an Email and when they click on the link provided in t

2条回答
  •  情歌与酒
    2021-01-18 03:20

    It's possible to overwrite this aspect in Django if you want to use a specific email backend.

    In django.test.utils, Django will change the e-mail backend to locmem as mentioned in the Django Testing documentation when Django sets up the testing environment:

    def setup_test_environment():
    ...
        mail.original_email_backend = settings.EMAIL_BACKEND
        settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
    

    So if you want to enable sending e-mails for a test, you just need to change the setting to what you want.

    from django.test.utils import override_settings
    
    @override_settings(EMAIL_BACKEND='django.core.mail.backends.filebased.EmailBackend')
    class MyTest(TestCase):
        # your test case
    

提交回复
热议问题