I haven't written a blog for a long time. , a busy job and a new job recently. 、
Read the previous written simple interface automation, take out the summary, also counted the results of learning
First to paste the final result, the result is written in the original Excel
After execution, the result is written to the "Status" column;
If the returned JSON has a message written to the "Response" column, if the execution fails, the returned JSON is also written to the
Pre-Preparation:
In Excel, fill in the interface, the request type, the data to be passed, the status code expected to return, and the description of the interface.
Code below, writing is very primitive, even do not define the method what, let's call V0.1 back to learn to have to further modify
#Coding:utf-8ImportRequestsImportJSONImportxlrdImportOS fromXlutils.copyImportCopyexcel='Apidemo.xls' #defines the Excel path that holds the use case, under the current pathdata =Xlrd.open_workbook (Excel) host='Http://xxxxxxx/api'#test Suit, write this yourself.headers = {'accept-version':'v2','Content-type':'Application/json'}#Define Headersresult= []#used to store validation resultsResponsevalue = []#Store the returned dataTable =data.sheet_by_index (0) Nrow= Table.nrows#Get row Count forIinchRange (1,nrow):#loop to get data in each rowRequestmethod = Table.cell (i,2). Value#method, post, get, deleteurl = host + Table.cell (i,1). Value#Stitching URLPayload = Table.cell (i,3). Value#define the value to passex = Table.cell (i,4). Value#gets the expected return value, which is now judged by Status_code. ifRequestmethod = ='Get':#Different ways to send different requestsR = Requests.get (Url,headers =headers)elifRequestmethod = ='Post': R= Requests.post (Url,data = Payload,headers =headers)elifRequestmethod = ='Delete': R= Requests.delete (Url,data = Payload,headers =headers)ifR.status_code = = ex:#if, as expected, case passesResult.append ('true') if 'message' inchR.json (). Keys ():#This depends on the situation, this way, such as account password error is also in the messageResponsevalue.append (R.json () ['message'])#deposit the message first into response Else: Responsevalue.append ("') Else: Result.append ('false')#If the result is incorrect, write it if there is a message, and write it all if 'message' inchR.json (). Keys (): Responsevalue.append (R.json () ['message']) Else: Responsevalue.append (Json.dumps (R.json (), Ensure_ascii=false))#do not decode cannot writer.close ()Print('A total of%d URLs, when%d is executed'% (nrow-1, i)) Book=Copy (data) Sheet1= Book.get_sheet (0)#Copy the original Excel forJinchRange (1,nrow):#writes the result to the corresponding table #将结果和response都写入到复制的工作表中Sheet1.write (j,5,result[j-1]) Sheet1.write (J,6,responsevalue[j-1]) Os.remove (Excel) Book.save (Excel)#to remove the original Excel, save the new Excel
The code to get the returned message, to see the actual situation, my side is the development of the error is returned through the message, such as the requested data does not exist, the requested registration of the mobile phone number has been registered and so on
There are many deficiencies in the code, such as the absence of an exception, and the end if an exception occurs. 、
In addition to the return status code is judged, there are a lot of things you can do, such as the structure of the returned JSON to judge, query the database, such as delete request, the interface returned successfully, but not necessarily the database has actually been deleted
There are unittest and Htmltestrunner used before using Appium, feeling can be used together with this;
In that case, the feeling can be customized case, for each interface of different functions, to make targeted judgments
Simple interface Automation V0.1 implemented by Python+excel