How to use _extend_test_list method in avocado

Best Python code snippet using avocado_python

safeloader.py

Source:safeloader.py Github

copy

Full Screen

...321 if 'recursive' in directives:322 return True323 # Still not decided, try inheritance324 return module.is_matching_klass(klass)325def _extend_test_list(current, new):326 for test in new:327 test_method_name = test[0]328 if test_method_name not in [_[0] for _ in current]:329 current.append(test)330def _examine_class(path, class_name, match, target_module, target_class,331 determine_match):332 """333 Examine a class from a given path334 :param path: path to a Python source code file335 :type path: str336 :param class_name: the specific class to be found337 :type path: str338 :param match: whether the inheritance from <target_module.target_class> has339 been determined or not340 :type match: bool341 :param target_module: the module name under which the target_class lives342 :type target_module: str343 :param target_class: the name of the class that class_name should344 ultimately inherit from345 :type target_class: str346 :param determine_match: a callable that will determine if a match has347 occurred or not348 :type determine_match: function349 :returns: tuple where first item is a list of test methods detected350 for given class; second item is set of class names which351 look like avocado tests but are force-disabled.352 :rtype: tuple353 """354 module = PythonModule(path, target_module, target_class)355 info = []356 disabled = set()357 for klass in module.iter_classes():358 if class_name != klass.name:359 continue360 docstring = ast.get_docstring(klass)361 if match is False:362 match = determine_match(module, klass, docstring)363 info = get_methods_info(klass.body,364 get_docstring_directives_tags(docstring),365 get_docstring_directives_requirements(366 docstring))367 # Getting the list of parents of the current class368 parents = klass.bases369 # From this point we use `_$variable` to name temporary returns370 # from method calls that are to-be-assigned/combined with the371 # existing `$variable`.372 # Searching the parents in the same module373 for parent in parents[:]:374 # Looking for a 'class FooTest(Parent)'375 if not isinstance(parent, ast.Name):376 # 'class FooTest(bar.Bar)' not supported withing377 # a module378 continue379 parent_class = parent.id380 _info, _disabled, _match = _examine_class(module.path, parent_class,381 match, target_module,382 target_class,383 determine_match)384 if _info:385 parents.remove(parent)386 _extend_test_list(info, _info)387 disabled.update(_disabled)388 if _match is not match:389 match = _match390 # If there are parents left to be discovered, they391 # might be in a different module.392 for parent in parents:393 if hasattr(parent, 'value'):394 if hasattr(parent.value, 'id'):395 # We know 'parent.Class' or 'asparent.Class' and need396 # to get path and original_module_name. Class is given397 # by parent definition.398 _parent = module.imported_objects.get(parent.value.id)399 if _parent is None:400 # We can't examine this parent (probably broken401 # module)402 continue403 parent_path = os.path.dirname(_parent)404 parent_module = os.path.basename(_parent)405 parent_class = parent.attr406 else:407 # We don't support multi-level 'parent.parent.Class'408 continue409 else:410 # We only know 'Class' or 'AsClass' and need to get411 # path, module and original class_name412 _parent = module.imported_objects.get(parent.id)413 if _parent is None:414 # We can't examine this parent (probably broken415 # module)416 continue417 parent_path, parent_module, parent_class = (418 _parent.rsplit(os.path.sep, 2))419 modules_paths = [parent_path,420 os.path.dirname(module.path)] + sys.path421 found_spec = PathFinder.find_spec(parent_module, modules_paths)422 if found_spec is not None:423 _info, _disabled, _match = _examine_class(found_spec.origin,424 parent_class,425 match,426 target_module,427 target_class,428 _determine_match_avocado)429 if _info:430 _extend_test_list(info, _info)431 disabled.update(_disabled)432 if _match is not match:433 match = _match434 return info, disabled, match435def find_python_tests(module_name, class_name, determine_match, path):436 """437 Attempts to find Python tests from source files438 A Python test in this context is a method within a specific type439 of class (or that inherits from a specific class).440 :param module_name: the name of the module from which a class should441 have come from442 :type module_name: str443 :param class_name: the name of the class that is considered to contain444 test methods445 :type class_name: str446 :type determine_match: a callable that will determine if a given module447 and class is contains valid Python tests448 :type determine_match: function449 :param path: path to a Python source code file450 :type path: str451 :returns: tuple where first item is dict with class name and additional452 info such as method names and tags; the second item is453 set of class names which look like Python tests but have been454 forcefully disabled.455 :rtype: tuple456 """457 module = PythonModule(path, module_name, class_name)458 # The resulting test classes459 result = collections.OrderedDict()460 disabled = set()461 for klass in module.iter_classes():462 docstring = ast.get_docstring(klass)463 # Looking for a class that has in the docstring either464 # ":avocado: enable" or ":avocado: disable465 if check_docstring_directive(docstring, 'disable'):466 disabled.add(klass.name)467 continue468 if check_docstring_directive(docstring, 'enable'):469 info = get_methods_info(klass.body,470 get_docstring_directives_tags(docstring),471 get_docstring_directives_requirements(472 docstring))473 result[klass.name] = info474 continue475 # From this point onwards we want to do recursive discovery, but476 # for now we don't know whether it is avocado.Test inherited477 # (Ifs are optimized for readability, not speed)478 # If "recursive" tag is specified, it is forced as test479 if check_docstring_directive(docstring, 'recursive'):480 is_valid_test = True481 else:482 is_valid_test = module.is_matching_klass(klass)483 info = get_methods_info(klass.body,484 get_docstring_directives_tags(docstring),485 get_docstring_directives_requirements(486 docstring))487 _disabled = set()488 # Getting the list of parents of the current class489 parents = klass.bases490 # Searching the parents in the same module491 for parent in parents[:]:492 # Looking for a 'class FooTest(Parent)'493 if not isinstance(parent, ast.Name):494 # 'class FooTest(bar.Bar)' not supported withing495 # a module496 continue497 parent_class = parent.id498 _info, _dis, _python_test = _examine_class(module.path,499 parent_class,500 is_valid_test,501 module_name,502 class_name,503 determine_match)504 if _info:505 parents.remove(parent)506 _extend_test_list(info, _info)507 _disabled.update(_dis)508 if _python_test is not is_valid_test:509 is_valid_test = _python_test510 # If there are parents left to be discovered, they511 # might be in a different module.512 for parent in parents:513 if hasattr(parent, 'value'):514 if hasattr(parent.value, 'id'):515 # We know 'parent.Class' or 'asparent.Class' and need516 # to get path and original_module_name. Class is given517 # by parent definition.518 _parent = module.imported_objects.get(parent.value.id)519 if _parent is None:520 # We can't examine this parent (probably broken...

Full Screen

Full Screen

core.py

Source:core.py Github

copy

Full Screen

...36 methods = [method for method, _, _ in methods_info]37 if st.name not in methods:38 methods_info.append((st.name, mt_tags, mt_dependencies))39 return methods_info40def _extend_test_list(current, new):41 for test in new:42 test_method_name = test[0]43 if test_method_name not in [_[0] for _ in current]:44 current.append(test)45def _examine_same_module(46 parents, info, disabled, match, module, target_module, target_class, determine_match47):48 # Searching the parents in the same module49 for parent in parents[:]:50 # Looking for a 'class FooTest(Parent)'51 if not isinstance(parent, ast.Name):52 # 'class FooTest(bar.Bar)' not supported within53 # a module54 continue55 parent_class = parent.id56 # From this point we use `_$variable` to name temporary returns57 # from method calls that are to-be-assigned/combined with the58 # existing `$variable`.59 _info, _disable, _match = _examine_class(60 target_module,61 target_class,62 determine_match,63 module.path,64 parent_class,65 match,66 )67 if _info:68 parents.remove(parent)69 _extend_test_list(info, _info)70 disabled.update(_disable)71 if _match is not match:72 match = _match73 return match74class ClassNotSuitable(Exception):75 """Exception raised when examination of a class should not proceed."""76def _get_attributes_for_further_examination(parent, module):77 """Returns path, module and class for further examination.78 This looks at one of the parents of an interesting class, so for the79 example class Test below:80 >>> class Test(unittest.TestCase, MixIn):81 >>> pass82 This function should be called twice: once for unittest.TestCase,83 and once for MixIn.84 :param parent: parent is one of the possibly many parents from85 which the class being examined inherits from.86 :type parent: :class:`ast.Attribute`87 :param module: PythonModule instance with information about the88 module being inspected89 :type module: :class:`avocado.core.safeloader.module.PythonModule`90 :raises: ClassNotSuitable91 :returns: a tuple with three values: the class name, the imported92 symbol instance matching the further examination step,93 and a hint about the symbol name also being a module.94 :rtype: tuple of (str,95 :class:`avocado.core.safeloader.imported.ImportedSymbol`,96 bool)97 """98 if hasattr(parent, "value"):99 # A "value" in an "attribute" in this context means that100 # there's a "module.class" notation. It may be called that101 # way, because it resembles "class" being an attribute of the102 # "module" object. In short, if "parent" has a "value"103 # attribute, it means that this is given as a104 # "module.parent_class" notation, meaning that:105 # - parent is a module106 # - parent.value *should* be a class, because there's107 # currently no support for the "module.module.parent_class"108 # notation. See issue #4706.109 klass = parent.value110 if not hasattr(klass, "id"):111 # We don't support multi-level 'parent.parent.Class'112 raise ClassNotSuitable113 else:114 # We know 'parent.Class' or 'asparent.Class' and need115 # to get path and original_module_name. Class is given116 # by parent definition.117 imported_symbol = module.imported_symbols.get(klass.id)118 if imported_symbol is None:119 # We can't examine this parent (probably broken module)120 raise ClassNotSuitable121 # We currently don't support classes whose parents are generics122 if isinstance(parent, ast.Subscript):123 raise ClassNotSuitable124 parent_class = parent.attr125 # Special situation: in this case, because we know the parent126 # class is given as, module.class notation, we know what the127 # module name is. The imported symbol, because of its knowledge128 # *only* about the imports, and not about the class definitions,129 # can not tell if an import is a "from module import other_module"130 # or a "from module import class"131 symbol_is_module = klass.id == imported_symbol.symbol_name132 else:133 # We only know 'Class' or 'AsClass' and need to get134 # path, module and original class_name135 klass = parent.id136 imported_symbol = module.imported_symbols.get(klass)137 if imported_symbol is None:138 # We can't examine this parent (probably broken module)139 raise ClassNotSuitable140 parent_class = imported_symbol.symbol141 symbol_is_module = False142 return parent_class, imported_symbol, symbol_is_module143def _find_import_match(parent_path, parent_module):144 """Attempts to find an importable module."""145 modules_paths = [parent_path] + sys.path146 found_spec = PathFinder.find_spec(parent_module, modules_paths)147 if found_spec is None:148 raise ClassNotSuitable149 return found_spec150def _examine_class(151 target_module, target_class, determine_match, path, class_name, match152):153 """154 Examine a class from a given path155 :param target_module: the name of the module from which a class should156 have come from. When attempting to find a Python157 unittest, the target_module will most probably158 be "unittest", as per the standard library module159 name. When attempting to find Avocado tests, the160 target_module will most probably be "avocado".161 :type target_module: str162 :param target_class: the name of the class that is considered to contain163 test methods. When attempting to find Python164 unittests, the target_class will most probably be165 "TestCase". When attempting to find Avocado tests,166 the target_class will most probably be "Test".167 :type target_class: str168 :param determine_match: a callable that will determine if a match has169 occurred or not170 :type determine_match: function171 :param path: path to a Python source code file172 :type path: str173 :param class_name: the specific class to be found174 :type path: str175 :param match: whether the inheritance from <target_module.target_class> has176 been determined or not177 :type match: bool178 :returns: tuple where first item is a list of test methods detected179 for given class; second item is set of class names which180 look like avocado tests but are force-disabled.181 :rtype: tuple182 """183 module = PythonModule(path, target_module, target_class)184 info = []185 disabled = set()186 for klass in module.iter_classes(class_name):187 if class_name != klass.name:188 continue189 docstring = ast.get_docstring(klass)190 if match is False:191 match = module.is_matching_klass(klass)192 info = get_methods_info(193 klass.body,194 get_docstring_directives_tags(docstring),195 get_docstring_directives_dependencies(docstring),196 )197 # Getting the list of parents of the current class198 parents = klass.bases199 match = _examine_same_module(200 parents,201 info,202 disabled,203 match,204 module,205 target_module,206 target_class,207 determine_match,208 )209 # If there are parents left to be discovered, they210 # might be in a different module.211 for parent in parents:212 try:213 (214 parent_class,215 imported_symbol,216 symbol_is_module,217 ) = _get_attributes_for_further_examination(parent, module)218 found_spec = imported_symbol.get_importable_spec(symbol_is_module)219 if found_spec is None:220 continue221 except ClassNotSuitable:222 continue223 _info, _disabled, _match = _examine_class(224 target_module,225 target_class,226 determine_match,227 found_spec.origin,228 parent_class,229 match,230 )231 if _info:232 _extend_test_list(info, _info)233 disabled.update(_disabled)234 if _match is not match:235 match = _match236 if not match and module.interesting_klass_found:237 imported_symbol = module.imported_symbols[class_name]238 if imported_symbol:239 found_spec = imported_symbol.get_importable_spec()240 if found_spec:241 _info, _disabled, _match = _examine_class(242 target_module,243 target_class,244 determine_match,245 found_spec.origin,246 class_name,247 match,248 )249 if _info:250 _extend_test_list(info, _info)251 disabled.update(_disabled)252 if _match is not match:253 match = _match254 return info, disabled, match255def find_python_tests(target_module, target_class, determine_match, path):256 """257 Attempts to find Python tests from source files258 A Python test in this context is a method within a specific type259 of class (or that inherits from a specific class).260 :param target_module: the name of the module from which a class should261 have come from. When attempting to find a Python262 unittest, the target_module will most probably263 be "unittest", as per the standard library module264 name. When attempting to find Avocado tests, the...

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:

YouTube

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

Run avocado 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?

Helpful

NotHelpful