Requests,PyTest

1:将Post和Get请求进行封装

GET:

  • 无参:get(URL)
  • 含参数:
    • URL后拼接
    • get(URL,params=)

POST:

  • post(URL,data=,headers=) 设置请求头信息headers
  • post(URL,json=)
  • post(URL,files=)
class BaseRequests():
    
    def __init__(self):
        #Session进行Cookie管理
        self.session=requests.session()

        #url从env.ini中读取
        #**kw关键字参数,相当于字典
    def get(self,url,**kw):
        """
        封装get请求,用于统一处理异常,记录日志
        :param url:请求的地址
        :param kw:关键字参数,调用时可传递任意多个带参数名的参数
        :return:响应
        """
        try:
            logging.debug(f"{url} get请求的参数为:{kw}")
            res=self.session.get(url,**kw)
            logging.debug(f"{url} get请求的返回值为:{res.text}")
            return res
        except Exception as e:
            logging.error(f"{url} get请求出错,错误信息为:{e}")

    def post(self,url,**kw):
        """
        封装get请求,用于统一处理异常,记录日志
        :param url:请求的地址
        :param kw:关键字参数,调用时可传递任意多个带参数名的参数
        :return:响应
        """
        try:
            logging.debug(f"{url} post请求的参数为:{kw}")
            res=self.session.post(url,**kw)
            logging.debug(f"{url} post请求的返回值为:{res.text}")
            return res
        except Exception as e:
            logging.error(f"{url} post请求出错,错误信息为:{e}")

上传文件:

def file(env_url,data,baseRequests):
    url=env_url+"文件上传路径"
    # 文件路径
    path=data['path']
    # 后端规定的文件接收参数
    fileName = data["fileName"]
    # 文件类型
    fileType = data["fileType"]
    with open(path, 'rb') as f:
        file = {fileName: (path, f, fileType)}
        return baseRequests.post(url, files=file)

API:

#获取响应状态码
res.status_code
#获取响应正文
res.json()
#获取请求头
res.request.headers

设置代理服务器:

# 定义代理服务器 经过本地fiddler服务器
proxy = {
    "http": "http://127.0.0.1:8888",
    "https": "http://127.0.0.1:8888"
}

# 运行时fiddler只需要启动,无需主动抓包
#proxies设置代理服务器
res = requests.get("", proxies=proxy)

2:PYTest

规则:

文件名以test开头

类名以Test开头

方法名以test开头

前后置方法:

# 面向过程:
  #方法
    setup_function() teardown_function()
  #模块
	setup_module() teardown_module()
# 面向对象:
  #方法 
    setup_method() teardown_method()
  #模块 
    setup_class() teardown_class()

建议使用fixture自定义前后置方法:

@pytest.fixture(autouse=True)
def login():
    print("前置:实现登录")
    yield #yield前为前置,yield后为后置 yield可以进行返回数据
    print("后置:实现退出登录")

使用fixture:

  • 将函数作为参数传入(可获取返回值)
  • 用例前添加装饰器@pytest.mark.usefixtures(“”)(无法获取返回值)
  • @pytest.fixture(autouse=True)(无法获取返回值)

fixture的Scope:默认:function|method->class类->module模块->package包->session一次会话(一次执行过程)

  • scope:class:类里第一次使用前置的用例前执行前置,在类里所有用例执行后执行后置
  • scope:module:模块中第一次使用前置的用例前执行前置,模块中所有用例执行后执行后置,一个模块只执行一次
  • scope:package:该包下第一次使用前置的用例前执行前置,该包下所有的用例执行后执行后置
  • scope:session:在一次执行过程中,第一次执行前置的用例前执行,所有用例执行后执行一次

作用域为package或者session时,应该将fixture放置在conftest.py

fixture的数据驱动:params:

#数据类型是列表
@pytest.fixture(params=[ 
    ['admin','123456'],
    ['customer','123456'],
    ['temp','123456']
])
def login_data(request):
    return request.param

fixture进行数据组合驱动:

@pytest.fixture(params=['admin', 'ADMIN', 'Admin'])
def search_str(request):
    return request.param


@pytest.fixture(params=['asc', 'desc'])
def order(request):
    return request.param


@pytest.fixture(params=[True, False])
def case_sensitive(request):
    return request.param

#一共3*2*2=12个用例,分别为不同的组合
def test_select(search_str,order,case_sensitive):
    print("查询的字符串",search_str,"  展示方向",order,"  忽略大小写",case_sensitive)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值