1.封装get,post,delete,post请求 api文件
# coding=utf-8
import json
import requests
class TestApi(object):
"""
/*
@param: @session ,@cookies
the request can be divided into session request and cookie request according to user's own choice
however,url and header is must ,other parameters are given by user to make it is None or not
*/
"""
def get(self,url,param,header,cookie=None,session=None,**kwargs):
if session:
return session.request("GET",url,param,headers=header,**kwargs)
elif cookie:
return requests.get(url,params=param,headers=header,cookies=cookie,**kwargs)
"""
/*
@param: @session ,@cookies
传入的是dict类型 python object 对象
header is form data: application/x-www-urlencoded
transfer data to data directly ,finally requests's submit will be like 'aa=dd&bb=ff' formation
header is json :application/json
due to the data can be 'str','dict'and tuple and so on ,so when we choose data and
data is given by dict,we must transfer it to json str,but when is json type str ,we must must
transfer python object dict to json str with json.dumps(),
finally the request submit data format is str like:
'aa=dd&bb=ff',but when choose json the submit will become like {'a': 'cc' ,'b': 'dd'} ,
data and json cant not be used in the requests at the same time
*/
"""
def post_data(self,url,type,data,header,cookie=None,session=None,**kwargs):
if cookie:
if type is "data":
return requests.post(url,data=data,header=header,cookies=cookie,**kwargs)
elif type is "json":
return requests.post(url,data=json.dumps(data),headers=header,cookies=cookie,**kwargs)
elif session:
if type is "data":
return session.request("POST",url,data=data,header=header,cookies=cookie,**kwargs)
elif type is "json":
return session.request("POST",url,data=json.dumps(data),headers=header,cookies=cookie,**kwargs)
"""
/*
@:param:@json object
json的value为传入可json可序列化python对象
请求header默认:ContentType: application/json
*/
"""
def post_json(self,url,header,json,cookie=None,session=None,**kwargs):
if cookie:
return requests.post(url,headers=header,json=json,cookies=cookie,**kwargs)
elif session:
return session.request("POST",url,headers=header,json=json,**kwargs)
"""
/*
@:param: @url,@data,@**kwargs
Tip: header you need to according to your api to be given in **kwargs position
*/
"""
def put(self,url,data,cookie=None,session=None,**kwargs):
if cookie:
return requests.put(url,data,cookies=cookie,**kwargs)
elif session:
return session.request("PUT",url,data,**kwargs)
"""
/*
@:param: @url,@data,@**kwargs
Tip: header you need to according to your api to given in **kwargs position
*/
"""
def delete(self,url,data,cookie=None,session=None,**kwargs):
if cookie:
return requests.delete(url,data,cookies=cookie,**kwargs)
elif session:
return session.request("DELETE",url,data,**kwargs)
2、编写读取配置文件以及参数获取类
# coding=utf-8
from ruamel import yaml
from API.apitest import *
"""
/*@param: python version 3.7
第一步制造配置文件yaml或者json都可以保存请求报文接口参数的:
写入方法很简单见:Jsread.py的Yml,Js类的write()方法
*/
"""
class Yml(object):
def __init__(self, yml_path):
self.yml_path = yml_path
def read(self):
with open(self.yml_path, 'r', encoding='utf-8')as f:
data = yaml.load(f,Loader=yaml.Loader)
return data
class EnvParameter(object):
def __init__(self, con_path):
defaults = {"url": None,
"header": None,
"data": None,
"method": None,
"param": None,
"type": None,
"json": None}
self.cookies = None
self.session = None
self.con_path = con_path
dict = Yml(self.con_path).read()
defaults.update(dict)
self.url = defaults["url"]
self.header = defaults["header"]
self.method = defaults["method"]
self.data = defaults["data"]
self.param = defaults["param"]
self.json = defaults["json"]
self.type = defaults["type"]
class TestSend(EnvParameter):
def __init__(self,config_path,cookie1=None,session1=None):
# EnvParameter.__init__(self,conpath=None ,url=None,method=None,header=None,type=None,data=None,param=None,json=None,cookies=None,session=None)
EnvParameter.__init__(self,config_path)
self.session=session1
self.cookie1=cookie1
# print(self.param,self.type) #测试下类继承效果
def send(self):
if self.method.upper()=="GET":
rep=TestApi().get(self.url,self.param,self.header,cookie=self.cookie1,session=self.session)
return rep
elif self.method.upper()=="POST":
rep=TestApi().post_data(self.url,self.type,self.data,self.header,cookie=self.cookie1,session=self.session1)
return rep
elif self.method.upper()=="PUT":
rep=TestApi().put(self.url,self.data,cookie=self.cookie1,session=self.session1)
return rep
elif self.method.upper()=="DELETE":
rep=TestApi().delete(self.url,self.data,cookie=self.cookie1,session=self.session1)
return rep
# if __name__ == "__main__":
# TestSend('./conf.yaml')
3.编写测试框架unitest,这里举个例子百度的验正框架有效性,白猫黑猫抓住老鼠好猫
import unittest
import requests
from API.testyaml import *
class Interface(unittest.TestCase):
@classmethod
def setUpClass(cls):
global session
s = requests.session()
requests.get(url="http://www.baidu.com")
session =s
print("---------------开始测试所有接口--------------")
@classmethod
def tearDownClass(cls):
"""清除cookie"""
session.cookies.clear() #也可以这样写 session.cookies=None
print("---------------加载所有接口结束销毁cookie--------------")
def test_001(self):
response=TestSend('./conf.yaml',session1=session).send()
print(response.status_code)
if __name__ =="__main__":
unittest.main()
测试报告生成见我的另一篇文章,传送门 :https://blog.csdn.net/chen498858336/article/details/83794116
测试报告模板见我文章另一篇非官方野生优化版,传送门:https://blog.csdn.net/chen498858336/article/details/83756509