python+requests+Excel+Jenkins interface automation
summary most people do interface automation process, found that basically is: python + requests do interface requests; Excel is used to store use cases; HTMLTestRunner generates test reports; Jenkins completes scheduled tasks.
actually takes only a fraction of the time to complete this process, with most of the time wasted in setting up the environment and writing use cases. Here I’m going to write down some of the problems.
interface request
(1) configuration request method: get and post method simple packaging, header parameter setting
# _*_coding:utf-8 _*
import requests
#get请求
def get(url,querystring):
headers = requestHeader()
response = requests.request("GET", url, headers=headers, params=querystring)
# print response.url
return response
#post请求
def post(url,payload,querystring):
headers = requestHeader()
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
# print response.url
return response
#设置请求的header
def requestHeader():
headers = {
'Content-Type': "application/json",
'Cache-Control': "no-cache"
}
return headers
(2) for the body of a post request, we used a “dict” in text at the beginning, but it always returned “error”. After that, postman used to debug and directly copied the body reference that had been converted into json to make the request, and it worked.
p>
WechatIMG12.jpeg
can be used to convert some problematic bodies in this way.
Another way to convert
(3) body to json: through ast module for conversion (recommended)
import ast
caseParams = ast.literal_eval(caseParams)
(4) get use case Excel
through openpyxl module
p>
p>
#用例名
caseName = caseFile.getCellVaule(ws,rowIndex,1)
#url
caseUrl = caseFile.getCellVaule(ws,rowIndex,3)
#请求方式
caseMethod = caseFile.getCellVaule(ws,rowIndex,4)
#参数
caseParams = caseFile.getCellVaule(ws,rowIndex,5)
note: in the process of obtaining the use case information, some parameters need to be fault-tolerant
A: whether there are parameters, if there are no parameters, need to pass null;
B: get parameter and post body need to be handled separately:
get parameter is ‘& Param ‘in the form of clear text spliced in the URL;
#将请求参数转换为json格式
caseParams = ast.literal_eval(caseParams)
keys = dict(caseParams).keys()
values = dict(caseParams).values()
for x in xrange(0,len(keys)):
tempQuery[keys[x]] = values[x]
, the body of the post needs to be put in dict;
caseParams = ast.literal_eval(caseParams)
(5) split the required parameters and write the result to the file
#请求返回的内容,转换为json格式
text = json.loads(response.text)
caseFile.setCellValue(ws,rowIndex,6,response.text)
#URL请求耗时
spendTime = response.elapsed.total_seconds()
caseFile.setCellValue(ws,rowIndex,7,spendTime)
#请求结果状态
statue = response.status_code
caseFile.setCellValue(ws,rowIndex,8,statue)
self.assertEqual(statue, 200)
(6) writes the result of the request to an HTML file and displays
p>
WechatIMG13.jpeg
(7) is configured to Jenkins to periodically execute the accessibility of the view interface
learning Python address: https://ke.qq.com/course/2707042?flowToken=1025648
if there are any problems you for learning Python, learning methods, learning course, how to learn efficiently, can be consulting me at any time, or the lack of system study materials, I do this year longer, they think is experienced, can help everyone puts forward constructive Suggestions, this is my Python communication qun: 785128166, if you have any question can be consulting me at any time.
p>