Realize the initial conception of interface automation and realize the first part

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.