Skip to content

Commit 1530a6c

Browse files
committed
Added a new TestCase assertion: assertMailboxContains(), to be able to better express some test cases.
- Legacy-Id: 14139
1 parent 27b2414 commit 1530a6c

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

ietf/utils/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import os
3636
import re
3737
import html5lib
38+
import sys
3839
import urllib2
40+
3941
from unittest.util import strclass
4042
from bs4 import BeautifulSoup
4143

@@ -137,6 +139,30 @@ def assertNoFormPostErrors(self, response, error_css_selector=".has-error"):
137139

138140
self.assertEqual(response.status_code, 302)
139141

142+
def assertMailboxContains(self, mailbox, subject=None, text=None, count=None):
143+
"""
144+
Asserts that the given mailbox contains *count* mails with the given
145+
*subject* and body *text* (if not None). At least one of subject,
146+
text, and count must be different from None. If count is None, the
147+
filtered mailbox must be non-empty.
148+
"""
149+
if subject is None and text is None and count is None:
150+
raise self.failureException("No assertion made, both text and count is None")
151+
mlist = mailbox
152+
if subject:
153+
mlist = [ m for m in mlist if subject in m["Subject"] ]
154+
if text:
155+
mlist = [ m for m in mlist if text in m.get_payload(decode=True) ]
156+
if count and len(mlist) != count:
157+
sys.stderr.write("Wrong count in assertMailboxContains(). The complete mailbox contains %s emails:\n\n" % len(mailbox))
158+
for m in mailbox:
159+
sys.stderr.write(m.as_string())
160+
sys.stderr.write('\n\n')
161+
if count:
162+
self.assertEqual(len(mlist), count)
163+
else:
164+
self.assertGreater(len(mlist), 0)
165+
140166
def __str__(self):
141167
return "%s (%s.%s)" % (self._testMethodName, strclass(self.__class__),self._testMethodName)
142168

0 commit comments

Comments
 (0)