summaryrefslogtreecommitdiff
path: root/django/test/utils.py
diff options
context:
space:
mode:
authorBoulder Sprinters <boulder-sprinters@djangoproject.com>2007-05-08 17:46:05 +0000
committerBoulder Sprinters <boulder-sprinters@djangoproject.com>2007-05-08 17:46:05 +0000
commit7f13278f8619b1155fa51276bb63afa9997610da (patch)
tree2df768ea9c6c866926ee7c3c6831a4fe91dfa097 /django/test/utils.py
parenta275d3da8ed8cea8c2c92fc15151f43fb56b42ce (diff)
boulder-oracle-sprint: Merged to [5173]
git-svn-id: http://code.djangoproject.com/svn/django/branches/boulder-oracle-sprint@5174 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/utils.py')
-rw-r--r--django/test/utils.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/django/test/utils.py b/django/test/utils.py
index 70b7f3cdbe..da339bb808 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -1,7 +1,7 @@
import sys, time
from django.conf import settings
from django.db import connection, backend, get_creation_module
-from django.core import management
+from django.core import management, mail
from django.dispatch import dispatcher
from django.test import signals
from django.template import Template
@@ -18,24 +18,54 @@ def instrumented_test_render(self, context):
dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
return self.nodelist.render(context)
+class TestSMTPConnection(object):
+ """A substitute SMTP connection for use during test sessions.
+ The test connection stores email messages in a dummy outbox,
+ rather than sending them out on the wire.
+
+ """
+ def __init__(*args, **kwargs):
+ pass
+ def open(self):
+ "Mock the SMTPConnection open() interface"
+ pass
+ def close(self):
+ "Mock the SMTPConnection close() interface"
+ pass
+ def send_messages(self, messages):
+ "Redirect messages to the dummy outbox"
+ mail.outbox.extend(messages)
+
def setup_test_environment():
"""Perform any global pre-test setup. This involves:
- Installing the instrumented test renderer
+ - Diverting the email sending functions to a test buffer
"""
Template.original_render = Template.render
Template.render = instrumented_test_render
+ mail.original_SMTPConnection = mail.SMTPConnection
+ mail.SMTPConnection = TestSMTPConnection
+
+ mail.outbox = []
+
def teardown_test_environment():
"""Perform any global post-test teardown. This involves:
- Restoring the original test renderer
+ - Restoring the email sending functions
"""
Template.render = Template.original_render
del Template.original_render
+ mail.SMTPConnection = mail.original_SMTPConnection
+ del mail.original_SMTPConnection
+
+ del mail.outbox
+
def _set_autocommit(connection):
"Make sure a connection is in autocommit mode."
if hasattr(connection.connection, "autocommit"):