Using lettuce, how can I verify that an email sent

2020-07-27 03:33发布

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条回答
冷血范
2楼-- · 2020-07-27 03:55

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.

查看更多
登录 后发表回答