Skip to content

Commit 074ac1f

Browse files
bpo-45229: Make ElementTree tests discoverable (GH-108859)
1 parent d0b22f6 commit 074ac1f

File tree

2 files changed

+35
-62
lines changed

2 files changed

+35
-62
lines changed

Lib/test/test_xml_etree.py

+17-49
Original file line numberDiff line numberDiff line change
@@ -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

39283929
class 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

42664234
if __name__ == '__main__':
4267-
test_main()
4235+
unittest.main()

Lib/test/test_xml_etree_c.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,25 @@ def test_element_with_children(self):
254254
self.check_sizeof(e, self.elementsize + self.extra +
255255
struct.calcsize('8P'))
256256

257-
def test_main():
258-
from test import test_xml_etree
259-
260-
# Run the tests specific to the C implementation
261-
support.run_unittest(
262-
MiscTests,
263-
TestAliasWorking,
264-
TestAcceleratorImported,
265-
SizeofTest,
266-
)
267257

268-
# Run the same test suite as the Python module
269-
test_xml_etree.test_main(module=cET)
258+
def install_tests():
259+
# Test classes should have __module__ referring to this module.
260+
from test import test_xml_etree
261+
for name, base in vars(test_xml_etree).items():
262+
if isinstance(base, type) and issubclass(base, unittest.TestCase):
263+
class Temp(base):
264+
pass
265+
Temp.__name__ = Temp.__qualname__ = name
266+
Temp.__module__ = __name__
267+
assert name not in globals()
268+
globals()[name] = Temp
269+
270+
install_tests()
271+
272+
def setUpModule():
273+
from test import test_xml_etree
274+
test_xml_etree.setUpModule(module=cET)
270275

271276

272277
if __name__ == '__main__':
273-
test_main()
278+
unittest.main()

0 commit comments

Comments
 (0)