import importlib.util from pathlib import Path import tempfile import unittest import xml.etree.ElementTree as ET spec = importlib.util.spec_from_file_location( 'test_results_gate', Path(__file__).parents[1] / 'scripts/check_test_results.py') gate = importlib.util.module_from_spec(spec) spec.loader.exec_module(gate) class ReleaseTestGateTest(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.directory = Path(self.temp.name) for classname, names in gate.REQUIRED.items(): suite = ET.Element('testsuite', name=classname) for name in names or {'regressionTest'}: ET.SubElement(suite, 'testcase', classname=classname, name=name) ET.ElementTree(suite).write(self.directory / f'TEST-{classname}.xml') def change_worker_report(self, change): path = self.directory / f'TEST-{gate.WORKERS}.xml' tree = ET.parse(path) change(tree.getroot()) tree.write(path) def test_all_required_tests_pass(self): self.assertEqual(gate.check([self.directory]), 10) def test_empty_report_directory_blocks_release(self): with self.assertRaisesRegex(ValueError, 'Missing critical test suite'): gate.check([self.directory / 'missing']) def test_failed_worker_test_blocks_release(self): self.change_worker_report(lambda r: ET.SubElement(r[0], 'failure')) with self.assertRaisesRegex(ValueError, 'Failed test'): gate.check([self.directory]) def test_skipped_worker_test_blocks_release(self): self.change_worker_report(lambda r: ET.SubElement(r[0], 'skipped')) with self.assertRaisesRegex(ValueError, 'Skipped critical test'): gate.check([self.directory]) def test_removed_coroutine_worker_test_blocks_release(self): def remove(root): for case in list(root): if case.get('name') == 'matrixCoroutineWorkerIsCreatedByTheApplicationFactory': root.remove(case) self.change_worker_report(remove) with self.assertRaisesRegex(ValueError, 'Missing critical tests'): gate.check([self.directory])