先搭建环境,具体百度有教程
开始写case(具体封装在研究中)
import requests
import json
def better_output(json_str)://美化JSON输出格式
return json.dumps(json.loads(json_str),indent=4)
def login():
url = "登录接口地址"
date = {'account': '姓名', 'password': '123456'}
r = requests.post(url, data=date)
print(r.status_code)
access_token = r.json()['content']['access_token']//拿到登录token,具体看登录成功后token的位置
return access_token
def test_search(): //搜索商品为例
token =login() //拿到登录token,避免再次登录
url1='https://devtpro.hopo.com.cn/api/v2/search_goods'
payload = {'per_page': '20',
'goods_type': '1',
'nav_param': 'built_in_components',
'client_type': '0',
'order_type': 'ASC',
'order': '0',
'current_page': '1',
'current': '1',
'per_page': '1'} //这部分信息可以在接口请求的时候拿到
headers={'Authorization': 'Bearer ' + token}//添加token到请求头
r2=requests.request("POST",url=url1,data=payload,headers=headers)
print(json.loads(better_output(r2.text)))
assert r2.json()['error_code'] == 0
test_search()