Search code examples
pythondjangolettuce

Can't access outbox while testing with lettuce


I am using Lettuce to test one of my applications.

I am testing a module to check if I can send emails.

I did some research and found in the Django documentation an easy way to test it.

from django.core import mail
from django.test import TestCase

class EmailTest(TestCase):
    def test_send_email(self):
        # Send message.
        mail.send_mail('Subject here', 'Here is the message.',
            '[email protected]', ['[email protected]'],
            fail_silently=False)

        # Test that one message has been sent.
        self.assertEqual(len(mail.outbox), 1)

        # Verify that the subject of the first message is correct.
        self.assertEqual(mail.outbox[0].subject, 'Subject here')

The problem is that I keep getting the error AttributeError: 'module' object has no attribute 'outbox'.

From what I found, the problem here is that

The Django server runs in a different process from the lettuce scripts, which would make the outbox inaccessible.

I did some more research and found a possible solution here.

The guy says this:

# in terrain.py
from lettuce import before, after, world
from django.conf import settings

@before.handle_request
    def override_mail_settings(httpd, server):
        settings.EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'

But I don't know what is my terrain.py equivalent.I tried in the steps.py file, but it didn't work.

Does anyone know how to fix this?


Solution

  • I managed to find here the answer to my problem after some more research.

    The only thing I had to do was edit settings.py and add this line:

    EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'