If you want to learn python automated testing well, of course, the indispensable knowledge is the basic knowledge of python. Many students have learned python again and again and can't get practical application. Therefore, over time, the basic knowledge slowly returns to zero, which not only wastes time but also eliminates their own initiative.
From the first period of our python interface automation test practice, some students were very confused when they saw args and * kwargs in the code, which is a typical lack of basic knowledge.
*args is used to package parameters into tuple s for function body calls
""" *args How to use: Used to package parameters into tuple Call to function body """ # Example 1 def func1(*args): print(args, type(args)) func1(1) # Example 2 def func2(x, y,*args): print(x, y, args) func2(1,2,3,4,5)
Execution results:
D:\tttesting\workspace\venv\Scripts\python3.exe D:/tttesting/test.py (1,)<class'tuple'> 12(3,4,5)
**kwargs packages keyword parameters into dict for function body calls
""" **kwargs How to use: Packaging keyword parameter assembly dict Call to function body """ # Example 1 def func3(**kwargs): print(kwargs, type(kwargs)) func3(a=2) # Example 2 def func4(**kwargs): print(kwargs) func4(a=1, b=2, c=3) def func(arg,*args,**kwargs): print(arg, args, kwargs) func(6,7,8,9, a=1, b=2, c=3)
Execution results:
D:\tttesting\workspace\venv\Scripts\python3.exe D:/tttesting/test.py {'a':2}<class'dict'> {'a':1,'b':2,'c':3} 6(7,8,9){'a':1,'b':2,'c':3} Process finished withexit code 0
The following contents are extracted from the basic knowledge added to the students in the first practical course of python interface automation test. Have you mastered them? I hope it will help you.
# -*- coding:utf8 -*- """method""" # def common(name, value): # print("this is {}. format(name)) # print(value) # # # def study_data_type_v2(): # common("character", "welcome to 1213abc") # common("list", [1, "string", dict(name='tt', age=123), [1, 3, 4, 5,],]) # print("there are other ordered data types: Yuanzu, set, etc.) # common("dictionary", ) """1. Simple data structure""" def study_data_type_v1(): print("I am a string") print("Here is the list") print([1, "character string", dict(name='tt', age=123), [1,3,4,5,], ])# Add an object here print("There are other ordered data types: Yuanzu, set, etc") print("Here is the dictionary") print({"name":'yyt', 'age':12} ) """2. Simple data type operations""" # region string str_a ="I like it" str_b ="having dinner" str_c ="sleep" test_1 ="This is a combination"+"String of"# String splicing 1 test_2 ="_".join([str_a, str_b, str_c])# String splicing 2 test_2 = test_2.split("-")# String segmentation test_3 = str(test_2).replace("_","&&")# String replacement str_d ="String judgment" bool_result = bool("judge"in str_d) str_e ="Traversal of string" for i in str_e: print(i) for index, i in enumerate(str_e): print("The sequence number in the string is{}The Chinese characters are{}".format(index, i)) # endregion # region list list_a =[1,3,5,4] list_b =["a","b","c"] list_c =[1,1,1,1] list_d =['d'] list_b.extend(list_d)# Merge two lists list_b[-1]='e'# assignment list_b.append('f')# Add split_list_1 = list_b[1:2] split_list_2 = list_b[:2] split_list_3 = list_b[1:] # judge bool_result = bool("a"in list_b) # sort list_a.sort(reverse=True) # ergodic for i in list_a: print(i) # endregion # region dictionary dict_a = dict(name='China', age=19) dict_b ={"name":"China", "age":19 } dict_a['age']=20# assignment dict_a.update(sex='female')# to update # judge bool_result = bool("name"in dict_b) keys = dict_a.keys() # ergodic for k, v in dict_a.items(): print("key: value:".format(key=k, value=v)) # endregion list_e =[i*10for i in range(0,5)]# Push to """ object """ classBaseObj(object): class_host ='http://www.class_dev.com' def __init__(self,**kwargs): """origin doc""" self.host ="http://www.dev.com" self.db ="test" # print("the most commonly used constructor") # self.update_value(**kwargs) # def __repr__(self): # print("print host according to my meaning: {}". Format (self. Host)) # print(self.__dict__) # # def __enter__(self): # print("I'm connecting to dB of {}". Format (self. DB)) # # def __exit__(self, exc_type, exc_val, exc_tb): # print("I need to close the current {} connection". format(self.db)) # def set_doc(self, doc): # self.__doc__ = doc # # def doc(self): # return self.__doc__ # # def update_value(self, **kwargs): # if kwargs: # for attribute_name in kwargs.keys(): # setattr(self, attribute_name, kwargs.get(attribute_name)) # def get_value(self, name, default=None): """ Gets the property value of the object :param name: Attribute name :param default: If no default value is returned :return: """ value = getattr(self, name, default) if value isNone: return default else: return value # # def clear_value(self): # for k, v in vars(self).items(): # setattr(self, k, None) # region modify attribute - example: uniformly modify the interface to access the host print(BaseObj.class_host) c =BaseObj() # Modify class properties BaseObj.class_host ='http://www.class_test.com' a =BaseObj() b =BaseObj() print(a.class_host) print(b.class_host) print(c.class_host)# Will this address change? # Modify the properties of object a a.host ='http://www.test.com' print(a.host) print(b.host) # Modify the properties of object b b.host ='http://www.test.com' print(a.host) print(b.host) # endregion # Do you know the effect of region after releasing various methods in the above objects? a =BaseObj() b =BaseObj(name='rabbit', age=19) # endregion # region inheritance classUserInfo(BaseObj): def __init__(self,**kwargs): self.name ="Dog" self.age =1 super(UserInfo, self).__init__(**kwargs) def set_name(self, name): self.name = name user_info =UserInfo(name="Hasch", db="dev")# Look at the properties inside user_info.name ="poodle" user_info.set_name("Ji Wawa") name = user_info.get_value("name") default_value = user_info.get_value("dddd","Default value") # endregion # region decorator import functools def log(func): @functools.wraps(func) def wrapper(*args,**kwargs): print('call %s():'% func.__name__) print('args = {}'.format(*args)) return func(*args,**kwargs) return wrapper @log def test(p): print(test.__name__ +" param: "+ p) test("I'm a param") # endregion
I hope the above contents can help you.
The following is the test data. It should be the most comprehensive and complete war preparation warehouse for friends doing [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!
Finally, it can be in the official account: the sad spicy bar! Get a 216 page interview document of Software Test Engineer for free. And the corresponding video learning tutorials for free!, It includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced continuous test integration, test architecture, development test framework, performance test, security test, etc.
Don't fight alone in learning. It's best to stay together and grow together. The effect of mass effect is very powerful. If we study together and punch in together, we will have more motivation to learn and stick to it. You can join our testing technology exchange group: 914172719 (there are various software testing resources and technical discussions)
Friends who like software testing, if my blog is helpful to you and if you like my blog content, please click "like", "comment" and "collect" for three times!