Today the next Python writes the interface test case with request, makes a rough approximate execution, and needs to find time to optimize. This takes an object, configuration, and common function as a class, and executes the test case for another class.
The test case is written roughly as follows (and needs to be beautified):
1. Write Test Cases
Here, you use Pyunit to read the test cases in Excel and execute
# Coding=utf-8
From OPENPYXL import Load_workbook
Import Sys
Import JSON
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')
From Requestconfig import TestClass
Import UnitTest
Class Testapi (UnitTest. TestCase):
def setUp (self):
Self.test=testclass ()
Print ("Start")
def tearDown (self):
Print ("End")
def test0000101 (self):
Test=self.test
Host=test.readsheetdata (' D2 ')
Path=test.readsheetdata (' E2 ')
Data=test.readsheetdata (' F2 ')
Method=test.readsheetdata (' G2 ')
Url=host+path
Print URL
Print data
Print method
Res=test.httpgetorpost (Method,url,data)
Print Res
testcode=res[' Code ']
def test0000201 (self):
Test=self.test
Host=test.readsheetdata (' D12 ')
Path=test.readsheetdata (' E12 ')
Data=test.readsheetdata (' F12 ')
Method=test.readsheetdata (' G12 ')
Url=host+path+test.getafterurl ()
Res = Test.httpgetorpost (method, URL, data)
Print Res
Test.assertresponsecode (res[' code '), ' H12 ')
if __name__ = = "__main__":
Unittest.main ()
1. Configuration files
It is primarily the function used in encapsulation. Include HTTP requests, read Excel,url configuration
Import requests
Import UnitTest
Import JSON
From OPENPYXL import Load_workbook
Import Sys
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')
Import CSV
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')
Class TestClass (object):
#封装http the POST function to return the request response
def httppost (Self,keyword,url):
# data=json.dumps (keyword)
headers={"Content-type": "Application/json"}
Res=requests.post (Url,data=keyword,headers=headers)
Responsejson=res.json ()
Return Responsejson
#封装http get function, return response
def httpget (Self,keyword,url,data):
headers={"Content-type": "Application/json"}
Res=requests.get (Url,data=data,headers=headers)
Responsejson=res.json ()
Return Responsejson
#封装请求函数
def httpgetorpost (Self,method,url,data):
# Global MREs
headers = {"Content-type": "Application/json"}
If method in "get":
Mres=requests.get (Url,data=data,headers=headers)
Elif method = = "POST":
# postdata = json.dumps (data)
Mres=requests.post (Url,data=data,headers=headers)
Elif method in "put":
Mres=requests.put (Url,data=data,headers=headers)
Elif method in "delete":
Mres=requests.delete (Url,data=data,headers=headers)
Else
MREs = Requests.post (URL, data=data, headers=headers)
Print ("error")
# Responsejson=mres.json ()
Return Mres.json ()
#读excel
def readsheetdata (Self,cell):
Wb=load_workbook (R ' d:\apitestcase.xlsx ')
Sheet=wb.active
Value=sheet[cell]
# Print (Value.value)
Return Value.value
#封装断言函数
def assertresponsecode (Self,code,cell):
Excelcode=self.excelcode=self.readsheetdata (cell)
Try
Assert Excelcode==code
Except Assertionerror as msg:
Print "Assertion Failed"
Return False
Else
Return True
def getloginusermess (self):
Method= "POST"
Url= "Http://apptest.buddyniu.com/api/apps/BUDDY_API_TEST/accounts/login"
data={"password": "e10adc3949ba59abbe56e057f20f883e", "Clientsecret": "a123af4e331cf61c0324cd43cbc2135d", " AccountId ":" 13590404631 "}
data = json.dumps (data)
Res=self.httppost (Data,url)
return res
def getaccessstoken (self):
Res=self.getloginusermess ()
Return res[' data ' [' Accesstoken ']
def getUserId (self):
Res=self.getloginusermess ()
Return res[' data ' [' userId ']
def getrefreshtoken (self):
Res=self.getloginusermess ()
Return res[' data ' [' Refreshtoken ']
def getafterurl (self):
Usermess=self.getloginusermess ()
Afterurl= "? access_token =" +usermess[' data ' [' Accesstoken ']+ "&userid =" +usermess[' data '] [' userId ']
Return Afterurl
Python+request+excel do interface Automation test (ii)