Skip to content

Commit b9dee6a

Browse files
committed
This is a much bigger issue.
1 parent 9e786db commit b9dee6a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pytest_django/plugin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,10 @@ def _dj_autoclear_mailbox() -> None:
599599

600600
from django.core import mail
601601

602-
del mail.outbox[:]
603-
# if hasattr(main, "outbox"):
604-
# mail.outbox.clear()
602+
# import ipdb; print('\a'); ipdb.sset_trace()
603+
# del mail.outbox[:]
604+
if hasattr(mail, "outbox"):
605+
mail.outbox.clear()
605606

606607

607608
@pytest.fixture()

tests/test_environment.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,39 @@
1919

2020

2121
@pytest.mark.django_project(
22-
project_root="django_project_root",
2322
extra_settings="""
2423
EMAIL_BACKEND = "django.core.mail.backends.dummy.EmailBackend"
24+
import unittest.mock
25+
from types import SimpleNamespace
26+
27+
def setup_test_environment(*a, **k):
28+
if hasattr(_TestState, "saved_data"):
29+
# Executing this function twice would overwrite the saved values.
30+
raise RuntimeError(
31+
"setup_test_environment() was already called and can't be called "
32+
"again without first calling teardown_test_environment()."
33+
)
34+
35+
saved_data = SimpleNamespace()
36+
_TestState.saved_data = saved_data
37+
saved_data.allowed_hosts = []
38+
saved_data.debug = False
39+
saved_data.email_backend = None
40+
saved_data.template_render = None
41+
42+
unittest.mock.patch("django.test.utils.setup_test_environment", setup_test_environment).start()
43+
from django.test.utils import _TestState
2544
""",
2645
)
27-
def test_manage_test_runner(django_pytester: DjangoPytester) -> None:
46+
def test_mail_auto_fixture(django_pytester: DjangoPytester) -> None:
2847
django_pytester.create_test_module(
2948
"""
3049
def test_bad_mail():
3150
pass
3251
"""
3352
)
34-
result = django_pytester.runpytest_subprocess("-s")
53+
result = django_pytester.runpytest_subprocess("-s", "-vv")
54+
print("\n".join([*result.outlines, *result.errlines]))
3555
assert "1 passed" in "\n".join([*result.outlines, *result.errlines])
3656

3757

0 commit comments

Comments
 (0)