@@ -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 ))
@@ -3955,8 +3956,9 @@ def test_issue14818(self):
3955
3956
# --------------------------------------------------------------------
3956
3957
3957
3958
class NoAcceleratorTest (unittest .TestCase ):
3958
- def setUp (self ):
3959
- if not pyET :
3959
+ @classmethod
3960
+ def setUpClass (cls ):
3961
+ if ET is not pyET :
3960
3962
raise unittest .SkipTest ('only for the Python version' )
3961
3963
3962
3964
# Test that the C accelerator was not imported for pyET
@@ -4202,8 +4204,7 @@ def get_option(config, option_name, default=None):
4202
4204
4203
4205
# --------------------------------------------------------------------
4204
4206
4205
-
4206
- def test_main (module = None ):
4207
+ def setUpModule (module = None ):
4207
4208
# When invoked without a module, runs the Python ET tests by loading pyET.
4208
4209
# Otherwise, uses the given module as the ET.
4209
4210
global pyET
@@ -4215,62 +4216,30 @@ def test_main(module=None):
4215
4216
global ET
4216
4217
ET = module
4217
4218
4218
- test_classes = [
4219
- ModuleTest ,
4220
- ElementSlicingTest ,
4221
- BasicElementTest ,
4222
- BadElementTest ,
4223
- BadElementPathTest ,
4224
- ElementTreeTest ,
4225
- IOTest ,
4226
- ParseErrorTest ,
4227
- XIncludeTest ,
4228
- ElementTreeTypeTest ,
4229
- ElementFindTest ,
4230
- ElementIterTest ,
4231
- TreeBuilderTest ,
4232
- XMLParserTest ,
4233
- XMLPullParserTest ,
4234
- BugsTest ,
4235
- KeywordArgsTest ,
4236
- C14NTest ,
4237
- ]
4238
-
4239
- # These tests will only run for the pure-Python version that doesn't import
4240
- # _elementtree. We can't use skipUnless here, because pyET is filled in only
4241
- # after the module is loaded.
4242
- if pyET is not ET :
4243
- test_classes .extend ([
4244
- NoAcceleratorTest ,
4245
- ])
4219
+ # don't interfere with subsequent tests
4220
+ def cleanup ():
4221
+ global ET , pyET
4222
+ ET = pyET = None
4223
+ unittest .addModuleCleanup (cleanup )
4246
4224
4247
4225
# Provide default namespace mapping and path cache.
4248
4226
from xml .etree import ElementPath
4249
4227
nsmap = ET .register_namespace ._namespace_map
4250
4228
# Copy the default namespace mapping
4251
4229
nsmap_copy = nsmap .copy ()
4230
+ unittest .addModuleCleanup (nsmap .update , nsmap_copy )
4231
+ unittest .addModuleCleanup (nsmap .clear )
4232
+
4252
4233
# Copy the path cache (should be empty)
4253
4234
path_cache = ElementPath ._cache
4235
+ unittest .addModuleCleanup (setattr , ElementPath , "_cache" , path_cache )
4254
4236
ElementPath ._cache = path_cache .copy ()
4237
+
4255
4238
# Align the Comment/PI factories.
4256
4239
if hasattr (ET , '_set_factories' ):
4257
4240
old_factories = ET ._set_factories (ET .Comment , ET .PI )
4258
- else :
4259
- old_factories = None
4260
-
4261
- try :
4262
- return support .run_unittest (* test_classes )
4263
- finally :
4264
- from xml .etree import ElementPath
4265
- # Restore mapping and path cache
4266
- nsmap .clear ()
4267
- nsmap .update (nsmap_copy )
4268
- ElementPath ._cache = path_cache
4269
- if old_factories is not None :
4270
- ET ._set_factories (* old_factories )
4271
- # don't interfere with subsequent tests
4272
- ET = pyET = None
4241
+ unittest .addModuleCleanup (ET ._set_factories , * old_factories )
4273
4242
4274
4243
4275
4244
if __name__ == '__main__' :
4276
- test_main ()
4245
+ unittest . main ()
0 commit comments