@@ -365,6 +365,7 @@ def test_path_cache(self):
365365 from xml .etree import ElementPath
366366
367367 elem = ET .XML (SAMPLE_XML )
368+ ElementPath ._cache .clear ()
368369 for i in range (10 ): ET .ElementTree (elem ).find ('./' + str (i ))
369370 cache_len_10 = len (ElementPath ._cache )
370371 for i in range (10 ): ET .ElementTree (elem ).find ('./' + str (i ))
@@ -3926,8 +3927,9 @@ def test_issue14818(self):
39263927# --------------------------------------------------------------------
39273928
39283929class NoAcceleratorTest (unittest .TestCase ):
3929- def setUp (self ):
3930- if not pyET :
3930+ @classmethod
3931+ def setUpClass (cls ):
3932+ if ET is not pyET :
39313933 raise unittest .SkipTest ('only for the Python version' )
39323934
39333935 # Test that the C accelerator was not imported for pyET
@@ -4192,8 +4194,7 @@ def get_option(config, option_name, default=None):
41924194
41934195# --------------------------------------------------------------------
41944196
4195-
4196- def test_main (module = None ):
4197+ def setUpModule (module = None ):
41974198 # When invoked without a module, runs the Python ET tests by loading pyET.
41984199 # Otherwise, uses the given module as the ET.
41994200 global pyET
@@ -4205,63 +4206,30 @@ def test_main(module=None):
42054206 global ET
42064207 ET = module
42074208
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 )
42374214
42384215 # Provide default namespace mapping and path cache.
42394216 from xml .etree import ElementPath
42404217 nsmap = ET .register_namespace ._namespace_map
42414218 # Copy the default namespace mapping
42424219 nsmap_copy = nsmap .copy ()
4220+ unittest .addModuleCleanup (nsmap .update , nsmap_copy )
4221+ unittest .addModuleCleanup (nsmap .clear )
4222+
42434223 # Copy the path cache (should be empty)
42444224 path_cache = ElementPath ._cache
4225+ unittest .addModuleCleanup (setattr , ElementPath , "_cache" , path_cache )
42454226 ElementPath ._cache = path_cache .copy ()
4227+
42464228 # Align the Comment/PI factories.
42474229 if hasattr (ET , '_set_factories' ):
42484230 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 )
42644232
42654233
42664234if __name__ == '__main__' :
4267- test_main ()
4235+ unittest . main ()
0 commit comments