@@ -56,7 +56,7 @@ def server(evt, buf, serv):
5656 serv .close ()
5757 evt .set ()
5858
59- class GeneralTests ( unittest . TestCase ) :
59+ class GeneralTests :
6060
6161 def setUp (self ):
6262 smtplib .socket = mock_socket
@@ -75,86 +75,101 @@ def testQuoteData(self):
7575 def testBasic1 (self ):
7676 mock_socket .reply_with (b"220 Hola mundo" )
7777 # connects
78- smtp = smtplib . SMTP (HOST , self .port )
79- smtp .close ()
78+ client = self . client (HOST , self .port )
79+ client .close ()
8080
8181 def testSourceAddress (self ):
8282 mock_socket .reply_with (b"220 Hola mundo" )
8383 # connects
84- smtp = smtplib . SMTP (HOST , self .port ,
85- source_address = ('127.0.0.1' ,19876 ))
86- self .assertEqual (smtp .source_address , ('127.0.0.1' , 19876 ))
87- smtp .close ()
84+ client = self . client (HOST , self .port ,
85+ source_address = ('127.0.0.1' ,19876 ))
86+ self .assertEqual (client .source_address , ('127.0.0.1' , 19876 ))
87+ client .close ()
8888
8989 def testBasic2 (self ):
9090 mock_socket .reply_with (b"220 Hola mundo" )
9191 # connects, include port in host name
92- smtp = smtplib . SMTP ("%s:%s" % (HOST , self .port ))
93- smtp .close ()
92+ client = self . client ("%s:%s" % (HOST , self .port ))
93+ client .close ()
9494
9595 def testLocalHostName (self ):
9696 mock_socket .reply_with (b"220 Hola mundo" )
9797 # check that supplied local_hostname is used
98- smtp = smtplib . SMTP (HOST , self .port , local_hostname = "testhost" )
99- self .assertEqual (smtp .local_hostname , "testhost" )
100- smtp .close ()
98+ client = self . client (HOST , self .port , local_hostname = "testhost" )
99+ self .assertEqual (client .local_hostname , "testhost" )
100+ client .close ()
101101
102102 def testTimeoutDefault (self ):
103103 mock_socket .reply_with (b"220 Hola mundo" )
104104 self .assertIsNone (mock_socket .getdefaulttimeout ())
105105 mock_socket .setdefaulttimeout (30 )
106106 self .assertEqual (mock_socket .getdefaulttimeout (), 30 )
107107 try :
108- smtp = smtplib . SMTP (HOST , self .port )
108+ client = self . client (HOST , self .port )
109109 finally :
110110 mock_socket .setdefaulttimeout (None )
111- self .assertEqual (smtp .sock .gettimeout (), 30 )
112- smtp .close ()
111+ self .assertEqual (client .sock .gettimeout (), 30 )
112+ client .close ()
113113
114114 def testTimeoutNone (self ):
115115 mock_socket .reply_with (b"220 Hola mundo" )
116116 self .assertIsNone (socket .getdefaulttimeout ())
117117 socket .setdefaulttimeout (30 )
118118 try :
119- smtp = smtplib . SMTP (HOST , self .port , timeout = None )
119+ client = self . client (HOST , self .port , timeout = None )
120120 finally :
121121 socket .setdefaulttimeout (None )
122- self .assertIsNone (smtp .sock .gettimeout ())
123- smtp .close ()
122+ self .assertIsNone (client .sock .gettimeout ())
123+ client .close ()
124124
125125 def testTimeoutZero (self ):
126126 mock_socket .reply_with (b"220 Hola mundo" )
127127 with self .assertRaises (ValueError ):
128- smtplib . SMTP (HOST , self .port , timeout = 0 )
128+ self . client (HOST , self .port , timeout = 0 )
129129
130130 def testTimeoutValue (self ):
131131 mock_socket .reply_with (b"220 Hola mundo" )
132- smtp = smtplib . SMTP (HOST , self .port , timeout = 30 )
133- self .assertEqual (smtp .sock .gettimeout (), 30 )
134- smtp .close ()
132+ client = self . client (HOST , self .port , timeout = 30 )
133+ self .assertEqual (client .sock .gettimeout (), 30 )
134+ client .close ()
135135
136136 def test_debuglevel (self ):
137137 mock_socket .reply_with (b"220 Hello world" )
138- smtp = smtplib . SMTP ()
139- smtp .set_debuglevel (1 )
138+ client = self . client ()
139+ client .set_debuglevel (1 )
140140 with support .captured_stderr () as stderr :
141- smtp .connect (HOST , self .port )
142- smtp .close ()
141+ client .connect (HOST , self .port )
142+ client .close ()
143143 expected = re .compile (r"^connect:" , re .MULTILINE )
144144 self .assertRegex (stderr .getvalue (), expected )
145145
146146 def test_debuglevel_2 (self ):
147147 mock_socket .reply_with (b"220 Hello world" )
148- smtp = smtplib . SMTP ()
149- smtp .set_debuglevel (2 )
148+ client = self . client ()
149+ client .set_debuglevel (2 )
150150 with support .captured_stderr () as stderr :
151- smtp .connect (HOST , self .port )
152- smtp .close ()
151+ client .connect (HOST , self .port )
152+ client .close ()
153153 expected = re .compile (r"^\d{2}:\d{2}:\d{2}\.\d{6} connect: " ,
154154 re .MULTILINE )
155155 self .assertRegex (stderr .getvalue (), expected )
156156
157157
158+ class SMTPGeneralTests (GeneralTests , unittest .TestCase ):
159+
160+ client = smtplib .SMTP
161+
162+
163+ class LMTPGeneralTests (GeneralTests , unittest .TestCase ):
164+
165+ client = smtplib .LMTP
166+
167+ def testTimeoutZero (self ):
168+ super ().testTimeoutZero ()
169+ local_host = '/some/local/lmtp/delivery/program'
170+ with self .assertRaises (ValueError ):
171+ self .client (local_host , timeout = 0 )
172+
158173# Test server thread using the specified SMTP server class
159174def debugging_server (serv , serv_evt , client_evt ):
160175 serv_evt .set ()
0 commit comments