import allure
from urllib3 import encode_multipart_formdata
from common.common_requests import Requests
from common.tools import sep, get_project_path
class TestApi:
@allure.feature("product")
@allure.story("upload_img")
@allure.description("上传头像图片")
def test_upload_image(self, token):
img_path = get_project_path() + sep(["img", "head.jpg"], add_sep_before=True)
file_data = {"file": ("upload_img", open(img_path, "rb").read())} #接口格式 file : with open()as upload_img
encode_data = encode_multipart_formdata(file_data) #转为url格式
# print(encode_data)
# print(encode_data[0])
# print(encode_data[1])
data = encode_data[0]
headers = {"token": token("miaozz"), "Content-Type": encode_data[1]}
res = Requests(headers).post("/api/product/upload_img", data=data)
print(res.json())
assert res.json()["code"] == 200
assert res.json()["msg"] == "成功"
写错了调用接口,没想到也能调用成功,{'code': 200, 'data': 'http://192.168.1.73:9090/product/product_img/16801476474095ad508a8-4681-4e3f-98ff-7380d05b095d', 'msg': '成功'} 就是不是头像的,所以我一直看头像有没有更新,最后发现是自己的接口地址写错了。