From 243c57cfe851431172bbf621ecc408afefbf8900 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Fri, 6 Aug 2021 09:59:41 +0530 Subject: [PATCH] [tests:download] Add batch testing for extractors Use `test_YourExtractor_all` to invoke them --- test/test_download.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/test/test_download.py b/test/test_download.py index 546748454..f26fb23c0 100644 --- a/test/test_download.py +++ b/test/test_download.py @@ -73,6 +73,8 @@ class TestDownload(unittest.TestCase): maxDiff = None + COMPLETED_TESTS = {} + def __str__(self): """Identify each test with the `add_ie` attribute, if available.""" @@ -94,6 +96,9 @@ class TestDownload(unittest.TestCase): def generator(test_case, tname): def test_template(self): + if self.COMPLETED_TESTS.get(tname): + return + self.COMPLETED_TESTS[tname] = True ie = yt_dlp.extractor.get_info_extractor(test_case['name'])() other_ies = [get_info_extractor(ie_key)() for ie_key in test_case.get('add_ie', [])] is_playlist = any(k.startswith('playlist') for k in test_case) @@ -255,12 +260,12 @@ def generator(test_case, tname): # And add them to TestDownload -for n, test_case in enumerate(defs): - tname = 'test_' + str(test_case['name']) - i = 1 - while hasattr(TestDownload, tname): - tname = 'test_%s_%d' % (test_case['name'], i) - i += 1 +tests_counter = {} +for test_case in defs: + name = test_case['name'] + i = tests_counter.get(name, 0) + tests_counter[name] = i + 1 + tname = f'test_{name}_{i}' if i else f'test_{name}' test_method = generator(test_case, tname) test_method.__name__ = str(tname) ie_list = test_case.get('add_ie') @@ -269,5 +274,22 @@ for n, test_case in enumerate(defs): del test_method +def batch_generator(name, num_tests): + + def test_template(self): + for i in range(num_tests): + getattr(self, f'test_{name}_{i}' if i else f'test_{name}')() + + return test_template + + +for name, num_tests in tests_counter.items(): + test_method = batch_generator(name, num_tests) + test_method.__name__ = f'test_{name}_all' + test_method.add_ie = '' + setattr(TestDownload, test_method.__name__, test_method) + del test_method + + if __name__ == '__main__': unittest.main()