How to use begin_recursive method in autotest

Best Python code snippet using autotest_python

coverage.py

Source:coverage.py Github

copy

Full Screen

...415 def exclude(self, re):416 if self.exclude_re:417 self.exclude_re += "|"418 self.exclude_re += "(" + re + ")"419 def begin_recursive(self):420 self.cstack.append(self.c)421 self.xstack.append(self.exclude_re)422 423 def end_recursive(self):424 self.c = self.cstack.pop()425 self.exclude_re = self.xstack.pop()426 # save(). Save coverage data to the coverage cache.427 def save(self):428 if self.usecache and self.cache:429 self.canonicalize_filenames()430 cache = open(self.cache, 'wb')431 import marshal432 marshal.dump(self.cexecuted, cache)433 cache.close()434 # restore(). Restore coverage data from the coverage cache (if it exists).435 def restore(self):436 self.c = {}437 self.cexecuted = {}438 assert self.usecache439 if os.path.exists(self.cache):440 self.cexecuted = self.restore_file(self.cache)441 def restore_file(self, file_name):442 try:443 cache = open(file_name, 'rb')444 import marshal445 cexecuted = marshal.load(cache)446 cache.close()447 if isinstance(cexecuted, types.DictType):448 return cexecuted449 else:450 return {}451 except:452 return {}453 # collect(). Collect data in multiple files produced by parallel mode454 def collect(self):455 cache_dir, local = os.path.split(self.cache)456 for f in os.listdir(cache_dir or '.'):457 if not f.startswith(local):458 continue459 full_path = os.path.join(cache_dir, f)460 cexecuted = self.restore_file(full_path)461 self.merge_data(cexecuted)462 def merge_data(self, new_data):463 for file_name, file_data in new_data.items():464 if self.cexecuted.has_key(file_name):465 self.merge_file_data(self.cexecuted[file_name], file_data)466 else:467 self.cexecuted[file_name] = file_data468 def merge_file_data(self, cache_data, new_data):469 for line_number in new_data.keys():470 if not cache_data.has_key(line_number):471 cache_data[line_number] = new_data[line_number]472 # canonical_filename(filename). Return a canonical filename for the473 # file (that is, an absolute path with no redundant components and474 # normalized case). See [GDR 2001-12-04b, 3.3].475 def canonical_filename(self, filename):476 if not self.canonical_filename_cache.has_key(filename):477 f = filename478 if os.path.isabs(f) and not os.path.exists(f):479 f = os.path.basename(f)480 if not os.path.isabs(f):481 for path in [os.curdir] + sys.path:482 g = os.path.join(path, f)483 if os.path.exists(g):484 f = g485 break486 cf = os.path.normcase(os.path.abspath(f))487 self.canonical_filename_cache[filename] = cf488 return self.canonical_filename_cache[filename]489 # canonicalize_filenames(). Copy results from "c" to "cexecuted", 490 # canonicalizing filenames on the way. Clear the "c" map.491 def canonicalize_filenames(self):492 for filename, lineno in self.c.keys():493 if filename == '<string>':494 # Can't do anything useful with exec'd strings, so skip them.495 continue496 f = self.canonical_filename(filename)497 if not self.cexecuted.has_key(f):498 self.cexecuted[f] = {}499 self.cexecuted[f][lineno] = 1500 self.c = {}501 # morf_filename(morf). Return the filename for a module or file.502 def morf_filename(self, morf):503 if isinstance(morf, types.ModuleType):504 if not hasattr(morf, '__file__'):505 raise CoverageException, "Module has no __file__ attribute."506 f = morf.__file__507 else:508 f = morf509 return self.canonical_filename(f)510 # analyze_morf(morf). Analyze the module or filename passed as511 # the argument. If the source code can't be found, raise an error.512 # Otherwise, return a tuple of (1) the canonical filename of the513 # source code for the module, (2) a list of lines of statements514 # in the source code, (3) a list of lines of excluded statements,515 # and (4), a map of line numbers to multi-line line number ranges, for516 # statements that cross lines.517 518 def analyze_morf(self, morf):519 if self.analysis_cache.has_key(morf):520 return self.analysis_cache[morf]521 filename = self.morf_filename(morf)522 ext = os.path.splitext(filename)[1]523 if ext == '.pyc':524 if not os.path.exists(filename[0:-1]):525 raise CoverageException, ("No source for compiled code '%s'."526 % filename)527 filename = filename[0:-1]528 elif ext != '.py':529 raise CoverageException, "File '%s' not Python source." % filename530 source = open(filename, 'r')531 lines, excluded_lines, line_map = self.find_executable_statements(532 source.read(), exclude=self.exclude_re533 )534 source.close()535 result = filename, lines, excluded_lines, line_map536 self.analysis_cache[morf] = result537 return result538 def first_line_of_tree(self, tree):539 while True:540 if len(tree) == 3 and type(tree[2]) == type(1):541 return tree[2]542 tree = tree[1]543 544 def last_line_of_tree(self, tree):545 while True:546 if len(tree) == 3 and type(tree[2]) == type(1):547 return tree[2]548 tree = tree[-1]549 550 def find_docstring_pass_pair(self, tree, spots):551 for i in range(1, len(tree)):552 if self.is_string_constant(tree[i]) and self.is_pass_stmt(tree[i+1]):553 first_line = self.first_line_of_tree(tree[i])554 last_line = self.last_line_of_tree(tree[i+1])555 self.record_multiline(spots, first_line, last_line)556 557 def is_string_constant(self, tree):558 try:559 return tree[0] == symbol.stmt and tree[1][1][1][0] == symbol.expr_stmt560 except:561 return False562 563 def is_pass_stmt(self, tree):564 try:565 return tree[0] == symbol.stmt and tree[1][1][1][0] == symbol.pass_stmt566 except:567 return False568 def record_multiline(self, spots, i, j):569 for l in range(i, j+1):570 spots[l] = (i, j)571 572 def get_suite_spots(self, tree, spots):573 """ Analyze a parse tree to find suite introducers which span a number574 of lines.575 """576 for i in range(1, len(tree)):577 if type(tree[i]) == type(()):578 if tree[i][0] == symbol.suite:579 # Found a suite, look back for the colon and keyword.580 lineno_colon = lineno_word = None581 for j in range(i-1, 0, -1):582 if tree[j][0] == token.COLON:583 # Colons are never executed themselves: we want the584 # line number of the last token before the colon.585 lineno_colon = self.last_line_of_tree(tree[j-1])586 elif tree[j][0] == token.NAME:587 if tree[j][1] == 'elif':588 # Find the line number of the first non-terminal589 # after the keyword.590 t = tree[j+1]591 while t and token.ISNONTERMINAL(t[0]):592 t = t[1]593 if t:594 lineno_word = t[2]595 else:596 lineno_word = tree[j][2]597 break598 elif tree[j][0] == symbol.except_clause:599 # "except" clauses look like:600 # ('except_clause', ('NAME', 'except', lineno), ...)601 if tree[j][1][0] == token.NAME:602 lineno_word = tree[j][1][2]603 break604 if lineno_colon and lineno_word:605 # Found colon and keyword, mark all the lines606 # between the two with the two line numbers.607 self.record_multiline(spots, lineno_word, lineno_colon)608 # "pass" statements are tricky: different versions of Python609 # treat them differently, especially in the common case of a610 # function with a doc string and a single pass statement.611 self.find_docstring_pass_pair(tree[i], spots)612 613 elif tree[i][0] == symbol.simple_stmt:614 first_line = self.first_line_of_tree(tree[i])615 last_line = self.last_line_of_tree(tree[i])616 if first_line != last_line:617 self.record_multiline(spots, first_line, last_line)618 self.get_suite_spots(tree[i], spots)619 def find_executable_statements(self, text, exclude=None):620 # Find lines which match an exclusion pattern.621 excluded = {}622 suite_spots = {}623 if exclude:624 reExclude = re.compile(exclude)625 lines = text.split('\n')626 for i in range(len(lines)):627 if reExclude.search(lines[i]):628 excluded[i+1] = 1629 # Parse the code and analyze the parse tree to find out which statements630 # are multiline, and where suites begin and end.631 import parser632 tree = parser.suite(text+'\n\n').totuple(1)633 self.get_suite_spots(tree, suite_spots)634 #print "Suite spots:", suite_spots635 636 # Use the compiler module to parse the text and find the executable637 # statements. We add newlines to be impervious to final partial lines.638 statements = {}639 ast = compiler.parse(text+'\n\n')640 visitor = StatementFindingAstVisitor(statements, excluded, suite_spots)641 compiler.walk(ast, visitor, walker=visitor)642 lines = statements.keys()643 lines.sort()644 excluded_lines = excluded.keys()645 excluded_lines.sort()646 return lines, excluded_lines, suite_spots647 # format_lines(statements, lines). Format a list of line numbers648 # for printing by coalescing groups of lines as long as the lines649 # represent consecutive statements. This will coalesce even if650 # there are gaps between statements, so if statements =651 # [1,2,3,4,5,10,11,12,13,14] and lines = [1,2,5,10,11,13,14] then652 # format_lines will return "1-2, 5-11, 13-14".653 def format_lines(self, statements, lines):654 pairs = []655 i = 0656 j = 0657 start = None658 pairs = []659 while i < len(statements) and j < len(lines):660 if statements[i] == lines[j]:661 if start == None:662 start = lines[j]663 end = lines[j]664 j = j + 1665 elif start:666 pairs.append((start, end))667 start = None668 i = i + 1669 if start:670 pairs.append((start, end))671 def stringify(pair):672 start, end = pair673 if start == end:674 return "%d" % start675 else:676 return "%d-%d" % (start, end)677 ret = string.join(map(stringify, pairs), ", ")678 return ret679 # Backward compatibility with version 1.680 def analysis(self, morf):681 f, s, _, m, mf = self.analysis2(morf)682 return f, s, m, mf683 def analysis2(self, morf):684 filename, statements, excluded, line_map = self.analyze_morf(morf)685 self.canonicalize_filenames()686 if not self.cexecuted.has_key(filename):687 self.cexecuted[filename] = {}688 missing = []689 for line in statements:690 lines = line_map.get(line, [line, line])691 for l in range(lines[0], lines[1]+1):692 if self.cexecuted[filename].has_key(l):693 break694 else:695 missing.append(line)696 return (filename, statements, excluded, missing,697 self.format_lines(statements, missing))698 def relative_filename(self, filename):699 """ Convert filename to relative filename from self.relative_dir.700 """701 return filename.replace(self.relative_dir, "")702 def morf_name(self, morf):703 """ Return the name of morf as used in report.704 """705 if isinstance(morf, types.ModuleType):706 return morf.__name__707 else:708 return self.relative_filename(os.path.splitext(morf)[0])709 def filter_by_prefix(self, morfs, omit_prefixes):710 """ Return list of morfs where the morf name does not begin711 with any one of the omit_prefixes.712 """713 filtered_morfs = []714 for morf in morfs:715 for prefix in omit_prefixes:716 if self.morf_name(morf).startswith(prefix):717 break718 else:719 filtered_morfs.append(morf)720 return filtered_morfs721 def morf_name_compare(self, x, y):722 return cmp(self.morf_name(x), self.morf_name(y))723 def report(self, morfs, show_missing=1, ignore_errors=0, file=None, omit_prefixes=[]):724 if not isinstance(morfs, types.ListType):725 morfs = [morfs]726 # On windows, the shell doesn't expand wildcards. Do it here.727 globbed = []728 for morf in morfs:729 if isinstance(morf, strclass):730 globbed.extend(glob.glob(morf))731 else:732 globbed.append(morf)733 morfs = globbed734 735 morfs = self.filter_by_prefix(morfs, omit_prefixes)736 morfs.sort(self.morf_name_compare)737 max_name = max([5,] + map(len, map(self.morf_name, morfs)))738 fmt_name = "%%- %ds " % max_name739 fmt_err = fmt_name + "%s: %s"740 header = fmt_name % "Name" + " Stmts Exec Cover"741 fmt_coverage = fmt_name + "% 6d % 6d % 5d%%"742 if show_missing:743 header = header + " Missing"744 fmt_coverage = fmt_coverage + " %s"745 if not file:746 file = sys.stdout747 print >>file, header748 print >>file, "-" * len(header)749 total_statements = 0750 total_executed = 0751 for morf in morfs:752 name = self.morf_name(morf)753 try:754 _, statements, _, missing, readable = self.analysis2(morf)755 n = len(statements)756 m = n - len(missing)757 if n > 0:758 pc = 100.0 * m / n759 else:760 pc = 100.0761 args = (name, n, m, pc)762 if show_missing:763 args = args + (readable,)764 print >>file, fmt_coverage % args765 total_statements = total_statements + n766 total_executed = total_executed + m767 except KeyboardInterrupt: #pragma: no cover768 raise769 except:770 if not ignore_errors:771 typ, msg = sys.exc_info()[0:2]772 print >>file, fmt_err % (name, typ, msg)773 if len(morfs) > 1:774 print >>file, "-" * len(header)775 if total_statements > 0:776 pc = 100.0 * total_executed / total_statements777 else:778 pc = 100.0779 args = ("TOTAL", total_statements, total_executed, pc)780 if show_missing:781 args = args + ("",)782 print >>file, fmt_coverage % args783 # annotate(morfs, ignore_errors).784 blank_re = re.compile(r"\s*(#|$)")785 else_re = re.compile(r"\s*else\s*:\s*(#|$)")786 def annotate(self, morfs, directory=None, ignore_errors=0, omit_prefixes=[]):787 morfs = self.filter_by_prefix(morfs, omit_prefixes)788 for morf in morfs:789 try:790 filename, statements, excluded, missing, _ = self.analysis2(morf)791 self.annotate_file(filename, statements, excluded, missing, directory)792 except KeyboardInterrupt:793 raise794 except:795 if not ignore_errors:796 raise797 798 def annotate_file(self, filename, statements, excluded, missing, directory=None):799 source = open(filename, 'r')800 if directory:801 dest_file = os.path.join(directory,802 os.path.basename(filename)803 + ',cover')804 else:805 dest_file = filename + ',cover'806 dest = open(dest_file, 'w')807 lineno = 0808 i = 0809 j = 0810 covered = 1811 while 1:812 line = source.readline()813 if line == '':814 break815 lineno = lineno + 1816 while i < len(statements) and statements[i] < lineno:817 i = i + 1818 while j < len(missing) and missing[j] < lineno:819 j = j + 1820 if i < len(statements) and statements[i] == lineno:821 covered = j >= len(missing) or missing[j] > lineno822 if self.blank_re.match(line):823 dest.write(' ')824 elif self.else_re.match(line):825 # Special logic for lines containing only 'else:'. 826 # See [GDR 2001-12-04b, 3.2].827 if i >= len(statements) and j >= len(missing):828 dest.write('! ')829 elif i >= len(statements) or j >= len(missing):830 dest.write('> ')831 elif statements[i] == missing[j]:832 dest.write('! ')833 else:834 dest.write('> ')835 elif lineno in excluded:836 dest.write('- ')837 elif covered:838 dest.write('> ')839 else:840 dest.write('! ')841 dest.write(line)842 source.close()843 dest.close()844# Singleton object.845the_coverage = coverage()846# Module functions call methods in the singleton object.847def use_cache(*args, **kw): 848 return the_coverage.use_cache(*args, **kw)849def start(*args, **kw): 850 return the_coverage.start(*args, **kw)851def stop(*args, **kw): 852 return the_coverage.stop(*args, **kw)853def erase(*args, **kw): 854 return the_coverage.erase(*args, **kw)855def begin_recursive(*args, **kw): 856 return the_coverage.begin_recursive(*args, **kw)857def end_recursive(*args, **kw): 858 return the_coverage.end_recursive(*args, **kw)859def exclude(*args, **kw): 860 return the_coverage.exclude(*args, **kw)861def analysis(*args, **kw): 862 return the_coverage.analysis(*args, **kw)863def analysis2(*args, **kw): 864 return the_coverage.analysis2(*args, **kw)865def report(*args, **kw): 866 return the_coverage.report(*args, **kw)867def annotate(*args, **kw): 868 return the_coverage.annotate(*args, **kw)869def annotate_file(*args, **kw): 870 return the_coverage.annotate_file(*args, **kw)...

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

Helpful

NotHelpful