Python + Requests +Excel+Jenkins interface automation

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

def get(url,querystring):
    headers = requestHeader()
    response = requests.request("GET", url, headers=headers, params=querystring)
    # print response.url
    return response

def post(url,payload,querystring):
    headers = requestHeader()
    response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
    # print response.url
    return response

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.


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

caseName    =   caseFile.getCellVaule(ws,rowIndex,1)
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;

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

text            =   json.loads(response.text)

spendTime       =   response.elapsed.total_seconds()

statue          =   response.status_code
self.assertEqual(statue, 200)

(6) writes the result of the request to an HTML file and displays


(7) is configured to Jenkins to periodically execute the accessibility of the view interface

learning Python address:

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.

Read More: