Best Python code snippet using assertpy_python
test_expected_exception.py
Source:test_expected_exception.py
...111def func_noop(*args, **kwargs):112 pass113def func_no_arg():114 raise RuntimeError('no arg err')115def func_one_arg(arg):116 raise RuntimeError('one arg err')117def func_multi_args(*args):118 raise RuntimeError('multi args err')119def func_kwargs(**kwargs):120 raise RuntimeError('kwargs err')121def func_all(arg1, arg2, *args, **kwargs):122 raise RuntimeError('all err: arg1=%s, arg2=%s, args=%s, kwargs=%s' % (arg1, arg2, args, [(k, kwargs[k]) for k in sorted(kwargs.keys())]))123class Foo(object):124 def bar(self):...
list_demo.py
Source:list_demo.py
...91 # çæå¨åªæå¨ä½¿ç¨å
ç´ çæ¶åæä¼çæ,èçå
å92 for i in generator:93 print(i)94 # å¦æçæå¨è¡¨è¾¾å¼æ¯ä¸ä¸ªå½æ°å¯ä¸çåæ°,åå¯ä»¥çç¥()95 func_one_arg(x * 2 for x in lst)96 # æå¤ä¸ªåæ°,åä¸è½çç¥()97 func_two_arg((x * 2 for x in lst), "arg2")98 def test_list_func(self):99 """ä½ç¨äºlistçä¸äºå½æ°"""100 lst = [1, 2, 3, 4, 5]101 print(f"sum:{sum(lst)},min:{min(lst)},max:{max(lst)}")102 # åªè¦æä¸ä¸ªä¸ä¸ºNone,0,""(" "ä¸ç®)çå°±è¿åTrue,å¦åè¿åFalse103 print(f"any False:{any(['', 0])},True:{any(['', 0, 1])}")104 # ææå
ç´ é½ä¸ä¸ºNone,0,""(" "ä¸ç®)çå°±è¿åTrue,å¦åè¿åFalse105 print(f"all False:{all(['', 0, 1])},True:{all(['1', 2, 1])}")106 lst2 = ["a", "b", "c", "d"]107 lst3 = ["e", "f", "g", "h"]108 # zipå°listå表çå
ç´ ç»ætuple,é¿åº¦ç±å
ç´ æå°çlistå³å®109 print(f"zip:{[i for i in zip(lst, lst2, lst3)]}")110 zip_list = [(1, "a"), (2, "b"), (3, "c")]111 # zip(*zip)å°±æ¯zipçååæä½112 lst, lst2 = zip(*zip_list)113 print(f"unzip: lst:{lst},lst2:{lst2}")114 def test_random(self):115 lst = [1, 2, 3, 4, 5]116 # choiceéæºè¿ålistä¸çä¸ä¸ªå
ç´ 117 print(f"choice:{random.choice(lst)}")118 # 对listè¿è¡éæºæåº119 random.shuffle(lst)120 print(f"shuffle:{lst}")121def func_one_arg(arg):122 for i in arg:123 print(i)124def func_two_arg(arg1, arg2):125 for i in arg1:...
test_intercepts.py
Source:test_intercepts.py
...6def func_no_args():7 return "func_no_args"8def func_no_return():9 pass10def func_one_arg(arg1):11 return "func_one_arg", arg112def func_two_args(arg1, arg2):13 return "func_two_args", arg1, arg214def func_one_kwarg(kwarg1="kwarg1"):15 return "func_one_kwarg", kwarg116def handler(func, *args, **kwargs):17 result = func(*args, **kwargs)18 return "handled", result19class TestRegisterFunctionHandler(unittest.TestCase):20 def setUp(self):21 intercepts.unregister_all()22 def test_register_func(self):23 self.assertEqual(24 intercepts.register(func, handler),25 func,26 "intercepts.register should return the function to be handled",27 )28 def test_register_func_no_args(self):29 result = func_no_args()30 intercepts.register(func_no_args, handler)31 self.assertEqual(32 func_no_args(), ("handled", result), "handler function should modify output"33 )34 def test_register_func_no_return(self):35 intercepts.register(func_no_return, handler)36 self.assertEqual(37 func_no_return(), ("handled", None), "handler function should modify output"38 )39 def test_register_func_one_arg(self):40 arg1 = 141 result = func_one_arg(arg1)42 intercepts.register(func_one_arg, handler)43 self.assertEqual(44 func_one_arg(arg1),45 ("handled", result),46 "handler function should modify output",47 )48 def test_register_func_two_args(self):49 arg1, arg2 = 1, 250 result = func_two_args(arg1, arg2)51 intercepts.register(func_two_args, handler)52 self.assertEqual(53 func_two_args(arg1, arg2),54 ("handled", result),55 "handler function should modify output",56 )57 def test_register_func_one_kwarg_1(self):58 result = func_one_kwarg()...
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!!