Best Python code snippet using avocado_python
safeloader.py
Source:safeloader.py
...192#: test behavior in various ways193DOCSTRING_DIRECTIVE_RE_RAW = r'\s*:avocado:[ \t]+(([a-zA-Z0-9]+?[a-zA-Z0-9_:,\=\-\.]*)|(r[a-zA-Z0-9]+?[a-zA-Z0-9_:,\=\{\}\"\-\.\/ ]*))\s*$'194# the RE will match `:avocado: tags=category` or `:avocado: requirements={}`195DOCSTRING_DIRECTIVE_RE = re.compile(DOCSTRING_DIRECTIVE_RE_RAW)196def get_docstring_directives(docstring):197 """198 Returns the values of the avocado docstring directives199 :param docstring: the complete text used as documentation200 :type docstring: str201 :rtype: builtin.list202 """203 result = []204 if docstring is None:205 return result206 for line in docstring.splitlines():207 try:208 match = DOCSTRING_DIRECTIVE_RE.match(line)209 if match:210 result.append(match.groups()[0])211 except TypeError:212 pass213 return result214def check_docstring_directive(docstring, directive):215 """216 Checks if there's a given directive in a given docstring217 :rtype: bool218 """219 return directive in get_docstring_directives(docstring)220def get_docstring_directives_tags(docstring):221 """222 Returns the test categories based on a `:avocado: tags=category`223 docstring224 :rtype: dict225 """226 result = {}227 for item in get_docstring_directives(docstring):228 if item.startswith('tags='):229 _, comma_tags = item.split('tags=', 1)230 for tag in comma_tags.split(','):231 if not tag:232 continue233 if ':' in tag:234 key, val = tag.split(':', 1)235 if key in result:236 result[key].add(val)237 else:238 result[key] = set([val])239 else:240 result[tag] = None241 return result242def get_docstring_directives_requirements(docstring):243 """244 Returns the test requirements from docstring patterns like245 `:avocado: requirement={}`.246 :rtype: list247 """248 requirements = []249 for item in get_docstring_directives(docstring):250 if item.startswith('requirement='):251 _, requirement_str = item.split('requirement=', 1)252 try:253 requirements.append(json.loads(requirement_str))254 except json.decoder.JSONDecodeError:255 # ignore requirement in case of malformed dictionary256 continue257 return requirements258def find_class_and_methods(path, method_pattern=None, base_class=None):259 """260 Attempts to find methods names from a given Python source file261 :param path: path to a Python source code file262 :type path: str263 :param method_pattern: compiled regex to match against method name264 :param base_class: only consider classes that inherit from a given265 base class (or classes that inherit from any class266 if None is given)267 :type base_class: str or None268 :returns: an ordered dictionary with classes as keys and methods as values269 :rtype: collections.OrderedDict270 """271 def inherits_from_base_class(class_statement, base_class_name):272 base_ids = [base.id for base in class_statement.bases273 if hasattr(base, 'id')]274 return base_class_name in base_ids275 result = collections.OrderedDict()276 with open(path) as source_file:277 mod = ast.parse(source_file.read(), path)278 for statement in mod.body:279 if isinstance(statement, ast.ClassDef):280 if base_class is not None:281 if not inherits_from_base_class(statement,282 base_class):283 continue284 if method_pattern is None:285 methods = [st.name for st in statement.body if286 isinstance(st, ast.FunctionDef)]287 methods = data_structures.ordered_list_unique(methods)288 else:289 methods = [st.name for st in statement.body if290 isinstance(st, ast.FunctionDef) and291 method_pattern.match(st.name)]292 methods = data_structures.ordered_list_unique(methods)293 result[statement.name] = methods294 return result295def get_methods_info(statement_body, class_tags, class_requirements):296 """297 Returns information on an Avocado instrumented test method298 """299 methods_info = []300 for st in statement_body:301 if (isinstance(st, ast.FunctionDef) and302 st.name.startswith('test')):303 docstring = ast.get_docstring(st)304 mt_tags = get_docstring_directives_tags(docstring)305 mt_tags.update(class_tags)306 mt_requirements = get_docstring_directives_requirements(docstring)307 mt_requirements.extend(class_requirements)308 methods = [method for method, _, _ in methods_info]309 if st.name not in methods:310 methods_info.append((st.name, mt_tags, mt_requirements))311 return methods_info312def _determine_match_avocado(module, klass, docstring):313 """314 Implements the match check for Avocado Instrumented Tests315 """316 directives = get_docstring_directives(docstring)317 if 'disable' in directives:318 return True319 if 'enable' in directives:320 return True321 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,...
test_safeloader_docstring.py
Source:test_safeloader_docstring.py
...61 "mention avocado: it's awesome, but that was not a "62 "directive. a tag would be something line this: "63 ":avocado: enable"64 )65 self.assertIsNotNone(get_docstring_directives(docstring))66 def test_newlines(self):67 docstring = (68 "\n\n\nThis is a docstring with many new\n\nlines "69 "followed by an avocado tag\n"70 "\n\n:avocado: enable\n\n"71 )72 self.assertIsNotNone(get_docstring_directives(docstring))73 def test_enabled(self):74 self.assertTrue(check_docstring_directive(":avocado: enable", "enable"))75 self.assertTrue(check_docstring_directive(":avocado:\tenable", "enable"))76 self.assertTrue(77 check_docstring_directive(":avocado: enable\n:avocado: tags=fast", "enable")78 )79 self.assertFalse(check_docstring_directive(":AVOCADO: ENABLE", "enable"))80 self.assertFalse(check_docstring_directive(":avocado: enabled", "enable"))81 def test_disabled(self):82 self.assertTrue(check_docstring_directive(":avocado: disable", "disable"))83 self.assertTrue(check_docstring_directive(":avocado:\tdisable", "disable"))84 self.assertFalse(check_docstring_directive(":AVOCADO: DISABLE", "disable"))85 self.assertFalse(check_docstring_directive(":avocado: disabled", "disable"))86 def test_get_tags_empty(self):...
docstring.py
Source:docstring.py
...6 r"\s*:avocado:[ \t]+(([a-zA-Z0-9]+?[a-zA-Z0-9_:,\=\-\.]*)|(dependency={.*}))\s*$"7)8# the RE will match `:avocado: tags=category` or `:avocado: dependency={}`9DOCSTRING_DIRECTIVE_RE = re.compile(DOCSTRING_DIRECTIVE_RE_RAW)10def get_docstring_directives(docstring):11 """12 Returns the values of the avocado docstring directives13 :param docstring: the complete text used as documentation14 :type docstring: str15 :rtype: builtin.list16 """17 result = []18 if docstring is None:19 return result20 for line in docstring.splitlines():21 try:22 match = DOCSTRING_DIRECTIVE_RE.match(line)23 if match:24 result.append(match.groups()[0])25 except TypeError:26 pass27 return result28def check_docstring_directive(docstring, directive):29 """30 Checks if there's a given directive in a given docstring31 :rtype: bool32 """33 return directive in get_docstring_directives(docstring)34def get_docstring_directives_tags(docstring):35 """36 Returns the test categories based on a `:avocado: tags=category`37 docstring38 :rtype: dict39 """40 result = {}41 for item in get_docstring_directives(docstring):42 if item.startswith("tags="):43 _, comma_tags = item.split("tags=", 1)44 for tag in comma_tags.split(","):45 if not tag:46 continue47 if ":" in tag:48 key, val = tag.split(":", 1)49 if key in result:50 result[key].add(val)51 else:52 result[key] = set([val])53 else:54 result[tag] = None55 return result56def get_docstring_directives_dependencies(docstring):57 """58 Returns the test dependencies from docstring patterns like59 `:avocado: dependencies={}`.60 :rtype: list61 """62 dependencies = []63 for item in get_docstring_directives(docstring):64 if item.startswith("dependency="):65 _, dependency_str = item.split("dependency=", 1)66 try:67 dependencies.append(json.loads(dependency_str))68 except json.decoder.JSONDecodeError:69 # ignore dependencies in case of malformed dictionary70 continue...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!