How to use _examine_same_module method in avocado

Best Python code snippet using avocado_python Github


Full Screen

...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( 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 = == 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 != 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, the265 target_module will most probably be "avocado".266 :type target_module: str267 :param target_class: the name of the class that is considered to contain268 test methods. When attempting to find Python269 unittests, the target_class will most probably be270 "TestCase". When attempting to find Avocado tests,271 the target_class will most probably be "Test".272 :type target_class: str273 :type determine_match: a callable that will determine if a given module274 and class is contains valid Python tests275 :type determine_match: function276 :param path: path to a Python source code file277 :type path: str278 :returns: tuple where first item is dict with class name and additional279 info such as method names and tags; the second item is280 set of class names which look like Python tests but have been281 forcefully disabled.282 :rtype: tuple283 """284 module = PythonModule(path, target_module, target_class)285 # The resulting test classes286 result = collections.OrderedDict()287 disabled = set()288 for klass in module.iter_classes():289 docstring = ast.get_docstring(klass)290 # Looking for a class that has in the docstring either291 # ":avocado: enable" or ":avocado: disable292 if check_docstring_directive(docstring, "disable"):293 disabled.add( continue295 if check_docstring_directive(docstring, "enable"):296 info = get_methods_info(297 klass.body,298 get_docstring_directives_tags(docstring),299 get_docstring_directives_dependencies(docstring),300 )301 result[] = info302 continue303 # From this point onwards we want to do recursive discovery, but304 # for now we don't know whether it is avocado.Test inherited305 # (Ifs are optimized for readability, not speed)306 # If "recursive" tag is specified, it is forced as test307 if check_docstring_directive(docstring, "recursive"):308 match = True309 else:310 match = module.is_matching_klass(klass)311 info = get_methods_info(312 klass.body,313 get_docstring_directives_tags(docstring),314 get_docstring_directives_dependencies(docstring),315 )316 # Getting the list of parents of the current class317 parents = klass.bases318 match = _examine_same_module(319 parents,320 info,321 disabled,322 match,323 module,324 target_module,325 target_class,326 determine_match,327 )328 # If there are parents left to be discovered, they329 # might be in a different module.330 for parent in parents:331 try:332 (...

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