Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Test framework with file based Email backend server

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 the Email their account get activated.

In my settings.py I am using

EMAIL_FILE_PATH  ='django.core.mail.backends.filebased.EmailBackend'

which points to the local directory.

When running PyUnit test case from eclipse everything works file. Text file gets generated for each email sent

But, When i am using

python ./manage.py test <component_name>

the files does not generate.

Any insight what is the difference when I execute test case with ./manage.py and when I use pyUnit ?

like image 282
Anuj Avatar asked Dec 12 '12 21:12

Anuj


1 Answers

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
like image 193
Tim Edgar Avatar answered Nov 03 '22 00:11

Tim Edgar