@@ -365,6 +365,7 @@ def test_path_cache(self):
365
365
from xml .etree import ElementPath
366
366
367
367
elem = ET .XML (SAMPLE_XML )
368
+ ElementPath ._cache .clear ()
368
369
for i in range (10 ): ET .ElementTree (elem ).find ('./' + str (i ))
369
370
cache_len_10 = len (ElementPath ._cache )
370
371
for i in range (10 ): ET .ElementTree (elem ).find ('./' + str (i ))
@@ -3926,8 +3927,9 @@ def test_issue14818(self):
3926
3927
# --------------------------------------------------------------------
3927
3928
3928
3929
class NoAcceleratorTest (unittest .TestCase ):
3929
- def setUp (self ):
3930
- if not pyET :
3930
+ @classmethod
3931
+ def setUpClass (cls ):
3932
+ if ET is not pyET :
3931
3933
raise unittest .SkipTest ('only for the Python version' )
3932
3934
3933
3935
# Test that the C accelerator was not imported for pyET
@@ -4192,8 +4194,7 @@ def get_option(config, option_name, default=None):
4192
4194
4193
4195
# --------------------------------------------------------------------
4194
4196
4195
-
4196
- def test_main (module = None ):
4197
+ def setUpModule (module = None ):
4197
4198
# When invoked without a module, runs the Python ET tests by loading pyET.
4198
4199
# Otherwise, uses the given module as the ET.
4199
4200
global pyET
@@ -4205,63 +4206,30 @@ def test_main(module=None):
4205
4206
global ET
4206
4207
ET = module
4207
4208
4208
- test_classes = [
4209
- ModuleTest ,
4210
- ElementSlicingTest ,
4211
- BasicElementTest ,
4212
- BadElementTest ,
4213
- BadElementPathTest ,
4214
- ElementTreeTest ,
4215
- IOTest ,
4216
- ParseErrorTest ,
4217
- XIncludeTest ,
4218
- ElementTreeTypeTest ,
4219
- ElementFindTest ,
4220
- ElementIterTest ,
4221
- TreeBuilderTest ,
4222
- XMLParserTest ,
4223
- XMLPullParserTest ,
4224
- BugsTest ,
4225
- KeywordArgsTest ,
4226
- BoolTest ,
4227
- C14NTest ,
4228
- ]
4229
-
4230
- # These tests will only run for the pure-Python version that doesn't import
4231
- # _elementtree. We can't use skipUnless here, because pyET is filled in only
4232
- # after the module is loaded.
4233
- if pyET is not ET :
4234
- test_classes .extend ([
4235
- NoAcceleratorTest ,
4236
- ])
4209
+ # don't interfere with subsequent tests
4210
+ def cleanup ():
4211
+ global ET , pyET
4212
+ ET = pyET = None
4213
+ unittest .addModuleCleanup (cleanup )
4237
4214
4238
4215
# Provide default namespace mapping and path cache.
4239
4216
from xml .etree import ElementPath
4240
4217
nsmap = ET .register_namespace ._namespace_map
4241
4218
# Copy the default namespace mapping
4242
4219
nsmap_copy = nsmap .copy ()
4220
+ unittest .addModuleCleanup (nsmap .update , nsmap_copy )
4221
+ unittest .addModuleCleanup (nsmap .clear )
4222
+
4243
4223
# Copy the path cache (should be empty)
4244
4224
path_cache = ElementPath ._cache
4225
+ unittest .addModuleCleanup (setattr , ElementPath , "_cache" , path_cache )
4245
4226
ElementPath ._cache = path_cache .copy ()
4227
+
4246
4228
# Align the Comment/PI factories.
4247
4229
if hasattr (ET , '_set_factories' ):
4248
4230
old_factories = ET ._set_factories (ET .Comment , ET .PI )
4249
- else :
4250
- old_factories = None
4251
-
4252
- try :
4253
- return support .run_unittest (* test_classes )
4254
- finally :
4255
- from xml .etree import ElementPath
4256
- # Restore mapping and path cache
4257
- nsmap .clear ()
4258
- nsmap .update (nsmap_copy )
4259
- ElementPath ._cache = path_cache
4260
- if old_factories is not None :
4261
- ET ._set_factories (* old_factories )
4262
- # don't interfere with subsequent tests
4263
- ET = pyET = None
4231
+ unittest .addModuleCleanup (ET ._set_factories , * old_factories )
4264
4232
4265
4233
4266
4234
if __name__ == '__main__' :
4267
- test_main ()
4235
+ unittest . main ()
0 commit comments