Skip to content

Commit 5455dca

Browse files
authored
mailoutbox fixups (#427)
Make sure `mail.outbox == foo`, `mail.outbox != bar` and `iter(mail.outbox)` raises exceptions. This commit also improves the readability of the provided assertion message by hiding the internal traceback and showing the documentation link.
1 parent e659dcc commit 5455dca

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

pytest_django/plugin.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,17 @@ def teardown():
402402
class _DirectMailboxAccessProtector(list):
403403

404404
def _raise_assertion(*args, **kwargs):
405-
raise AssertionError('To access mail.outbox, use the mailoutbox fixture.')
406-
407-
__len__ = __getitem__ = __nonzero__ = __bool__ = _raise_assertion
405+
__tracebackhide__ = True
406+
raise AssertionError('''To access mail.outbox, use the mailoutbox fixture.
407+
See http://pytest-django.readthedocs.io/en/latest/helpers.html#mailoutbox for more information.''')
408+
409+
__len__ = _raise_assertion
410+
__getitem__ = _raise_assertion
411+
__nonzero__ = _raise_assertion
412+
__bool__ = _raise_assertion
413+
__eq__ = _raise_assertion
414+
__ne__ = _raise_assertion
415+
__iter__ = _raise_assertion
408416

409417

410418
@pytest.fixture(autouse=True)

tests/test_environment.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,37 @@
1919
# to do it.
2020

2121

22-
def test_direct_mailbox_access_not_allowed():
23-
with pytest.raises(AssertionError):
24-
len(mail.outbox)
22+
class Test_direct_mailbox_access_not_allowed():
2523

26-
with pytest.raises(AssertionError):
27-
mail.outbox[0]
24+
def test_len(self):
25+
with pytest.raises(AssertionError):
26+
len(mail.outbox)
2827

29-
with pytest.raises(AssertionError):
30-
if mail.outbox:
31-
pass
28+
def test_indexing(self):
29+
with pytest.raises(AssertionError):
30+
mail.outbox[0]
31+
32+
def test_bool(self):
33+
with pytest.raises(AssertionError):
34+
if mail.outbox:
35+
pass
36+
37+
def test_equality(self):
38+
with pytest.raises(AssertionError):
39+
mail.outbox == 'whatever'
40+
41+
def test_not_equality(self):
42+
with pytest.raises(AssertionError):
43+
mail.outbox != 'whatever'
44+
45+
def test_unpacking(self):
46+
with pytest.raises(AssertionError):
47+
(foo,) = mail.outbox
48+
49+
def test_iteration(self):
50+
with pytest.raises(AssertionError):
51+
for x in mail.outbox:
52+
pass
3253

3354

3455
def test_direct_mailbox_proection_should_not_break_sending_mail():

0 commit comments

Comments
 (0)