How to use _CmpToKey method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...8from fnmatch import fnmatch9from unittest2 import case, suite10import unittest2.compatibility1112def _CmpToKey(mycmp):1314 class K(object):1516 def __init__(self, obj):17 self.obj = obj1819 def __lt__(self, other):20 return mycmp(self.obj, other.obj) == -12122 return K232425VALID_MODULE_NAME = re.compile('[_a-z]\\w*\\.py$', re.IGNORECASE)2627def _make_failed_import_test(name, suiteClass):28 message = 'Failed to import test module: %s' % name29 if hasattr(traceback, 'format_exc'):30 message += '\n%s' % traceback.format_exc()3132 def testImportFailure(self):33 raise ImportError(message)3435 attrs = {name: testImportFailure}36 ModuleImportFailure = type('ModuleImportFailure', (case.TestCase,), attrs)37 return suiteClass((ModuleImportFailure(name),))383940class TestLoader(unittest.TestLoader):41 testMethodPrefix = 'test'42 sortTestMethodsUsing = cmp43 suiteClass = suite.TestSuite44 _top_level_dir = None4546 def loadTestsFromTestCase(self, testCaseClass):47 if issubclass(testCaseClass, suite.TestSuite):48 raise TypeError('Test cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?')49 testCaseNames = self.getTestCaseNames(testCaseClass)50 if not testCaseNames and hasattr(testCaseClass, 'runTest'):51 testCaseNames = ['runTest']52 loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))53 return loaded_suite5455 def loadTestsFromModule(self, module, use_load_tests = True):56 tests = []57 for name in dir(module):58 obj = getattr(module, name)59 if isinstance(obj, type) and issubclass(obj, unittest.TestCase):60 tests.append(self.loadTestsFromTestCase(obj))6162 load_tests = getattr(module, 'load_tests', None)63 tests = self.suiteClass(tests)64 if use_load_tests and load_tests is not None:65 return load_tests(self, tests, None)66 return tests6768 def loadTestsFromName(self, name, module = None):69 parts = name.split('.')70 if module is None:71 parts_copy = parts[:]72 while parts_copy:73 try:74 module = __import__('.'.join(parts_copy))75 break76 except ImportError:77 del parts_copy[-1]78 if not parts_copy:79 raise8081 parts = parts[1:]82 obj = module83 for part in parts:84 parent, obj = obj, getattr(obj, part)8586 if isinstance(obj, types.ModuleType):87 return self.loadTestsFromModule(obj)88 if isinstance(obj, type) and issubclass(obj, unittest.TestCase):89 return self.loadTestsFromTestCase(obj)90 if isinstance(obj, types.UnboundMethodType) and isinstance(parent, type) and issubclass(parent, case.TestCase):91 return self.suiteClass([parent(obj.__name__)])92 if isinstance(obj, unittest.TestSuite):93 return obj94 if hasattr(obj, '__call__'):95 test = obj()96 if isinstance(test, unittest.TestSuite):97 return test98 if isinstance(test, unittest.TestCase):99 return self.suiteClass([test])100 raise TypeError('calling %s returned %s, not a test' % (obj, test))101 else:102 raise TypeError("don't know how to make test from: %s" % obj)103104 def loadTestsFromNames(self, names, module = None):105 suites = [ self.loadTestsFromName(name, module) for name in names ]106 return self.suiteClass(suites)107108 def getTestCaseNames(self, testCaseClass):109110 def isTestMethod(attrname, testCaseClass = testCaseClass, prefix = self.testMethodPrefix):111 return attrname.startswith(prefix) and hasattr(getattr(testCaseClass, attrname), '__call__')112113 testFnNames = filter(isTestMethod, dir(testCaseClass))114 if self.sortTestMethodsUsing:115 testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))116 return testFnNames117118 def discover(self, start_dir, pattern = 'test*.py', top_level_dir = None):119 if top_level_dir is None and self._top_level_dir is not None:120 top_level_dir = self._top_level_dir121 elif top_level_dir is None:122 top_level_dir = start_dir123 top_level_dir = os.path.abspath(os.path.normpath(top_level_dir))124 start_dir = os.path.abspath(os.path.normpath(start_dir))125 if top_level_dir not in sys.path:126 sys.path.append(top_level_dir)127 self._top_level_dir = top_level_dir128 if start_dir != top_level_dir and not os.path.isfile(os.path.join(start_dir, '')):129 raise ImportError('Start directory is not importable: %r' % start_dir) ...

Full Screen

Full Screen Github


Full Screen

...11 return attrname.startswith(prefix) and \12 hasattr(getattr(testCaseClass, attrname), '__call__')13 testFns = [getattr(testCaseClass, t) for t in filter(isTestMethod, dir(testCaseClass))]14 if self.sortTestMethodsUsing:15 testFns.sort(key=_CmpToKey(self.sortTestMethodsUsing))16 return [n.__name__ for n in testFns]17def testorder_cmp(self, tc1, tc2):18 if hasattr(tc1, '__order__') and hasattr(tc2, '__order__'):19 return cmp(tc1.__order__, tc2.__order__)20 else:21 return cmp(tc1.__name__, tc2.__name__)22orig_TestLoader_sortTestMethodsUsing = None23orig_TestLoader_getTestCaseNames = None24def replace_default_ordering():25 orig_TestLoader_getTestCaseNames = TestLoader.getTestCaseNames26 TestLoader.getTestCaseNames = testorder_getTestCaseNames27 orig_TestLoader_sortTestMethodsUsing = TestLoader.sortTestMethodsUsing28 TestLoader.sortTestMethodsUsing = testorder_cmp29def restore_default_ordering():...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?