From f6fe8ecc10a3d88470e25fe6ebc61122154653f0 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 30 Sep 2016 12:22:52 -0400 Subject: [PATCH] Refs #27210 -- Fixed isolation of test_fail_silently_on_connection_error. The test wouldn't pass if a mail server is running on the system. --- tests/mail/tests.py | 48 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/tests/mail/tests.py b/tests/mail/tests.py index 1bb7236032..e680fb165a 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -1449,9 +1449,6 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase): finally: SMTP.send = send - -class SMTPNoServerTests(SimpleTestCase): - def test_send_messages_after_open_failed(self): """ send_messages() shouldn't try to send messages if open() raises an @@ -1465,30 +1462,31 @@ class SMTPNoServerTests(SimpleTestCase): email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com']) self.assertEqual(backend.send_messages([email]), None) + +class SMTPBackendStoppedServerTests(SMTPBackendTestsBase): + """ + These tests require a separate class, because the FakeSMTPServer is shut + down in setUpClass(), and it cannot be restarted ("RuntimeError: threads + can only be started once"). + """ + @classmethod + def setUpClass(cls): + super(SMTPBackendStoppedServerTests, cls).setUpClass() + cls.backend = smtp.EmailBackend(username='', password='') + cls.server.stop() + + def test_server_stopped(self): + """ + Closing the backend while the SMTP server is stopped doesn't raise an + exception. + """ + self.backend.close() + def test_fail_silently_on_connection_error(self): """ A socket connection error is silenced with fail_silently=True. """ - backend = smtp.EmailBackend(username='', password='') with self.assertRaises(socket.error): - backend.open() - backend.fail_silently = True - backend.open() - - -class SMTPBackendStoppedServerTest(SMTPBackendTestsBase): - """ - This test requires a separate class, because it shuts down the - FakeSMTPServer started in setUpClass(). It cannot be restarted - ("RuntimeError: threads can only be started once"). - """ - - def test_server_stopped(self): - """ - Test that closing the backend while the SMTP server is stopped doesn't - raise an exception. - """ - backend = smtp.EmailBackend(username='', password='') - backend.open() - self.server.stop() - backend.close() + self.backend.open() + self.backend.fail_silently = True + self.backend.open()