22import datetime
33import textwrap
44import unittest
5+ import functools
56import contextlib
7+ import collections
68from test import support
79from nntplib import NNTP , GroupInfo , _have_ssl
810import nntplib
@@ -228,18 +230,42 @@ def test_zzquit(self):
228230 self .server .quit ()
229231 cls .server = None
230232
233+ @classmethod
234+ def wrap_methods (cls ):
235+ # Wrap all methods in a transient_internet() exception catcher
236+ # XXX put a generic version in test.support?
237+ def wrap_meth (meth ):
238+ @functools .wraps (meth )
239+ def wrapped (self ):
240+ with support .transient_internet (self .NNTP_HOST ):
241+ meth (self )
242+ return wrapped
243+ for name in dir (cls ):
244+ if not name .startswith ('test_' ):
245+ continue
246+ meth = getattr (cls , name )
247+ if not isinstance (meth , collections .Callable ):
248+ continue
249+ # Need to use a closure so that meth remains bound to its current
250+ # value
251+ setattr (cls , name , wrap_meth (meth ))
252+
253+ NetworkedNNTPTestsMixin .wrap_methods ()
254+
231255
232256class NetworkedNNTPTests (NetworkedNNTPTestsMixin , unittest .TestCase ):
233257 # This server supports STARTTLS (gmane doesn't)
234258 NNTP_HOST = 'news.trigofacile.com'
235259 GROUP_NAME = 'fr.comp.lang.python'
236260 GROUP_PAT = 'fr.comp.lang.*'
237261
262+ NNTP_CLASS = NNTP
263+
238264 @classmethod
239265 def setUpClass (cls ):
240266 support .requires ("network" )
241267 with support .transient_internet (cls .NNTP_HOST ):
242- cls .server = NNTP (cls .NNTP_HOST , timeout = TIMEOUT , usenetrc = False )
268+ cls .server = cls . NNTP_CLASS (cls .NNTP_HOST , timeout = TIMEOUT , usenetrc = False )
243269
244270 @classmethod
245271 def tearDownClass (cls ):
@@ -248,7 +274,7 @@ def tearDownClass(cls):
248274
249275
250276if _have_ssl :
251- class NetworkedNNTP_SSLTests (NetworkedNNTPTestsMixin , unittest . TestCase ):
277+ class NetworkedNNTP_SSLTests (NetworkedNNTPTests ):
252278
253279 # Technical limits for this public NNTP server (see http://www.aioe.org):
254280 # "Only two concurrent connections per IP address are allowed and
@@ -258,17 +284,7 @@ class NetworkedNNTP_SSLTests(NetworkedNNTPTestsMixin, unittest.TestCase):
258284 GROUP_NAME = 'comp.lang.python'
259285 GROUP_PAT = 'comp.lang.*'
260286
261- @classmethod
262- def setUpClass (cls ):
263- support .requires ("network" )
264- with support .transient_internet (cls .NNTP_HOST ):
265- cls .server = nntplib .NNTP_SSL (cls .NNTP_HOST , timeout = TIMEOUT ,
266- usenetrc = False )
267-
268- @classmethod
269- def tearDownClass (cls ):
270- if cls .server is not None :
271- cls .server .quit ()
287+ NNTP_CLASS = nntplib .NNTP_SSL
272288
273289 # Disabled as it produces too much data
274290 test_list = None
0 commit comments