Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Django email testing support #313

Merged
merged 1 commit into from
Jan 14, 2013

Conversation

lorin
Copy link
Contributor

@lorin lorin commented Jan 12, 2013

This commit adds support for inspecting emails sent that were sent by the Django server.

I would appreciate some help in getting my tests integrated into the existing test framework. Currently the tests are in lettuce/django/tests/functional.

You can manually verify this code works by going into the lettuce/django/tests/functional' directory and doingmake test`. There's a test_email.py there that is supposed to do the same thing, but nose is failing because of a Django-related issue.

This commit adds support for inspecting emails sent that were sent by the Django server started by lettuce.
@gabrielfalcao
Copy link
Owner

Hello @lorin,
I love your commits, lettuce rarely gets people contributing with so much test coverage, I just love it.

I'd just like to understand why not just use Django's locmem email backend.

I usually use that for testing emails being sent from django apps:

settings/testing.py

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

then inside a test:

from sure import expect
from django.core import mail

def test_email_was_sent():
    expect(mail).to.have.property("mail").being.empty

    my_model = MyModel.objects.get(name="foobar")
    my_model.send_email()

    expect(mail.outbox).to.have.length_of(1)

@gabrielfalcao
Copy link
Owner

Also we can't make lettuce just change the setting EMAIL_BACKEND like it's being done here

For example at yipit we use the locmem backend for testing, so let's say I apply this pull request and make a new release; our tests would start to fail because lettuce is replacing the setting without asking for permission
👊

@lorin
Copy link
Contributor Author

lorin commented Jan 14, 2013

Hi @gabrielfalcao

The locmem backend works great for unit tests, but I could not get it to work properly when using Django with Lettuce.

As I understand it, the problem is that the Django server runs in a different process than the code that's running inside of steps.py, since the Django server is started by multiprocessing. Since locmem keeps things in memory instead of the database, the code executing in steps.py has a different copy of mail.outbox than the code executing inside of the Django server, since they are in separate memory address spaces.

I agree that we don't want to break existing tests, and if it's working with your lettuce tests, than that means I'm wrong about the behavior of locmem here.

Let me try to write a failing test that uses locmem as a backend to illustrate the problem. In the meantime, do you have any open source code from yipit or elsewhere that uses Lettuce, Django and locmem that I could take a look at?

@danclaudiupop
Copy link

harvest.py is using django.test.utils.setup_test_environment that overrides email backend to locmem whether you specify a different EMAIL_BACKEND in settings.

As @lorin said django server runs in a different process than lettuce and therefore you are not able to access mail.outbox.

@gabrielfalcao
Copy link
Owner

ok guys, that's awesome

gabrielfalcao added a commit that referenced this pull request Jan 14, 2013
Add Django email testing support
@gabrielfalcao gabrielfalcao merged commit f84207b into gabrielfalcao:master Jan 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants