Best Python code snippet using locust
test_runners.py
Source:test_runners.py
...167 168 class MyLocust(Locust):169 class task_set(TaskSet):170 @task171 def will_error(self):172 raise HeyAnException(":(")173 174 runner = LocalLocustRunner([MyLocust], self.options)175 176 l = MyLocust()177 l._catch_exceptions = False178 179 self.assertRaises(HeyAnException, l.run)180 self.assertRaises(HeyAnException, l.run)181 self.assertEqual(1, len(runner.exceptions))182 183 hash_key, exception = runner.exceptions.popitem()184 self.assertTrue("traceback" in exception)185 self.assertTrue("HeyAnException" in exception["traceback"])186 self.assertEqual(2, exception["count"])187 188 def test_exception_is_catched(self):189 """ Test that exceptions are stored, and execution continues """190 class HeyAnException(Exception):191 pass192 193 class MyTaskSet(TaskSet):194 def __init__(self, *a, **kw):195 super(MyTaskSet, self).__init__(*a, **kw)196 self._task_queue = [197 {"callable":self.will_error, "args":[], "kwargs":{}}, 198 {"callable":self.will_stop, "args":[], "kwargs":{}},199 ]200 201 @task(1)202 def will_error(self):203 raise HeyAnException(":(")204 205 @task(1)206 def will_stop(self):207 self.interrupt()208 209 class MyLocust(Locust):210 min_wait = 10211 max_wait = 10212 task_set = MyTaskSet213 214 runner = LocalLocustRunner([MyLocust], self.options)215 l = MyLocust()216 ...
reg_ex.py
Source:reg_ex.py
1# reg ex module2import re3string = 'search inside of this text please!'4# re.search(pattern, string, flags)5a = re.search('this', string) # prints a Match object, which also gives a span of the index of occurance6# will_error returns None7## trying .group() on a NOne value will error out8will_error= re.search('tasdklfj', string)9# print(will_error.group())10print(a.span())11print(a.start())12print(a.end())13# when we need to do multiple seaches .group() is really useful14print(a.group())15# reg ex module16# establishing a pattern17pattern = re.compile('this')18full_match_pattern = re.compile('search this inside of this text please!'19)20match_pattern = re.compile('search this ')21string = 'search this inside of this text please!'22full_match_pattern = re.compile('search this inside of this text please!'23)24pattern_search = pattern.search(string)25find_all = pattern.findall(string)26will_error= pattern.search(string)27full_match = pattern.fullmatch(string)28full_match_true = full_match_pattern.fullmatch(string)29match_method = match_pattern.match(string)30print(pattern_search.span())31print(pattern_search.start())32print(pattern_search.end())33print(find_all)34# prints None since the string provided isn't a full match for the string we are checking against35print(full_match)36# prints a Match object since it is a full match37print(full_match_true)38# prints match because we don't require a FULL match - .match() doesnt' care what comes after it's match, does care aobut before39print(match_method)40email_pattern = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")41email_string = 'new@email.test'42email = email_pattern.search(email_string)43print(email)44# Password checker45## at least 8 characters46## letters, numbers, $%#@47def main(): 48 password = 'Geeky1y@1'49 reg = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$%#@])[A-Za-z\d@$!#%*?&]{8,20}$"50 51 # compiling regex 52 password_pattern = re.compile(reg) 53 54 # searching regex 55 password_match = re.fullmatch(password_pattern, password) 56 57 # validating conditions 58 if password_match: 59 print("Password is valid.") 60 else: 61 print("Password invalid !!") 62 63# Driver Code 64if __name__ == '__main__': ...
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!!