peopledoc / django-mail-factory

Django Mail Manager
Other
72 stars 17 forks source link

In tests, add a shortcut to assert mails are sent with correct parameters #16

Open benoitbryon opened 11 years ago

benoitbryon commented 11 years ago

As a django-mail-factory user, I setup Python functions (like views) that send email using mail_factory... thus I setup tests to check that mails are actually sent with expected parameters. Since this is a common task, I would appreciate a shortcut that makes tests easier to write.

The feature is mainly for users. Maybe it could also be used by django-mail-factory's own tests.

Here is an example of what I'm thinking of:

from django.test import TestCase

from mail_factory.tests import MailValidator

from myapp import send_notifications

class SomeTestCase(TestCase):
    def test_send_notifications(self):
        """send_notifications sends mails."""
        validator = MailValidator()  # Starts watching mail.outbox
        send_notifications('test@example.com')
        validator.assert_mail_sent(
            self,  // use TestCase API for assertions
            template_name='notification',
            to=['to@example.com'])

... where the "validator" is an helper to check result/usage of django-mail-factory, at least most common use cases.

Perhaps the example is not a good one, I'm not experienced enough to tell what is usually tested, or what should be tested. I guess that, if mail_factory had test utilities, I would simply use them (and test what they cover).

benoitbryon commented 11 years ago

I used such tools in django-downloadview: shortcuts to check that download views actually return expected responses. Since it is a common task to override and configure custom download views, test utilities are really helpful.

See:

Natim commented 11 years ago

This looks to me to be a mail-factory features doesn't it?

https://github.com/novagile/django-mail-factory/blob/master/mail_factory/mails.py#L37

benoitbryon commented 11 years ago

I was thinking of checks like:

Little code may be needed for each check, but if every user is supposed to write this little code, perhaps some shortcuts would help. And perhaps test utilities would encourage users to write tests.

Natim commented 10 years ago

That sounds good to me. Did you need to do that for one of your project? If yes do you mind doing a PR?