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>
Read More:
- Failed to establish a new connection: [winerror 10048] in the requests thread pool, the interface is called circularly to get the datagram error
- [Solved] Python requests ConnectionError Error: connection aborted BadStatusLine
- Python netmiko library Cisco telnet switch automation
- The automatic token of Python interface is passed into the header
- [Solved] Python Requests Error: simplejson.errors.JSONDecodeError: Expecting value
- Python Requests Error: Max retries exceeded with url
- Python requests Error: ConnectionError: (‘Connection aborted.‘, error(104, ‘Connection reset by peer‘))
- Python reads excel and stores it in the list according to the header hash copy.deepcopy
- A summary of a demo development process for Python using the QT5 development interface
- Python openpyxl excel open zipfile error resolution: zipfile.BadZipFile: File is not a zip file
- ModuleNotFoundError: No module named ‘requests‘ [How to Solve]
- [Solved] ModuleNotFoundError: No module named ‘requests‘
- [Solved] Pychar error: modulenotfounderror: no module named ‘requests_ HTML ‘solution
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- Pyexcel Error: xlrd.biffh.XLRDError: Excel xlsx file; not supported
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- [Solved] Cannot open .xlsx file, xlrd.biffh.XLRDError: Excel xlsx file; not supported
- Python Error: pip install mysql-connector-python failed