Skip to content

Instantly share code, notes, and snippets.

@LoanCB
Last active July 4, 2022 06:58
Show Gist options
  • Save LoanCB/366503830ee1af960b9d0d9b1c5b25b9 to your computer and use it in GitHub Desktop.
Save LoanCB/366503830ee1af960b9d0d9b1c5b25b9 to your computer and use it in GitHub Desktop.
Django custom TestCase for unit test
def _debugMail(body_html: bool):
print(
" ---------------------------------------------------------------------\n",
"| DEBUG INFORMATIONS ON THE MAIL(S) CONTAINED IN THE FOLDER |\n",
"---------------------------------------------------------------------\n\n"
)
for index, email in enumerate(mail.outbox):
print(f"Mail n°{index + 1}")
print(f"Subject : {email.subject}")
print(f"Recipient(s) : {email.to}")
print(f"Contenu : {email.body}")
if body_html:
print(f"Contenu HTML : {email.alternatives[0][0]}")
print('\n')
print(
" ---------------------------------------------------------------------\n",
"| END DEBUG |\n",
"---------------------------------------------------------------------"
)
class CustomTestCase(TestCase):
"""
class inherited from TestCase to add custom functions:
- assertMessageContains
- assertExternalLink
- assertSameMail
- assertVariousMail
"""
def assertMessageContains(self, response, messages_list, debug=False):
"""
Function to test messages returned by the response view
:param response: The response of the view
:param messages_list: An ordered list of messages to test
:type messages_list: list of string
:param debug: Show all response messages
:type debug: bool
"""
response_messages = list(response.context['messages'])
if debug:
print(
" ---------------------------------------------------------------------\n",
"| DEBUG MESSAGES RETURNED BY THE RESPONSE |\n",
"---------------------------------------------------------------------"
)
for i in range(len(response_messages)):
print(f"Message n°{i + 1} :\n{response_messages[i]}\n\n")
print(
" ---------------------------------------------------------------------\n",
"| END DEBUG |\n",
"---------------------------------------------------------------------"
)
self.assertEqual(len(response_messages), len(messages_list))
for i in range(len(response_messages)):
self.assertEqual(str(response_messages[i]), messages_list[i])
def assertExternalLink(self, response, link):
"""
Function to test external link returned by the response view
Attributes:
:param response: The response of the view
:param link: The returned link
:type link: str
"""
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], link)
def assertSameMail(self, subject: str, recipient: Iterable, count: int = 1, start: int = None, end: int = None,
body_text: str = None, body_html: str = None, debug: bool = False):
"""
Function to test send one or more same mails to a list of recipient
Attributes:
:param subject: The subject of the mail
:param recipient: Addressee(s) of mail(s)
:param count: Number of mails (default 1). Not necessary if start and end are filled in
:param start: The start of the for if you want to test a part of mails
:param end: The end of the for if you want to test a part of mails
:param body_text: The body of the mail in str text
:param body_html: The body of the mail in str html
:param debug: Print informations before testing
Example mail:
self.assertSameMail('amazing subject !', [foo@example.com])
Example multiple mails:
self.assertSameMail('amazing subject !', [foo@example.com], 5)
Example mails with an interval:
self.assertSameMail('amazing subject !', [foo@example.com], start=1, end=3)
"""
if debug:
_debugMail(True if body_html else False)
count = end - start if (start and end) else count
self.assertEqual(len(mail.outbox[start:end]), count)
for email in mail.outbox[start:end]:
self.assertEqual(email.subject, subject)
for m in email.to:
self.assertIn(m, recipient)
if body_text:
self.assertEqual(body_text, email.body)
if body_html:
self.assertEqual(body_html, email.alternatives[0][0])
def assertVariousMail(self, subject: Union[list, tuple, set], recipient: Iterable, start: int = None,
end: int = None, body_text: Union[list, tuple, set] = None,
body_html: Union[list, tuple, set] = None, debug: bool = False):
"""
Function to test send one or more differents mails to a list of recipient
Attributes:
:param subject: The subject of the mail
:param recipient: Addressee(s) of mail(s)
:param start: The start of the for if you want to test a part of mails
:param end: The end of the for if you want to test a part of mails
:param body_text: The body of the mail in str text
:param body_html: The body of the mail in str html
:param debug: Print informations before testing
Example mail:
self.assertVariousMail(['foo 1', 'foo 2'], ['foo@fexample.com'])
Example multiple mails:
self.assertVariousMail(['foo 1', 'foo 2'], ['foo@fexample.com', 'foo2@example.com'])
Example mails with an interval:
self.assertVariousMail(['foo 1', 'foo 2'], ['foo@fexample.com'], start=1, end=3)
"""
if debug:
_debugMail(True if body_html else False)
count = end - start if (start and end) else len(subject)
self.assertEqual(len(mail.outbox[start:end]), count)
for index, email in enumerate(mail.outbox[start:end]):
self.assertEqual(email.subject, subject[index])
for m in email.to:
self.assertIn(m, recipient)
if body_text:
self.assertEqual(body_text[index], email.body)
if body_html:
self.assertEqual(body_html[index], email.alternatives[0][0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment