Best Python code snippet using autotest_python
coverage.py
Source:coverage.py
...93 94 def doCode(self, node):95 if hasattr(node, 'decorators') and node.decorators:96 self.dispatch(node.decorators)97 self.doSuite(node, node.code)98 99 visitFunction = visitClass = doCode100 def getFirstLine(self, node):101 # Find the first line in the tree node.102 lineno = node.lineno103 for n in node.getChildNodes():104 f = self.getFirstLine(n)105 if lineno and f:106 lineno = min(lineno, f)107 else:108 lineno = lineno or f109 return lineno110 def getLastLine(self, node):111 # Find the first line in the tree node.112 lineno = node.lineno113 for n in node.getChildNodes():114 lineno = max(lineno, self.getLastLine(n))115 return lineno116 117 def doStatement(self, node):118 self.recordLine(self.getFirstLine(node))119 visitAssert = visitAssign = visitAssTuple = visitDiscard = visitPrint = \120 visitPrintnl = visitRaise = visitSubscript = \121 visitDecorators = \122 doStatement123 124 def recordNodeLine(self, node):125 return self.recordLine(node.lineno)126 127 def recordLine(self, lineno):128 # Returns a bool, whether the line is included or excluded.129 if lineno:130 # Multi-line tests introducing suites have to get charged to their131 # keyword.132 if lineno in self.suite_spots:133 lineno = self.suite_spots[lineno][0]134 # If we're inside an exluded suite, record that this line was135 # excluded.136 if self.excluding_suite:137 self.excluded[lineno] = 1138 return 0139 # If this line is excluded, or suite_spots maps this line to140 # another line that is exlcuded, then we're excluded.141 elif self.excluded.has_key(lineno) or \142 self.suite_spots.has_key(lineno) and \143 self.excluded.has_key(self.suite_spots[lineno][1]):144 return 0145 # Otherwise, this is an executable line.146 else:147 self.statements[lineno] = 1148 return 1149 return 0150 151 default = recordNodeLine152 153 def recordAndDispatch(self, node):154 self.recordNodeLine(node)155 self.dispatch(node)156 def doSuite(self, intro, body, exclude=0):157 exsuite = self.excluding_suite158 if exclude or (intro and not self.recordNodeLine(intro)):159 self.excluding_suite = 1160 self.recordAndDispatch(body)161 self.excluding_suite = exsuite162 163 def doPlainWordSuite(self, prevsuite, suite):164 # Finding the exclude lines for else's is tricky, because they aren't165 # present in the compiler parse tree. Look at the previous suite,166 # and find its last line. If any line between there and the else's167 # first line are excluded, then we exclude the else.168 lastprev = self.getLastLine(prevsuite)169 firstelse = self.getFirstLine(suite)170 for l in range(lastprev+1, firstelse):171 if self.suite_spots.has_key(l):172 self.doSuite(None, suite, exclude=self.excluded.has_key(l))173 break174 else:175 self.doSuite(None, suite)176 177 def doElse(self, prevsuite, node):178 if node.else_:179 self.doPlainWordSuite(prevsuite, node.else_)180 181 def visitFor(self, node):182 self.doSuite(node, node.body)183 self.doElse(node.body, node)184 def visitIf(self, node):185 # The first test has to be handled separately from the rest.186 # The first test is credited to the line with the "if", but the others187 # are credited to the line with the test for the elif.188 self.doSuite(node, node.tests[0][1])189 for t, n in node.tests[1:]:190 self.doSuite(t, n)191 self.doElse(node.tests[-1][1], node)192 def visitWhile(self, node):193 self.doSuite(node, node.body)194 self.doElse(node.body, node)195 def visitTryExcept(self, node):196 self.doSuite(node, node.body)197 for i in range(len(node.handlers)):198 a, b, h = node.handlers[i]199 if not a:200 # It's a plain "except:". Find the previous suite.201 if i > 0:202 prev = node.handlers[i-1][2]203 else:204 prev = node.body205 self.doPlainWordSuite(prev, h)206 else:207 self.doSuite(a, h)208 self.doElse(node.handlers[-1][2], node)209 210 def visitTryFinally(self, node):211 self.doSuite(node, node.body)212 self.doPlainWordSuite(node.body, node.final)213 214 def visitGlobal(self, node):215 # "global" statements don't execute like others (they don't call the216 # trace function), so don't record their line numbers.217 pass218the_coverage = None219class coverage:220 error = "coverage error"221 # Name of the cache file (unless environment variable is set).222 cache_default = ".coverage"223 # Environment variable naming the cache file.224 cache_env = "COVERAGE_FILE"225 # A map from canonical Python source file name to a dictionary in...
CovParse.py
Source:CovParse.py
...57 if hasattr(node, 'decorators') and node.decorators:58 self.dispatch(node.decorators)59 self.recordAndDispatch(node.code)60 else:61 self.doSuite(node, node.code)62 visitFunction = visitClass = doCode63 def getFirstLine(self, node):64 # Find the first line in the tree node.65 lineno = node.lineno66 for n in node.getChildNodes():67 f = self.getFirstLine(n)68 if lineno and f:69 lineno = min(lineno, f)70 else:71 lineno = lineno or f72 return lineno73 def getLastLine(self, node):74 # Find the first line in the tree node.75 lineno = node.lineno76 for n in node.getChildNodes():77 lineno = max(lineno, self.getLastLine(n))78 return lineno79 def doStatement(self, node):80 self.recordLine(self.getFirstLine(node))81 visitAssert = visitAssign = visitAssTuple = visitDiscard = visitPrint = \82 visitPrintnl = visitRaise = visitSubscript = visitDecorators = \83 doStatement84 def recordNodeLine(self, node):85 return self.recordLine(node.lineno)86 def recordLine(self, lineno):87 # Returns a bool, whether the line is included or excluded.88 # print("REC", lineno)89 if lineno:90 self.all_statements[lineno] = 191 # Multi-line tests introducing suites have to get charged to their92 # keyword.93 if lineno in self.suite_spots:94 lineno = self.suite_spots[lineno][0]95 # If we're inside an exluded suite, record that this line was96 # excluded.97 if self.excluding_suite:98 # print("EXCLUDE", lineno, self.excluding_suite)99 self.excluded[lineno] = self.excluding_suite100 return self.excluding_suite101 # If this line is excluded, or suite_spots maps this line to102 # another line that is excluded, then we're excluded.103 elif self.excluded.has_key(lineno):104 return self.excluded[lineno]105 elif self.suite_spots.has_key(lineno) and \106 self.excluded.has_key(self.suite_spots[lineno][1]):107 return self.excluded[self.suite_spots[lineno][1]]108 # Otherwise, this is an executable line.109 else:110 self.statements[lineno] = 1111 return 1112 return 0113 default = recordNodeLine114 def recordAndDispatch(self, node):115 self.recordNodeLine(node)116 self.dispatch(node)117 def doSuite(self, intro, body, exclude=0, qq=0):118 exsuite = self.excluding_suite119 exclTag = None120 # if not exclude and intro:121 if intro:122 # if qq:123 # print("SUITE", intro.lineno, body.__class__, len(body.nodes))124 exclTag = self.recordNodeLine(intro)125 if exclude:126 self.excluding_suite = exclude127 elif exclTag not in [None, 1, 0]:128 self.excluding_suite = exclTag129 # if qq or self.excluding_suite == 1:130 # print("EXCL-1", body.lineno, self.excluding_suite, exsuite, exclude, exclTag)131 self.recordAndDispatch(body)132 self.excluding_suite = exsuite133 # if qq:134 # print("EXCL-2", body.lineno, self.excluding_suite, exsuite)135 def doPlainWordSuite(self, prevsuite, suite):136 # Finding the exclude lines for else's is tricky, because they aren't137 # present in the compiler parse tree. Look at the previous suite,138 # and find its last line. If any line between there and the else's139 # first line are excluded, then we exclude the else.140 lastprev = self.getLastLine(prevsuite)141 firstelse = self.getFirstLine(suite)142 for l in range(lastprev+1, firstelse):143 if self.suite_spots.has_key(l):144 self.doSuite(None, suite, exclude=self.excluded.get(l, 0))145 break146 else:147 self.doSuite(None, suite)148 def doElse(self, prevsuite, node):149 if node.else_:150 self.doPlainWordSuite(prevsuite, node.else_)151 def visitFor(self, node):152 self.doSuite(node, node.body)153 self.doElse(node.body, node)154 def visitIf(self, node):155 # The first test has to be handled separately from the rest.156 # The first test is credited to the line with the "if", but the others157 # are credited to the line with the test for the elif.158 exsuite = self.excluding_suite159 exclTag = self.recordNodeLine(node)160 if exclTag not in [None, 1, 0]:161 self.excluding_suite = exclTag162 # print("IF", node.lineno, self.excluding_suite)163 # print("EXCL-XX", node.lineno, self.excluding_suite, exsuite)164 self.doSuite(node, node.tests[0][1])165 for t, n in node.tests[1:]:166 self.doSuite(t, n, qq=1)167 # print("EXCL-YY", node.lineno, self.excluding_suite, exsuite)168 self.doElse(node.tests[-1][1], node)169 # print("EXCL-ZZ", node.lineno, self.excluding_suite, exsuite)170 self.excluding_suite = exsuite171 def visitWhile(self, node):172 self.doSuite(node, node.body)173 self.doElse(node.body, node)174 def visitTryExcept(self, node):175 self.doSuite(node, node.body)176 for i in range(len(node.handlers)):177 a, b, h = node.handlers[i]178 if not a:179 # It's a plain "except:". Find the previous suite.180 if i > 0:181 prev = node.handlers[i-1][2]182 else:183 prev = node.body184 self.doPlainWordSuite(prev, h)185 else:186 self.doSuite(a, h)187 self.doElse(node.handlers[-1][2], node)188 def visitTryFinally(self, node):189 self.doSuite(node, node.body)190 self.doPlainWordSuite(node.body, node.final)191 def visitGlobal(self, node):192 # "global" statements don't execute like others (they don't call the193 # trace function), so don't record their line numbers.194 pass195def get_suite_spots(tree, spots):196 """ Analyze a parse tree to find suite introducers which span a number197 of lines.198 """199 import symbol, token200 for i in range(1, len(tree)):201 if type(tree[i]) == type(()):202 if tree[i][0] == symbol.suite:203 # Found a suite, look back for the colon and keyword....
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!!