Recently after work has been thinking how to improve work efficiency, especially the interface test to a piece, and then thought of the interface automation, in the long-term testing process will often do interface testing, but that is only a single interface, then how do we do batch interface testing it? Consider yourself as follows:
1, in a working directory to prepare several files, the main program to run the file, interface address text file, request parameter text file, request parameter value of the text file
2, the main thinking: by running the main program, to get the interface address of the text file, and then the address corresponding request parameters and request parameter values corresponding, and then splicing on
3, and then send the request to the server through Urllib, according to the returned status code and return value to determine whether the interface is successful
Here is a simplified version of the script written:
#__author__ = ' Ytxu ' #coding =utf-8import urllib,urllib2,os,sys,jsonclass interfaceautotestmain: #读取接口 def Interfaceda Ta (self): file = ' InterfaceData.txt ' interfacedatadata = ". Join (self. ReadFile (file)) return interfacedatadata# read variable def Variety (self): postvariety = [] Varietydata = ' V ArietyDataFile.txt ' Data = self. ReadFile (Varietydata) for Lindata in Data: #postData [lindata] lin = lindata.replace (' \ n ', ') Postvariety.append (Lin) return postvariety# read parameter data def Parameter (self): Postparameter = [] Parameterdata = ' ParameterDataFile.txt ' Data = self. ReadFile (Parameterdata) for Lindata in Data: #检查请求数据中是否包含多元数组 lin = lindata.replace (' \ n ', ') Postparameter.append (Lin) return postparameter# read file def ReadFile (self,filename): F = open (file Name) data = F.readlines () return data# splicing variable and data def postdata (self): #将两个列表合并为一个字典 Data =dict (Zip (self). Variety (), self. Parameter ())) Return data# interface, data splicing def soudata (self): Jdata = Urllib.urlencode (self. PostData ()). Replace (' + ', '). Replace ("%27", "%22") req = Urllib2. Request (self. Interfacedata (), jdata) response = Urllib2.urlopen (req) responsestr = response.read () code = Response. GetCode () if (code ==200): ResJson = Json.loads (responsestr) if (resjson["ret"]!=0): Print "Error" #print eval (code) #脚本运行run = Interfaceautotestmain () run. Soudata ()
Realize the initial conception of interface automation and realize the first part