Skip to content

Commit 7aee367

Browse files
peterlauripelme
authored andcommitted
re-introduced auto clearing of mail.outbox (pytest-dev#434)
1 parent 8f104d2 commit 7aee367

File tree

4 files changed

+35
-63
lines changed

4 files changed

+35
-63
lines changed

docs/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
Changelog
22
=========
33

4+
3.1.2
5+
-----
6+
7+
Bug fixes
8+
^^^^^^^^^
9+
10+
* Auto clearing of ``mail.outbox`` has been re-introduced to not break
11+
functionality in 3.x.x release. This means that Compatibility issues
12+
mentioned in the 3.1.0 release are no longer present. Related issue:
13+
_`pytest-django issue <https://github.com/pytest-dev/pytest-django/issues/433>`
14+
415
3.1.1
516
-----
617

docs/helpers.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,11 @@ Clearing of site cache
254254
If ``django.contrib.sites`` is in your INSTALLED_APPS, Site cache will
255255
be cleared for each test to avoid hitting the cache and cause wrong Site
256256
object to be returned by ``Site.objects.get_current()``.
257+
258+
259+
Clearing of mail.outbox
260+
~~~~~~~~~~~~~~~~~~~~~~~
261+
262+
``mail.outbox`` will be cleared for each pytest, to give tests a empty
263+
mailbox. It is however more pytestic to use the ``mailoutbox`` fixture
264+
to access ``mail.outbox``.

pytest_django/plugin.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -415,44 +415,22 @@ def teardown():
415415
request.addfinalizer(teardown)
416416

417417

418-
class _DirectMailboxAccessProtector(list):
419-
420-
def _raise_assertion(*args, **kwargs):
421-
__tracebackhide__ = True
422-
raise AssertionError('''To access mail.outbox, use the mailoutbox fixture.
423-
See http://pytest-django.readthedocs.io/en/latest/helpers.html#mailoutbox for more information.''')
424-
425-
__len__ = _raise_assertion
426-
__getitem__ = _raise_assertion
427-
__nonzero__ = _raise_assertion
428-
__bool__ = _raise_assertion
429-
__eq__ = _raise_assertion
430-
__ne__ = _raise_assertion
431-
__iter__ = _raise_assertion
432-
433-
434-
@pytest.fixture(autouse=True)
435-
def _error_on_direct_mail_outbox_access(monkeypatch):
418+
@pytest.fixture(scope='function', autouse=True)
419+
def _dj_autoclear_mailbox():
436420
if not django_settings_is_configured():
437421
return
438422

439423
from django.core import mail
440-
441-
outbox = _DirectMailboxAccessProtector()
442-
monkeypatch.setattr(mail, 'outbox', outbox)
443-
return outbox
424+
del mail.outbox[:]
444425

445426

446427
@pytest.fixture(scope='function')
447-
def mailoutbox(monkeypatch, _error_on_direct_mail_outbox_access):
428+
def mailoutbox(monkeypatch, _dj_autoclear_mailbox):
448429
if not django_settings_is_configured():
449430
return
450431

451432
from django.core import mail
452-
453-
outbox = list()
454-
monkeypatch.setattr(mail, 'outbox', outbox)
455-
return outbox
433+
return mail.outbox
456434

457435

458436
@pytest.fixture(autouse=True, scope='function')

tests/test_environment.py

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,17 @@
1818
# This is possible with some of the testdir magic, but this is the lazy way
1919
# to do it.
2020

21-
22-
class Test_direct_mailbox_access_not_allowed():
23-
24-
def test_len(self):
25-
with pytest.raises(AssertionError):
26-
len(mail.outbox)
27-
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
53-
54-
55-
def test_direct_mailbox_proection_should_not_break_sending_mail():
56-
mail.send_mail('subject', 'body', '[email protected]', ['[email protected]'])
21+
@pytest.mark.parametrize('subject', ['subject1', 'subject2'])
22+
def test_autoclear_mailbox(subject):
23+
assert len(mail.outbox) == 0
24+
mail.send_mail(subject, 'body', '[email protected]', ['[email protected]'])
25+
assert len(mail.outbox) == 1
26+
27+
m = mail.outbox[0]
28+
assert m.subject == subject
29+
assert m.body == 'body'
30+
assert m.from_email == '[email protected]'
31+
assert m.to == ['[email protected]']
5732

5833

5934
class TestDirectAccessWorksForDjangoTestCase(TestCase):

0 commit comments

Comments
 (0)