Using lettuce, how can I verify that an email sent from a Django web application has the correct contents?

怎甘沉沦 提交于 2019-12-09 19:57:20

问题


I have a Django-based web application that is required to send a confirmation email to the user on an attempt to change the registered email address. The functionality has been implemented, but the lettuce test intended to verify the contents of the email is failing.

To verify the operation, my plan was to use the file backend (EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend') then verify the contents of the file within my lettuce step.

When running "normally" (e.g. via manage.py runserver), the email file is created as expected. When run via lettuce (manage.py harvest), the web site appears to be getting driven correctly (I'm using Selenium to drive it) but no email file is generated.

What have I missed? Is there some setting (e.g. in the terrain.py file) I need to use so the file backend is also used during the test process?


回答1:


You can use django.core.mail.outbox as described in django docs https://docs.djangoproject.com/en/dev/topics/testing/#email-services

from django.core import mail

assert len(mail.outbox) == 1
assert mail.outbox[0].subject == 'Subject here'

Lettuse uses django.test.utils.setup_test_environment that overrides email backend to the locmem email backend.



来源:https://stackoverflow.com/questions/7795529/using-lettuce-how-can-i-verify-that-an-email-sent-from-a-django-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!