summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_xml_etree.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-04 10:04:32 (GMT)
committerGitHub <noreply@github.com>2023-09-04 10:04:32 (GMT)
commit074ac1f72e392a576516639f650bac0519d1cb52 (patch)
treef556839b383ddee0486ce55956eb2a6d1fa7de45 /Lib/test/test_xml_etree.py
parentd0b22f6bd84239e50b43709f98f2bb950222cfe5 (diff)
downloadcpython-074ac1f72e392a576516639f650bac0519d1cb52.zip
cpython-074ac1f72e392a576516639f650bac0519d1cb52.tar.gz
cpython-074ac1f72e392a576516639f650bac0519d1cb52.tar.bz2
bpo-45229: Make ElementTree tests discoverable (GH-108859)
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r--Lib/test/test_xml_etree.py66
1 files changed, 17 insertions, 49 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index da2828c..6d413aa 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -365,6 +365,7 @@ class ElementTreeTest(unittest.TestCase):
from xml.etree import ElementPath
elem = ET.XML(SAMPLE_XML)
+ ElementPath._cache.clear()
for i in range(10): ET.ElementTree(elem).find('./'+str(i))
cache_len_10 = len(ElementPath._cache)
for i in range(10): ET.ElementTree(elem).find('./'+str(i))
@@ -3926,8 +3927,9 @@ class KeywordArgsTest(unittest.TestCase):
# --------------------------------------------------------------------
class NoAcceleratorTest(unittest.TestCase):
- def setUp(self):
- if not pyET:
+ @classmethod
+ def setUpClass(cls):
+ if ET is not pyET:
raise unittest.SkipTest('only for the Python version')
# Test that the C accelerator was not imported for pyET
@@ -4192,8 +4194,7 @@ class C14NTest(unittest.TestCase):
# --------------------------------------------------------------------
-
-def test_main(module=None):
+def setUpModule(module=None):
# When invoked without a module, runs the Python ET tests by loading pyET.
# Otherwise, uses the given module as the ET.
global pyET
@@ -4205,63 +4206,30 @@ def test_main(module=None):
global ET
ET = module
- test_classes = [
- ModuleTest,
- ElementSlicingTest,
- BasicElementTest,
- BadElementTest,
- BadElementPathTest,
- ElementTreeTest,
- IOTest,
- ParseErrorTest,
- XIncludeTest,
- ElementTreeTypeTest,
- ElementFindTest,
- ElementIterTest,
- TreeBuilderTest,
- XMLParserTest,
- XMLPullParserTest,
- BugsTest,
- KeywordArgsTest,
- BoolTest,
- C14NTest,
- ]
-
- # These tests will only run for the pure-Python version that doesn't import
- # _elementtree. We can't use skipUnless here, because pyET is filled in only
- # after the module is loaded.
- if pyET is not ET:
- test_classes.extend([
- NoAcceleratorTest,
- ])
+ # don't interfere with subsequent tests
+ def cleanup():
+ global ET, pyET
+ ET = pyET = None
+ unittest.addModuleCleanup(cleanup)
# Provide default namespace mapping and path cache.
from xml.etree import ElementPath
nsmap = ET.register_namespace._namespace_map
# Copy the default namespace mapping
nsmap_copy = nsmap.copy()
+ unittest.addModuleCleanup(nsmap.update, nsmap_copy)
+ unittest.addModuleCleanup(nsmap.clear)
+
# Copy the path cache (should be empty)
path_cache = ElementPath._cache
+ unittest.addModuleCleanup(setattr, ElementPath, "_cache", path_cache)
ElementPath._cache = path_cache.copy()
+
# Align the Comment/PI factories.
if hasattr(ET, '_set_factories'):
old_factories = ET._set_factories(ET.Comment, ET.PI)
- else:
- old_factories = None
-
- try:
- return support.run_unittest(*test_classes)
- finally:
- from xml.etree import ElementPath
- # Restore mapping and path cache
- nsmap.clear()
- nsmap.update(nsmap_copy)
- ElementPath._cache = path_cache
- if old_factories is not None:
- ET._set_factories(*old_factories)
- # don't interfere with subsequent tests
- ET = pyET = None
+ unittest.addModuleCleanup(ET._set_factories, *old_factories)
if __name__ == '__main__':
- test_main()
+ unittest.main()