使用pytest框架进行接口请求

import requests
import pytest

class TestSendRequest:
#全局变量,即类变量,通过类名访问
access_token=“”
cks=“”

def test_get_token(self):
url=‘https://coolapi.coolcollege.cn/login-api/v3/login’
data={#body填写json格式、form格式
“mobile”:“132****”,
“password”:“***”,
“password_encrypted”:“1”,
“sms_code”:“”,
“login_type”:“account_password_login”,
“enterprise_id”:“”,
“anonymous_id”:“1800819662242582528”,
“outer_login_type”:“”
}
#发送get请求
rep=requests.get(url=url,json=data)
print(rep.json())
access_token = rep.json()[‘$.code’]

#发送post请求
def test_edit_flag(self):
url=“…?access_token=”+TestSendRequest.access_token+“”
data={}
rep=requests.post(url,json=data
#data=json.dumps(data) 序列化,把字典格式的数据转换成string格式
#json.loads(data) 反序列化:把str格式转换成字典格式
)
print(rep.json()

#上传文件:图片/文档等
def test_file_upload(self):
url=
data={“media”:open(r"本地文件地址",“rb”)

    }
    rep=response.post(url=url,files=data)
    print(rep.json())

#需要带请求头以及需要cookie关联的接口如何测试
def test_start(self):
url=‘’,
rep=requests.get(url=url)
print(rep.text)
#通过正则表达式从html中获得鉴权码和cookie
token=rep.search(‘name=“token” value=“(.*?)”’,rep.text)[1]
print(token)
cks=rep.cookies

def test_login(self):
url=“…”
data={}
headers={}
rep=requests.post(url,json=data,headers=header,cookies=TestSendRequest.cks
#data=json.dumps(data) 序列化,把字典格式的数据转换成string格式
#json.loads(data) 反序列化:把str格式转换成字典格式
)
print(rep.json())
if name == ‘main’:
pytest.main([‘-vs’])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值