# Improve email unit-testing

**URL:** <https://forum.djangoproject.com/t/improve-email-unit-testing/32044>\
**Category:** Django Internals\
**Created:** [June 13, 2024, 9:08am UTC](https://forum.djangoproject.com/t/improve-email-unit-testing/32044 "2024-06-13T09:08:48Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![GitRon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gitron/32/13088_2.png) [@GitRon](https://forum.djangoproject.com/u/GitRon)\
**Post date:** [June 13, 2024, 9:08am UTC](https://forum.djangoproject.com/t/improve-email-unit-testing/32044/1 "2024-06-13T09:08:48Z")

</div>

Hello Django-world!

As some might have seen in Copenhagen (Day) or Vigo, I’ve created a package called django-pony-express. Since I got (in my opinion) really good feedback from a bunch of smart people, I’d like to suggest to move some stuff that I’ve built into core.

I think, the test suite might be a great starting point. I’ve create a wrapper for `mail.outbox` so you can use it like a Django Queryset. ([Docs](https://django-pony-express.readthedocs.io/en/latest/features/tests.html)).

Current:  
`html_content = mail.outbox[0].alternatives[0][0]`

My suggestion:  
`html_content = email_test_service.filter(subject='Nigerian prince').get_html_content()`

And for assertions:

```auto
list_of_emails = email_test_service.filter(subject=subject)

# We expect an email to be sent
self.assertEqual(list_of_emails.count(), previous_email_count + 1)

# Assert content
list_of_emails.assert_body_contains(f"Hola {user.first_name}")

```

Currently, the functionality lives in a class which you have to instanciate. I’m open to suggestions on how to make this (even) more django-esque.

The code is simple, it works, it’s documented and it’s tested.

I’d like go gather some thumbs-up to check if this is worth following up on.

Best from Cologne  
Ronny

---

_[View the full topic](https://forum.djangoproject.com/t/improve-email-unit-testing/32044)._
