Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Rework test_download to allow running all tests of a single extractor #27461

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions test/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def _file_md5(fn):
return hashlib.md5(f.read()).hexdigest()


defs = gettestcases()
ie_testcases = gettestcases()


class TestDownload(unittest.TestCase):
class BaseDownloadTCase(unittest.TestCase):
# Parallel testing in nosetests. See
# http://nose.readthedocs.org/en/latest/doc_tests/test_multiprocess/multiprocess.html
_multiprocess_shared_ = True
Expand All @@ -83,8 +83,9 @@ def strclass(cls):
strclass(self.__class__),
' [%s]' % add_ie if add_ie else '')

def setUp(self):
self.defs = defs

TestDownload = unittest.TestSuite()


# Dynamically generate tests

Expand Down Expand Up @@ -246,20 +247,42 @@ def try_rm_tcs_files(tcs=None):
return test_template


cache = {}

# 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)
for test_case in ie_testcases:
# Get or create a sub-test for the extractor file
module_name = test_case.get('module_name', 'unknown').rsplit('.', 1)[-1] # type: str
extractor_file_name = str('test_%s' % module_name)
extractor_file_suite = getattr(TestDownload, extractor_file_name, None)
if extractor_file_suite is None:
extractor_file_suite = type(extractor_file_name, (unittest.TestSuite,), {})()
setattr(TestDownload, extractor_file_name, extractor_file_suite)
TestDownload.addTest(extractor_file_suite)

# Get or create a sub-test for the info extractor
# This class contains the actual tests
extractor_class_name = str('test_%s' % test_case['name'])
ExtractorClass = getattr(extractor_file_suite, extractor_class_name, None)
if ExtractorClass is None:
ExtractorClass = type(extractor_class_name, (BaseDownloadTCase,), {})
setattr(extractor_file_suite, extractor_class_name, ExtractorClass)

i = 0
tname = 'test_%d' % i
while hasattr(ExtractorClass, tname):
tname = 'test_%d' % i
i += 1
test_method = generator(test_case, tname)
test_method.__name__ = str(tname)
ie_list = test_case.get('add_ie')
test_method.add_ie = ie_list and ','.join(ie_list)
setattr(TestDownload, test_method.__name__, test_method)
del test_method

setattr(ExtractorClass, test_method.__name__, test_method)
extractor_file_suite.addTest(ExtractorClass(test_method.__name__))

del test_method, test_case


if __name__ == '__main__':
unittest.main()
unittest.main(argv=['TestDownload'])
1 change: 1 addition & 0 deletions youtube_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,7 @@ def get_testcases(self, include_onlymatching=False):
if not include_onlymatching and t.get('only_matching', False):
continue
t['name'] = type(self).__name__[:-len('IE')]
t['module_name'] = type(self).__module__
yield t

def is_suitable(self, age_limit):
Expand Down